How to dockerize your Python project

One of the main hurdles in sharing projects is that your code doesn't run on another person's machine. It's essential to realize that projects are unique and require their own environment to properly get executed. For this purpose, we use Docker.

Docker

Docker has a highly adept mechanism of being able to create and run containers on any machine that has Docker installed. This really helps in providing portability as well as consistency across environments.

Note: Docker enables us to package our application plus its dependencies into a standard container, called Docker image. We can build this image using a Dockerfile.

Setting up Docker
Setting up Docker

Dockerfile

To Dockerize a Python project, we typically create a Dockerfile in the root directory of the said Python project. A Dockerfile contains instructions to define a build process and the dependencies needed in a single file.

Let's create our own Dockerfile for a sample code provided at the end of our answer!

Steps to create a Dockerfile

FROM ubuntu:20.04
  • The first thing we do while making a Dockerfile is to specify the base imagea minimal operating system required for an application to use for the Docker container. In this case, we'll use the ubuntu:20.04 image as the starting point.

RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
  • Having set up a minimal sort of operating system, we now install the material specific to our application. This line uses the RUN instruction to execute commands within the container during the image build process. It updates the package list apt-get update.

Question

Why do we call apt-get update?

Show Answer
  • Since our main goal is to be able to run a Python project, we're going to install Python 3 i.e. the current latest version and pip for Python 3 (apt-get install -y python3 python3-pip).

  • The && operator allows us to run multiple commands in a single line.

  • The rm -rf /var/lib/apt/lists/* command removes the cache to reduce the image size.

Note: pip is a package installer for Python. Let's say we wanted to use NumPy in our code. We could achieve this by first installing this library using`pip install numpy`.

RUN pip3 install numpy && pip3 install matplotlib
  • Now, this is the step that will always vary from project to project. Different applications require different modules or libraries.

  • In our case, the code we've set up involves the numpy and matplotlib libraries. These lines use pip3 to install these within the container.

COPY plot.py /app/
  • Since our code file is named plot.py, we will be including it in the Dockerfile. We can copy more files, or even a folder, in our Dockerfile.

  • This line copies the plot.py file from the build contextthe directory where the Dockerfile is located to the /app/ directory inside the container.

WORKDIR /app
  • This line sets the working directory to /app/ inside the container. We do this to ensure that subsequent commands are executed relative to this directory.

CMD ["python3", "plot.py"]
  • Finally, we specify the command we would want to run once the container is started. The CMD instruction specifies the command to run when it starts. In this case, we make it execute plot.py's code using Python 3.

Dockerfile

Now that we have gone through the steps of making a Dockerfile, our final file will look like this.

FROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
RUN pip3 install numpy && pip3 install matplotlib
COPY plot.py /app/
WORKDIR /app
CMD ["python3", "plot.py"]
Dockerfile for a Python project that uses numpy and matplotlib

Running the Dockerfile

Once we're done making the Dockerfile, we simply have to build and run the image using the following commands:

docker build -t imageName
docker run imageName

After these commands are executed, the container will start and the code in plot.py will be executed in this container.

Note: For this purpose, make sure Docker is installed on your machine. For Linux, it can be downloaded here.

Code sample

The Dockerfile we made was for a Python code that made use of the matplotlib and numpy libraries.

Let's make some interesting data visuals using matplotlib while the calculations are done using numpy and see our code in action!

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(0, 10, 100)

Y1 = np.sin(X)
Y2 = np.cos(X)

FIG, AX = plt.subplots(figsize=(8, 6))
FIG.patch.set_facecolor('black')
AX.set_facecolor('black')

AX.spines['bottom'].set_color('white')
AX.spines['top'].set_color('white')
AX.spines['right'].set_color('white')
AX.spines['left'].set_color('white')
AX.tick_params(colors='white')

LINE1, = AX.plot(X, Y1, color = 'cyan', linestyle='-', linewidth = 2, label = 'sin(x)')
LINE2, = AX.plot(X, Y2, color = 'magenta', linestyle='--', linewidth=2, label='cos(x)')

AX.set_title('Plot depiction', color='white', fontsize=18)

AX.set_xlabel('X-axis', color='white', fontsize=14)
AX.set_ylabel('Y-axis', color='white', fontsize=14)

LEGEND = AX.legend(loc='upper right', frameon=False, facecolor='black')

for text in LEGEND.get_texts():
    text.set_color('white')

AX.grid(color='white', linestyle=':', linewidth=0.5)

AX.text(1, 0.8, 'Beautiful', color='white', fontsize=12)
AX.text(5, -0.8, 'Complicated', color='white', fontsize=12)

plt.show()

plt.savefig('output/myplot.png')

Output

The following plot depicts the output of the code we executed above. For such a code, a Dockerfile will usually be required to include the used dependencies.

End notes

We can say that even if some machines already have the required dependencies, Docker still offers advantages such as:

  • Isolation

  • Reproducibility

  • Portability

  • Simplified dependency management

  • Better collaboration

Therefore, it's a valuable tool for development, testing, and deployment. By following the above steps and customizing the Dockerfile according to your own project, you're ready to run your Python project through Docker!

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved