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.
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
to use for the Docker container. In this case, we'll use thebase image a minimal operating system required for an application ubuntu:20.04image 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
RUNinstruction to execute commands within the container during the image build process. It updates the package listapt-get update.
Why do we call apt-get update?
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
numpyandmatplotliblibraries. These lines usepip3to 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.pyfile from the to thebuild context the directory where the Dockerfile is located /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
CMDinstruction 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.04RUN apt-get update && apt-get install -y \python3 \python3-pip \&& rm -rf /var/lib/apt/lists/*RUN pip3 install numpy && pip3 install matplotlibCOPY plot.py /app/WORKDIR /appCMD ["python3", "plot.py"]
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 imageNamedocker 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