Skip to content
Pinter Computing

Knowledge Base for IT Professionals, Teachers, and Astronauts

Pinter Computing

Knowledge Base for IT Professionals, Teachers, and Astronauts

  • Home
  • Programming
  • DevOps
  • Project Management
  • Software and Hardware
  • Miscellaneous
  • Egyebek
  • About
  • Experience
  • Education
  • Contact
  • Home
  • Programming
  • DevOps
  • Project Management
  • Software and Hardware
  • Miscellaneous
  • Egyebek
  • About
  • Experience
  • Education
  • Contact
Close

Search

Home/Knowledge Base/Migrate a Python application into a Docker container
Knowledge Base

Migrate a Python application into a Docker container

By Laszlo Pinter
July 5, 2022 3 Min Read
0

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

region_name=get_config_value("AWS_REGION")

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
AWS_PROFILE=aws1

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.

MY_VAR1
MY_VAR2

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:
	# TO INITIALIZE THE ENVIRONMENT EXECUTE
	# . ./init.sh

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

run:
	python aws_worker.py

docker-build:
	docker build -t aws-worker .

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

Tags:

DevOpsPythonSoftwareDevelopment
Author

Laszlo Pinter

Follow Me
Other Articles
Previous

Migrate your application into a Docker container

Next

Migrate a Node.js application into a Docker container

No Comment! Be the first one.

Leave a Reply Cancel reply

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

Search

Last Changes

  • DevOps Engineering part 1. (Mac) - Make your Macintosh easier to use June 25, 2026
  • Japan travel tips June 22, 2026
  • How to stop the rain and snow in Cities: Skylines II June 20, 2026
  • Cities: Skylines II Developer Mode June 20, 2026
  • 'CSII_MANAGEDPATH' has incorrect path(s) when building Cities: Skylines II mod June 20, 2026

Tags

.NET .NETcore 3Dprinting ASP.NET Core AutodeskInventor AWS C# Chef cloud DevOps Docker EntityFramework Games Git Go iOS iPad iPhone iPod Java Kubernetes Linux MacOSX MSSQL MVC Node.js Packer PowerShell Python RDS RightScale Ruby security Splunk TeamCity Terraform TestKitchen Tomcat Ubuntu Vagrant VirtualBox VisualStudio Windows WordPress Xcode

Recent Comments

  • Zengei László on MyHeritage családfa exportálása és küldése emailben
  • Raúl Castillo on DynDns update error
  • MICHAEL on Windows Media Player 12 cannot find the album information
  • Nargis on Configure Epson ET-3850 scanning on Windows 11
  • Venczelné Zemen Erika on Delta S2302 termosztát programozása

–

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Copyright 2026 — Pinter Computing. All rights reserved.