Image by Author
When building applications with Python, you’ll often run into dependency conflicts, version mismatches, and the like. With Docker, you can package applications—along with the required dependencies, runtime, and config—into a single portable artifact called the image. Which you can then use to spin up a Docker container that runs the app.
So whether it is a simple Python application or a data science application, Docker makes managing dependencies simpler. This is especially helpful in data science projects where you need different libraries and specific versions of these libraries for your application to work without errors. With Docker you can have isolated, consistent, and reproducible environments for all your applications.
As a first step in this direction, let’s learn how to containerize a Python application.
Step 1: Get Started
First, install Docker on the platform you use. You can run Docker on Windows, Linux, and MacOs. Here are a couple of things you may want to do after you’ve installed Docker on your machine.
The Docker daemon binds to a Unix socket, owned by the root
user by default. So you can access it only using sudo
. To avoid prefixing all your docker commands with sudo
, create a docker
group add a user to the group like so:
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
For newer versions of Docker, BuildKit is the default builder. If you’re using an older version of Docker, however, you may get deprecation warnings when you run the docker build
command. This is because the legacy build client will be deprecated in future releases. As a workaround, you can install buildx, a CLI tool to use BuildKit’s capabilities. And use the docker buildx build
command to build with BuildKit.
Step 2: Code Your Python Application
Next, code a Python application which we can containerize using Docker. Here we’ll containerize a simple command-line TO-DO list app. The code for this app is on GitHub: todo.py file.
You can containerize any Python app of your choice or follow along with the example we use here. If you’re interested in a step-by-step tutorial on building the command-line TO-DO application, read Build a Command-Line App with Python in 7 Easy Steps.
Step 3: Create the Dockerfile
Next, we’ll create a Dockerfile. Think of it as a recipe that defines how to build the Docker image for the application. Create a file named Dockerfile
in your working directory with the following:
# Use Python 3.11 as base image
FROM python:3.11-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Command to run the Python script
CMD ["/bin/bash"]
Here, we use Python 3.11 as the base image. We then set the working directory for all the following instructions with the WORKDIR
command. We then use the COPY
command to copy files from the project into the container’s file system.
Because we’re containerizing a command-line app, we specify the command to execute as “/bin/bash”
. Which starts an interactive bash shell when we run the image and start a container.
Step 4: Build the Docker Image
We have our todo.py file and Dockerfile ready. Next, we can build the Docker image with the following command:
docker build -t todo-app .
With the -t
option in the build command, you can specify both a name and a tag like so: docker build -t name:tag .
This command builds a Docker image named todo-app
based on the instructions in the Dockerfile
. The .
at the end specifies that the build context is the current directory.
The build takes a couple of minutes:
Sending build context to Docker daemon 4.096kB
Step 1/4 : FROM python:3.11-slim
3.11-slim: Pulling from library/python
13808c22b207: Pull complete
6c9a484475c1: Pull complete
b45f078996b5: Pull complete
16dd65a710d2: Pull complete
fc35a8622e8e: Pull complete
Digest: sha256:dad770592ab3582ab2dabcf0e18a863df9d86bd9d23efcfa614110ce49ac20e4
Status: Downloaded newer image for python:3.11-slim
---> c516402fec78
Step 2/4 : WORKDIR /app
---> Running in 27d02ba3a48d
Removing intermediate container 27d02ba3a48d
---> 7747abda0fc0
Step 3/4 : COPY . /app
---> fd5cb75a0529
Step 4/4 : CMD ["/bin/bash"]
---> Running in ef704c22cd3f
Removing intermediate container ef704c22cd3f
---> b41986b633e6
Successfully built b41986b633e6
Successfully tagged todo-app:latest
Step 5: Run Your Docker Container
Once the image is built, you can start a Docker container from the built image with the following command:
The -it
option is a combination of -i
and -t
:
- The
-i
option is used to run containers interactively and keeps STDIN open even if not attached. - The
-t
option allocates a pseudo-TTY. So it provides a terminal interface within the container that you can interact with.
Now, our TO-DO app runs inside the Docker container, and we can interact with it at the command line:
root@9d85c09f01ec:/app# python3 todo.py
usage: todo.py [-h] [-a] [-l] [-r]
Command-line Todo List App
options:
-h, --help show this help message and exit
-a , --add Add a new task
-l, --list List all tasks
-r , --remove Remove a task by index
root@9d85c09f01ec:/app# python3 todo.py -a 'walk 2 miles'
root@9d85c09f01ec:/app# python3 todo.py -l
1. walk 2 miles
Wrapping Up
And there you have it! You’ve successfully containerized a command-line Python application using Docker. In this tutorial, we looked at containerizing a simple Python application using Docker.
We built this application in Python without using any external Python libraries. So we did not define a requirements.txt file. The requirements.txt file usually lists the various libraries and their versions, which you can install using a simple pip install
command. If you want a tutorial that focuses on Docker for data science, check out Docker Tutorial for Data Scientists.
Bala Priya C is a developer and technical writer from India. She likes working at the intersection of math, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, coding, and coffee! Currently, she’s working on learning and sharing her knowledge with the developer community by authoring tutorials, how-to guides, opinion pieces, and more. Bala also creates engaging resource overviews and coding tutorials.