One-off cronjob container on Docker Swarm

Docker containers are good for running small tasks due to the size and memory requirement for running in a server. It makes docker a perfect candidate for running short-lived and one-off jobs.

Throwing Docker Swarm into the picture, we get a cluster where we can start the docker service and let docker select the suitable worker node that has enough compute capacity and memory to schedule this new container instead of us worrying about that selection process. But how do we run a one-off container that starts a process, executes it and die after the execution?

--restart-condition none

is your life savor.
For example,

docker service create --name nginx --restart-condition none nginx

This would create a new nginx container and schedule this in one of the worker nodes and if the nginx stops then the container will stop and will not restart. This is not helpful where the container hosts an application. Here we need 100% uptime, however, the use case for this trick is the one-off cronjob container.

Leave a Reply

Your email address will not be published.