

To set up Docker, you will need to prepare the system for installation. Access to a command line/terminal window.Make sure your new cron file is executable with a little chmod and chown and then apply your changes. In your Docker file you’ll need to make sure your script is executable and copy your cron file to the right place. Having a valid cron file is just half the battle in getting cron to run successfully in Docker. PATH=/your-working-directory/cron-scripts:/usr/local/bin:rest-of-path-goes-hereĠ * * * * scheduled_script.py > /proc/1/fd/1 2>&1 It sets up PATH to point to my scripts, changes the shell to bash, make sure cron can see environment variables, and gets my script ready to run each hour piping its output to STDOUT and errors to STDERR. The below example is where my cron file ended up, once I got everything working. This script enables Bash’s job control, makes sure environment variables are in a place cron can get to them at, and starts up the cron service itself.

proc/1/rd/1 is the trick for getting output in the right place.įirst, I created a little shell script - build_projects_env.sh - for my container to run to get things ready for cron. My container runs in Docker Swarm, so it was also important that the output for my script landed in the same STDOUT that eventually ends up in docker.log. I added my scripts directory in Docker to PATH in my cronfile and set bash as my SHELL. I’m sure there’s a way to get it to work that way, but I didn’t want to play around with that. In my case, I wanted to run a few Python scripts on a schedule. With syslog installed, you can tail /var/log/syslog while you’re debugging your cron files, which is incredibly helpful. Add apt-get install cron and rsyslog in your Dockerfile before you start anything else. There’s no cron nor is there a syslog when you want to debug things. It’s not difficult once you figure it out, but it did take a bit of research and learning to get everything to work.įirst off, Debian Slim is real slim. Recently, I needed to get cron working inside a Docker container running Debian Slim.
