I am currently executing an exe file using windows task scheduler. I would like to run my exe file in docker container. Though I could think of running my exe file in docker, I am not sure how to schedule the run as it was achieved through windows task scheduler.
Please advise on how to schedule and run the .exe file in docker..
Note: Helm is what I use for deployment. So I cannot use docker-compose.yaml file.
Thanks,
As @David Maze rightly pointed out, you might be interested in CronJobs.
We can find in the CronJob documentation:
CronJobs are useful for creating periodic and recurring tasks, like running backups or sending emails. CronJobs can also schedule individual tasks for a specific time, such as scheduling a Job for when your cluster is likely to be idle.
You can use a CronJob to run Jobs on a time-based schedule, it's similar to Cron tasks on a Linux or UNIX system.
I'll create a simple example from scratch to illustrate how it works.
You typically create a container image of your application and push it to a registry before referring to it in a CronJob
.
I'm not sure if you have a docker image already built, so I'll create one too.
Suppose I have a job.py
Python script and want to "package" it as a docker image:
$ cat job.py
print("Starting job...")
for i in range(1, 6):
print(i)
print("Done")
Docker can build images automatically by reading the instructions from a Dockerfile.
I have a single Python script, so I will use the python:3
image as the base image:
$ cat Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY job.py .
CMD [ "python", "./job.py" ]
After creating a Dockerfile we can use the docker build command to build Docker image and docker push
to share this image to the Docker Hub registry or to a self-hosted one.
NOTE: I'm using Docker Hub in this example.
### docker build -t <hub-user>/<repo-name>[:<tag>]
$ docker build -t zyrafywchodzadoszafy/cronjob:latest .
...
Successfully built cc46cde8fcdd
Successfully tagged zyrafywchodzadoszafy/cronjob:latest
### docker push <hub-user>/<repo-name>:<tag>
$ docker push zyrafywchodzadoszafy/cronjob:latest
The push refers to repository [docker.io/zyrafywchodzadoszafy/cronjob]
adabca8949d9: Pushed
a1e07bb90a13: Pushed
...
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
zyrafywchodzadoszafy/cronjob latest cc46cde8fcdd 14 minutes ago 885MB
We can quickly make sure that everything is working by running this docker image:
$ docker run -it --rm zyrafywchodzadoszafy/cronjob:latest
Starting job...
1
2
3
4
5
Done
Now it's time to create a CronJob
:
$ cat cronjob.yml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-test
spec:
jobTemplate:
metadata:
name: cronjob-test
spec:
template:
metadata:
spec:
containers:
- image: zyrafywchodzadoszafy/cronjob:latest
name: cronjob-test
restartPolicy: OnFailure
schedule: '*/1 * * * *'
$ kubectl apply -f cronjob.yml
cronjob.batch/cronjob-test created
$ kubectl get cronjob cronjob-test
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob-test */1 * * * * False 1 10s 36s
In the example above, cronjob-test
CronJob
runs the job.py
Python script every minute.
Finally, to see if it works as expected, let's take a look at the Pods
spawned by the cronjob-test
CronJob
:
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
cronjob-test-1618581120-vmqtc 0/1 Completed 0 2m37s
cronjob-test-1618581180-nqqsd 0/1 Completed 0 97s
cronjob-test-1618581240-vhrhm 0/1 Completed 0 37s
$ kubectl logs -f cronjob-test-1618581120-vmqtc
Starting job...
1
2
3
4
5
Done
Much more information on specific configuration options can be found in the CronJob documentation.