| by Arround The Web | No comments

How to Dockerize a Python Application

Docker containers are small executable packages of Docker forums that are being used to containerize the application. Docker containers use OS-level virtualization and system resources. These containers pack the application source code and all essential dependencies in one unit. This feature enables the users to share and deploy the application or project on another system easily.

This write-up will demonstrate how to dockerize a python application.

How to Dockerize a Python Application?

Docker containers are usually utilized to dockerize any application in Docker. More specifically, to Dockerize or Containerize the Python application in Docker, utilize the listed steps.

Step 1: Make Dockerfile

Dockerfile is a building block of Docker to containerize any application by creating a Docker image. Therefore, to containerize the Python program, first, create a Dockerfile and specify the following instructions in the file:

FROM python
WORKDIR /src/app
COPY . .
CMD [ "python", "./pythonapp.py" ]

According to the above snippet:

  • FROM” is utilized to define the container base image. For instance, we have used the “python” image as a base image.
  • WORKDIR” specifies the container’s working directory.
  • COPY” is used to paste the source file into the container path.
  • CMD” is specifying the executables of the container. For instance, we have specified the “pythonapp.py” as a container executable.

Step 2: Create Program File

Next, create a Python program file “pythonapp.py”:

Paste the following one-line Python code into the “pythonapp.py” file:

print("Hello, I am building first Python application")

Step 3: Launch Terminal

After that, launch the terminal in the Visual Studio editor as shown below:

Step 4: Build Docker Image

Generate the Docker image that will be utilized for creating the container for the Python program. For this purpose, use the given command. This command will read the Dockerfile and generate the Docker image:

> docker build -t python-image .

Here, the “-t” option is utilized to define the image name or image tag:

Step 5: Build and Run Container

Next, containerize the Python application by creating the container using the docker image. The “–name” flag defines the container’s name in which the Python program will execute:

> docker run --name python-container python-image

Here, you can see we have successfully containerized and deployed the Python application:

To view the Python container, list down all Docker containers using “docker ps” command:

> docker ps -a

This is all about how to containerize the Python application.

Conclusion

To containerize the Python application or to build the container from python, first create a Dockerfile that instructs the daemon how to create the image. Then, generate the image using the “docker build -t <image>” command. After that, run the newly generated image to deploy and containerize the application. This write-up has demonstrated how to dockerize or containerize the python application.

Share Button

Source: linuxhint.com

Leave a Reply