AI Development Simplified: The Power...
December 26, 2024
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.
Running your program in production might likewise be challenging for similar reasons! To deal with these challenges it’s better to go with containerization.
It’s helpful to know that there is a simple solution if you are experiencing these issues. Instead of relying on virtualization, it makes use of the containerization principle. To put it simply, containerization is a much lighter version of virtualization. We’ll be containerizing a sample Python project using Docker since it’s the de facto industry standard for containerization.
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.
The best thing about Docker is that you can get surety that your application will run at the same speed on both devices if you containerize it and transmit the image to your colleague’s PC. This is because all of the dependencies for the application are included in the container.
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.
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.
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.
Our application needs to be containerized, so the first step is to generate a new text file called Dockerfile:
Three steps must be specified in the Dockerfile (for what we consider to be a minimally viable Dockerfile):
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 most recent Python 3 image based on Ubuntu will be chosen.
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.
Everything is required for the first action.
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.
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.
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.
One thing to note is that requirements.txt’s path has changed since we ran the pip install for the first time.
The duplicated files are located inside the image’s /src path, which is the cause of the issue.
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.
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).
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.
However, if we use curl to attempt a connection to our application at localhost:8888, we will encounter a problem.
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.
In this blog, we have learned how you can containerize your python applications in simple steps. A python development company like ours can help you from start to end. Feel free to contact us anytime.