Image by author
When building applications with Python, you will often encounter dependency conflicts, version mismatches, and the like. With Docker, you can package applications (along with the necessary dependencies, runtime, and configuration) into a single portable artifact called an image. Which you can then use to spin up a Docker container that runs the application.
So whether it is a simple Python application or a data science application, Docker simplifies dependency management. This is especially useful in data science projects where you need different libraries and specific versions of these libraries to make your application 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 contain 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 might want to do after you've installed Docker on your machine.
The Docker daemon binds to a Unix socket, owned by the root
default user. So you can access it only using sudo
. To avoid prepending all docker commands with sudo
create a docker
group adds a user to the group like this:
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
For newer versions of Docker, BuildKit is the default builder. However, if you are using an older version of Docker, you may receive deprecation warnings when you run the docker build
domain. This is because the legacy build client will be deprecated in future releases. As a workaround, you can install buildx, a CLI tool for using BuildKit capabilities. and use the docker buildx build
command to build with BuildKit.
Step 2: Code your Python application
Next, code a Python application that we can contain using Docker. Here we will contain a simple Command line to-do list app. The code of this application is on GitHub: todo.py file.
It can contain any Python application of your choice or follow the example we use here. If you are interested in a step-by-step tutorial on how to create the command-line TO-DO application, read Create a command-line application with Python in 7 easy steps.
Step 3: Create the Dockerfile
Next, we will create a Dockerfile. Think of it as a recipe that defines how to build the Docker image for the application. Create a file called 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 following instructions with the WORKDIR
domain. Then we use the COPY
command to copy files from the project to the container file system.
Because we are containerizing a command line application, we specify the command to run as “/bin/bash”
. Which starts an interactive bash shell when we run the image and start a container.
Step 4 – Create the Docker image
We now have our todo.py and Dockerfile file 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 label like this: docker build -t name:tag .
This command creates a Docker image called todo-app
based on the instructions of the Dockerfile
. He .
at the end you specify 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 created, you can start a Docker container from the created image with the following command:
He -it
The option is a combination of -i
and -t
:
- He
-i
The option is used to run containers interactively and keeps STDIN open even if you are not connected. - He
-t
The option assigns a pseudo-TTY. Therefore it provides a terminal interface inside the container that you can interact with.
Now, our TO-DO application runs inside the Docker container and we can interact with it on 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
Ending
And there you have it! You have successfully containerized a command-line Python application using Docker. In this tutorial, we look at containerizing a simple Python application using Docker.
We built this application in Python without using external Python libraries. So we don't define a requirements.txt file. The require.txt file usually lists the various libraries and their versions, which you can install using a simple pip install
domain. If you want a tutorial that focuses on Docker for data science, check out the Docker Tutorial for Data Scientists.
twitter.com/balawc27″ rel=”noopener”>Bala Priya C. is a developer and technical writer from India. He enjoys working at the intersection of mathematics, programming, data science, and content creation. His areas of interest and expertise include DevOps, data science, and natural language processing. He likes to read, write, code and drink coffee! Currently, he is working to learn and share his knowledge with the developer community by creating tutorials, how-to guides, opinion pieces, and more. Bala also creates engaging resource descriptions and coding tutorials.
<script async src="//platform.twitter.com/widgets.js” charset=”utf-8″>