How to containerize a Python application?
Is it challenging to deploy to production, discuss your Python code with coworkers, or compile it from a source? The best technology to handle this for your Python project is Docker. It is one of the most widely used containerization technologies used by Python developers. . It enables the packaging of a program along with each of its dependencies into a solitary, small-footprint container. Containerization is the name of this procedure.
Why you should containerize your app
Now the important question arises why you should containerize your app? Well, it has a simple logic let’s understand.
A minor update in an external library’s version can alter your application’s functionality and cause it to act differently. Because of this, containerizing a program enables it to run consistently regardless of the workspace or device that it is installed on.
What Exactly Is Docker?
Simply said, Docker enables you to run your programs inside a container—a regulated environment created according to your specifications. Similar to a regular virtual machine, a container makes use of the resources of your computer (VM). Furthermore, in terms of system resources, containers are very different from conventional virtual machines.
How to containerize a Python application
Making a Docker image with the source code, dependencies, and configuration which is necessary to run a Python program is known as containerizing it. It’s a process.
Decide which base image to utilize.
Decide which files to copy into the Docker image.
Install the application’s prerequisites.
Base image
The FROM command specifies a base image, which can be either a private or a public image.
The image name consists of two distinct components: picture: tag. In this instance, the tag is 3.8.5-slim-buster and the picture is python.
Copy the Application’s file
We’ll use the COPY command to copy the application inside the Docker image:
The first component of the COPY command is a route relative to the created context, not to our local machine, which is an important distinction to make.
Install dependencies
Installing our dependencies within the Docker image is the final step. We’ll utilize the RUN command to RUN pip install in order to accomplish that.
The duplicated files are located inside the image’s /src path, which is the cause of the issue.
Build and Run a Docker
Now that our Dockerfile is finished, we can use it to create a Docker image. We have to employ the docker build command for this.
Now, Using the docker run command, we can run the image we just built:
This command will launch a container based on the movie-recommender image and run the Python script /src/app.py inside of it.
We failed to make our application’s port accessible to the local machine, which is the cause.
Using the -p HostPort: ContainerPort flag, we can accomplish that.
Therefore, let’s try running the program once more while indicating that we wish to locally expose port 8888:
Now the curl localhost:8888
Just now, a Python application is containerized.
Wrapping Up
In this blog, we have learned how you can containerize your python applications in simple steps.
Originally published by: https://www.inexture.com/containerize-python-application-using-docker/












