How to Containerize a Python Application using Docker?
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.
- There are many dependencies needed for the project.
- The project needs an outdated compiler or certain outdated library versions. Once you install it, further projects could start to fail.
- Even when you use Windows or a Mac, the software was created to be built and used on Linux.
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?
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.
Our application needs to be containerized, so the first step is to generate a new text file called Dockerfile:
- Decide which base image to utilize.
- Decide which files to copy into the Docker image.
- Install the application’s prerequisites.
Base image
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.
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.
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.
Build and Run a Docker
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:
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/
Comments
Post a Comment