Migrate a Python application into a Docker container

To avoid repetition, for overview and language agnostic examples on application migration to containers see Migrate your application into a Docker container

Update your application

Exclude downloadable libraries

Add a Python-specific .gitignore file to the root of the Git repository.

Read configuration values from environment variables

Python natively supports the reading of environment variables. The os.environ.get() function reads the environment variables. This small function can intelligently report missing variables.

import os
import sys

def get_config_value(variable_name):

  value = os.environ.get(variable_name, None)
  if value is not None:
    return value

  sys.exit(f"'{variable_name}' environment variable not found")

Call it with


Automate the application build

Create the init.sh file

Create the init.sh file in the source directory and enable the execution with the command:

chmod +x init.sh

This file will contain the terminal initialization steps for the application. This example

  • activates the Anaconda Python environment and
  • sets the AWS_PROFILE environment variable value
conda activate aws-worker

Create the env.list file

The env.list text file contains the list of environment variables (without values) used by the application. When we run the Docker container, the –env-file option will copy those into the environment of the container.


Save the list of referenced Python packages in the requirements.txt file

When you install Python packages those are available for your application. As Python is an interpreted language those packages also have to be available during runtime in the container. The pip freeze command saves the list in a text file, so the container build process can also install them.

pip freeze > requirements.txt

Create the Dockerfile

The Dockerfile contains the list of instructions to build our application into a Docker container. This generic Dockerfile can build any Python application, as the referenced Python packages are listed in the requirements.txt file. In this example our main application file is aws_worker.py

FROM python:bullseye

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# -u option turns off print buffering in Python
CMD [ "python", "-u", "./aws_worker.py" ]

Create the Makefile

In the source directory create the Makefile to automate frequently executed steps. This Makefile provides the following functionality:

  • make init ( the first and default option, so it is enough to type “make”)
    • provides instructions on how to execute the init.sh file the first time you open the terminal in this directory. (“Make” recipes execute the instructions in subprocesses, so those lines cannot update the parent environment you are calling them from. The extra dot instructs the terminal to run the init.sh script in the same process. It is same as “source” but more portable.)
  • make run
    • starts the Python application
  • make install
    • installs the referenced Python packages and saves the list in the requirements.txt file
  • make docker-build
    • using the Dockerfile, builds the application into a Docker container
  • make docker-run
    • runs the Docker container using the list of environment variables from the env.list file
	# . ./init.sh

	pip install boto3
	pip install requests
	# Save the package list in the requirements file
	pip freeze > requirements.txt

	python aws_worker.py

	docker build -t aws-worker .

	docker run -it --env-file env.list aws-worker

Leave a comment

Your email address will not be published. Required fields are marked *