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.

Photo by Annie Spratt on Unsplash
# ┌───────────── 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>
your-project
├── task.py
├── crontab
├── Dockerfile
└── requirements.txt
docker build -t image-name:image-tag .
docker run -d image-name:image-tag

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.

your-project
├── task.py
├── crontab
├── Dockerfile
└── requirements.txt
└── .github
└── workflows
└── build.yml

Computer science student and certified shipping agent

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store