Python on Docker: How to Host a Python Application in a Docker Container?

Hosting A Python Application On A Docker Container

Hey! Let’s get into how you can host Python on Docker. By the end of this tutorial, you’ll know how to create a docker container, add your app to the container, and keep it running without interfering with your system processes.

If you’re a beginner in Python, go through this Python video course by following through the entire course map.

Once you’re all set, you’re ready to get started!


What is a Docker Container?

Before understanding the concept of containers, let us discuss and understand the emergence of the same.

Back in the days when VM was the show of the town, all the applications were hosted on VMs. The application owners had to pay a huge cost for the resources that were underutilized but blocked by them for the traffic that had a way to the application. This did not turn to sustain for long as it was not pocket-friendly. Moreover, all the app-related configuration files needed to be stored on that single VM.

This is when Containers came into the picture.

With Containers, came the concept of pay for what you use.

Basically, Docker introduced us to the concept of containers. Containers pack up the application and all its related configuration files into a single isolated environment. It folds all the system-related software requirements, code, and libraries into a separate space altogether.

With Containers, we only pay for the resources that we utilize during the run-time of the application. That is, we won’t need to block the resources as a container scales the resource usage with your application.

Having understood about containers, let us now try to host a simple Python application on a Docker Container.

Steps to Run Python on Docker

This entire process of hosting applications into containers runs through the below scenarios–

  1. Creation of a Dockerfile
  2. Building an Image
  3. Running the application as a container

Step 1 – Create a Dockerfile

A Dockerfile is the actual blueprint of the configuration needs with regards to the application that is planned to be hosted. We can include all the system-level commands that need to be executed to have your final application blueprint ready for processing. It may include some addons or libraries needed by the application.

Also, we will be running the below simple python application as a container–

demo.py

data = list()
data = ['JournalDev','AskPython']
for x in lst:
print(x)

Dockerfile–

FROM python:3
ADD demo.py /
CMD [ "python", "./demo.py" ]

There are few directives offered by Dockerfile as shown below–

  • FROM – It sets the base image for the upcoming instructions to work on. For this example, the Python version has been set to 3 as the base image layer. Dockerfile when executes the instruction fetches base image from the Docker Hub that is a repository of open-source images.
  • ADD – With the ADD directive, we can inculcate instructions in the form of files, directories or even URL to the base dockerfile.
  • CMD – This directive enables us to declare a position for us to have a command to run the particular python or any bash/sh scripts.

Step 2 – Build a Docker Image

Having built the Dockerfile, it is important for us to have a base ready out for production. That is, we need to create a base layer out of all the commands defined in the Dockerfile to have a container running and performing well. For the same, we create a Docker Image using the below format of the command.

Syntax–

docker build -t image-name:tag .

Example–

docker build -t python-image:1.0 .

Output–

[+] Building 5.4s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                                             0.1s 
 => => transferring dockerfile: 31B                                                                              0.0s 
 => [internal] load .dockerignore                                                                                0.1s 
 => => transferring context: 2B                                                                                  0.0s 
 => [internal] load metadata for docker.io/library/python:3                                                      5.0s 
 => [internal] load build context                                                                                0.1s 
 => => transferring context: 31B                                                                                 0.0s 
 => [1/2] FROM docker.io/library/python:[email protected]:b6a9702c4b2f9ceeff807557a63a710ad49ce737ed85c46174a059a299b580  0.0s 
 => CACHED [2/2] ADD demo.py /                                                                                 0.0s 
 => exporting to image                                                                                           0.1s 
 => => exporting layers                                                                                          0.0s 
 => => writing image sha256:8b2da808b361bc5112e2afa087b9eb4e305304bcc53c18925d04fe8003f92975                     0.0s 
 => => naming to docker.io/library/python-image:1.0  

Step 3 – Run the Image (Have the application hosted in container)

These images (created above) are actually independent of any underlying configuration. On the other hand, containers need an image as the base configuration to run the instance of your application. For the same, we need to ignite the process of running the container through which our instance of an application would run.

We make use of the docker run command to actually have a container up and running over the configurations mentioned in the Dockerfile.

Example:

docker run python-image:1.0

Output:

JournalDev 
AskPython

We can even check for the presence of this running container on the desktop by installing the Docker Community Edition application as shown below ( for sample purposes):

Docker Community Edition
Docker Community Edition

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 🙂