AUTOMATE YOUR LIFE
Running scheduled Python tasks in a Docker container
A boilerplate project for running a Python script in a Docker container using crontab. Including email alerts.
Python is really handy if you want to automate any mundane task you have to do on a regular basis. For example checking if there is an update on any given website, or always book a reservation for a meeting room as soon as possible.
All of these task have something in common. You want to schedule them and once they run you want to have as little maintenance as possible.
A Docker container using crontab is the perfect solution for this. Once you wrote your script, just set the cronjob to your schedule, build the container and run it on your machine or any cloud provider.
First of all we need a Python script that we want to run. I created one that sends me an email every time the script is run. You can use the code in your own project so that you can send yourself the results of the script without having to SSH into the Docker container.
All of the Python libraries used are already included in Python. If you need any other, make sure to add them to the requirements.txt
Now we need to call that script in our crontab file. Make sure to include an empty row at the bottom so it will run properly. The cronjob below runs every 30 minutes and saves the output of every print statement in a logfile that will be created in your working directory.
In case you forgot, here is a quick refresher on crontab syntax from Wikipedia
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6)(Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>
All that is left to do is our Dockerfile.
Once you are done your folder should look something like this:
Now build the container
docker build -t image-name:image-tag .
And run it
docker run -d image-name:image-tag
And you are done. You now have a scheduled job running inside a Docker container that you can run on any server, cloud provider or your own machine.
But wait, there is more!
If you want to automatically build the docker container and push it to a registry, every time you release a new version on GitHub — I got you covered. You can achieve just that using GitHub actions. All you need is a build.yml (the name does not matter, as long as it is a YAML file) inside .gitub/workflows. Your folder structure should now look as below.
Now we add a trigger that will run a docker build and docker push action, every time you release a new version on GitHub. Of course you could change that to pull requests on main, or whatever floats your boat.
I have created a template GitHub repository that you can use to setup your own project.