3

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!

2
  • Why does your container terminates when your run the job?
    – storm
    Commented Sep 5, 2018 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 Commented Sep 6, 2018 at 21:19

2 Answers 2

5

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
2

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.

4
  • Interesting - the service is still one, however, the service runs multiple co-related jobs that are dependent of each other. Commented Sep 7, 2018 at 14:26
  • I would use a bash script as entrypoint and manage this dependency within the script.
    – storm
    Commented Sep 7, 2018 at 15:25
  • 1
    Would you use another container to host the "controller" script? Or would you use cron or systemd on the machine that hosts all of the containers?
    – craig
    Commented Jul 1, 2021 at 12:26
  • @craig Also wondering this Commented Jan 16 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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