logo
logo
Sign in

How to containerize a Python application?

avatar
INEXTURE Solutions
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?

Now let’s have some basics of docker. What it is and how it can help in the containerization process of an app. 

Docker is an open development, distribution, and execution environment for distributed applications. It provides the common toolbox needed by programmers, software developers, and operations professionals to benefit from the distributed and interconnected nature of modern systems.


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.



  1. Decide which base image to utilize.
  2. Decide which files to copy into the Docker image.
  3. Install the application’s prerequisites.


Base image

The FROM command specifies a base image, which can be either a private or a public image.

We will utilize the official Python Docker image in this instance, which is accessible via the Docker hub.



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 specified files (or folder) are copied into the Docker image by this command. In this instance, we want to put all of the files from our local folder into the /src path of the Docker image.


Install dependencies



One thing to note is that requirements.txt’s path has changed since we ran the pip install for the first time.


Build and Run a Docker 


Now that our Dockerfile is finished, we can use it to create a Docker image.

In this command, a Docker image named movie-recommender is created from the current folder (at the end specify which building context we will use).



How is that even doable? Why are we unable to connect to our container-running application?


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/


collect
0
avatar
INEXTURE Solutions
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more