Automatically build and deploy a containerized Python app to Docker Hub

Peter Andersson
3 min readApr 20, 2021

Docker really has improved the stability and “deployability” (if that is a word) of applications.

But building containers is a step that I can live without, it should of course be automatically built whenever I change the code.

By following the steps outlines below, you will have a basic CI/CD setup in no time.

This simple setup ignores tests, so we can assume that we only push tested and validated code to the main Github branch, from where the container is built.

Prerequisites

  • a Github repository with your Python app, and a working Dockerfile
  • an account at Docker Hub

Overview of actions

  • Link your Docker Hub account to your Github account. You do this from Docker Hub, specifically https://hub.docker.com/settings/linked-accounts
  • Create a repository (let’s call it “my-new-repo”) in your Docker Hub account using https://hub.docker.com/repositories
  • Add secrets to your Github repository so that Github can login to your Docker Hub repo.
  • Setup a Github Action that will build your Docker image and push it to Docker Hub

Adding secrets

It’s best if you start with this, since when you check-in your first version of the Action, Github will automatically detect that and run your script. And if you don’t have your secrets setup, it will fail, and you will be sad.

Go to the Settings page of your repo and down on the left you will find the Secrets menu item.

Start with adding two SECRETS to your Github repository

Call them DOCKER_HUB_USER and DOCKER_HUB_PASSWORD. Use the Docker Hub username and password tha you already know.

Setting up a Github Action

Go to the Actions Page, and select “set up a workflow yourself”

Remove the pre-inserted code, and replace it with this template, but change “my-dockerhub-username” and “my-dockerhub-repo” to your Docker Hub equivalents.

name: Docker Image CI 
on:
push:
branches: [ main ]
jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Push to Docker Hub
uses: docker/build-push-action@v2
with:
push: true
tags: my-dockerhub-username/my-new-repo:latest

This script will now be run when you do a “git push” to the “main” branch, but you can customize it to run on other events too.

Now when you save/commit this new Github Action script, it will trigger a build (since you just pushed an update to the repo).

Github will build the image, and then use DOCKER_USER/DOCKER_PASSWORD to login and push the image to Docker Hub.

You can inspect the build log by (still on the Actions page) by clicking on the workflow.

If you login to Docker Hub again, you can see the newly pushed image.

The next step is to let Docker Hub call a webhook which in turn could do a “docker pull my-dockerhub-user/my-new-repo. Look out for part 2 of this article series where I will describe that part as well as how to manage multiple Docker containers.

--

--

Peter Andersson

Experienced programmer since 30+ years. Working as a consultant mainly in finance/trading but also in general data analytics. Likes Java and Python, and Linux.