Bert has already published an excellent article on Docker, one year ago. Since then, we continued to experiment on this topic and I would like to present a very simple (but not trivial) use case:
run a cron job in a docker container.
The source code is available on github.
Depending on your platform, you can find the complete documentation on the docker website.
The cron job
We will create a cron job that will display “Hello world”, every minute, in the console
Let’s create a new file called “crontab” to describe our job.
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1 # An empty line is required at the end of this file for a valid cron file.
This job will echo “Hello world”, every minutes, and redirect the output in the file /var/log/cron.log
- a blank line is required at the end of this file!
- the job is run by the “root” user as it is the only user that will exist in the container
The docker image
The following DockerFile describes all the steps to build your image
FROM ubuntu:latest MAINTAINER firstname.lastname@example.org # Add crontab file in the cron directory ADD crontab /etc/cron.d/hello-cron # Give execution rights on the cron job RUN chmod 0644 /etc/cron.d/hello-cron # Create the log file to be able to run tail RUN touch /var/log/cron.log # Run the command on container startup CMD cron && tail -f /var/log/cron.log
Then you can build the image with
sudo docker build --rm -t ekito/cron-example .
And run it:
sudo docker run -t -i ekito/cron-example
Be patient, wait for 2 minutes and your commandline should display:
Hello world Hello world
Now you can create you own script to backup you data, check your system status, or whatever you want !Google+