1

I have an interesting question related to scheduling my Docker Container.

I have a virtual machine with a docker image inside of it. I can spin up a container from the image that has an entrypoint (jar).

I am in a dilemna on how I should schedule the jobs to run against the jar.

Here are my 2 options:

1) If I run a cron on the VM then I can call the docker run commands to produce a container and run different jobs against the jar. The issue here is that once a single job is run, the container automatically terminates and the second job has no previous histroy of the previously run jobs. I believe theres a way to keep a container running forever with an entry point but I haven't explored/tested this yet.

2) I can install a cron inside of the container and through the Dockerfile provide the cron for updating - however, the issue here is how would I launch the container and keep it running for the cron jobs to run?

If you need any further details please do let me know.

Thanks!

  • Why does your container terminates when your run the job? – storm Sep 5 '18 at 13:38
  • When you use an entrypoint in your image and you launch a container from that image - the entrypoint is used as the executable. When the executable finishes and returns an exit code - the container terminates as well – Saiyam Sethi Sep 6 '18 at 21:19
1

You don't need cron. Most server OS-es (CoreOS, RedHat, CentOS, Ubuntu, etc.) these days run systemd, and systemd unit files can run your container to execute the job, for example:

Two unit files. One is mytimedstuff.service and mytimedstuff.timer. The .timer defines when the service should be executed.

In mytimedstuff.service

[Unit]
Description=Executes mystuff
After=default.target docker_network_apps.service
Requires=default.target docker_network_apps.service

[Service]
Type=oneshot
User=root
ExecStartPre=/usr/bin/docker pull mystuff:stable
ExecStartPre=-/bin/bash -c "/usr/bin/docker rm -f mystuffcontainer 2>/dev/null"
ExecStart=/bin/bash -c "/usr/bin/docker run --name mystuffcontainer mystuff:stable mystuff"

In mytimedstuff.timer:

[Unit]
Description=My stuff runs at 00:10 sharp.

[Timer]
OnCalendar=00:10:00

[Install]
WantedBy=multi-user.target
0

My understanding is that you want to run multiple services in your container.

The best practice to run several jobs is to create several containers for your different processes and manage them through docker compose this allows you to define in one file all the containers needed for your app and launch in one single command.

Otherwise, docker docs provide two simple solutions for your situation.

One thing to remember when doing Docker: one container, one service.

  • Interesting - the service is still one, however, the service runs multiple co-related jobs that are dependent of each other. – Saiyam Sethi Sep 7 '18 at 14:26
  • I would use a bash script as entrypoint and manage this dependency within the script. – storm Sep 7 '18 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.