Run Python in Docker: Hosting a Python Script on Docker With Examples

Hello readers! In this article, we will be Understanding the process of Hosting a Python script on a Docker Container in detail. So, let us begin!


What is a Container?

A Container is a package of all the system and software requirements that are needed to run an application on the cloud. It comprises the run-time execution files, system settings, code, and libraries.

Thus, with containers, we can modify the specifications as containers are writable and then host the applications easily on them.


Hosting a Python script on Docker

Having understood about containers, it is now the time to implement a script through containers. By this, we will ensure the container takes care of all the requirements and hosting.

Before deploying the script, let us understand the flow of hosting an application on a container in Docker.

Recommended Read: How to install Docker on Ubuntu?

  1. At first, we need to create a Dockerfile. A Dockerfile is a blueprint that is necessary to create images in Docker. It contains all the necessary commands which we would usually execute through the command line to create an image for the container.
  2. Having created a Dockerfile, we would now require to build an image on top of the Dockerfile. A Docker image can be considered as a template that helps create a container in docker. It packs up the applications and sets the server environment, thus making it easy for us to use the environment for hosting the applications over containers in a convenient manner.
  3. Now, we can run the image to see the output of the python script over. As soon as the image runs, a container with a random name gets created.

Now, let us start with the implementation!! We would be using the below Python script to host it on the docker container.

sample.py

lst = list()
lst = ['Python', 'Machine Learning', 'R Language', 'Bootstrap']
for x in lst:
print(x)

In the above script, we have created a list and then iterated through a for loop to print the elements of the Python list.


1. Create a Dockerfile

Dockerfile:

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

There are certain directives offered by Dockerfile as shown below–

  • FROM – This directive sets the base image for the subsequent instructions to work on. In this example, we have set python version 3 as the base image. Now, the Dockerfile would fetch this base image from the Docker Hub which is actually a repository of open-source images.
  • ADD – The ADD instruction copies new files, directories, or remote file URLs from <src> and adds them to the filesystem of the image at the path <destination>. In our case, the src = sample.py, and the destination is /
  • CMD – This directive runs the services alongside the base image in a customized manner.

2. Building an image from the Dockerfile

After building the Dockerfile, we now need to build the image on top of the Dockerfile using the below command–

docker build -t image-name:tag .

We can provide any customized image name to it and the tag helps it segregate from other images on the hub.

Example:

docker build -t python-img:5.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 sample.py /                                                                                 0.0s
 => exporting to image                                                                                           0.1s
 => => exporting layers                                                                                          0.0s
 => => writing image sha256:8b2da808b361bc5112e2afa087b9eb4e305304bcc53c18925d04fe8003f92975                     0.0s
 => => naming to docker.io/library/python-img:5.0

We need to remember that docker images are read only structures and can run independently. On the other side, a container is built on top of an image and it needs an image to run itself.


3. Run the docker image

Let us now run our created image to see the python script’s output from the container on the GIT BASH console.

docker run python-img:5.0

Output:

Python
Machine learning
R language
Bootstrap

We can also see the reflection of the image running on the Docker Community Edition Console as shown below–

Docker_Image

Docker Image

As mentioned in the previous section, a container with a random name gets created for the called image as seen below:

Docker_Container

Docker Container

We can even create containers with customized names and run the script through the container using the below command:

docker run -it --name container-name image:tag

Example:

docker run -it --name sample python-img:5.0

Output:

Python
Machine learning
R language
Bootstrap

Docker_customized_Container

As seen above, we see two containers created over the same image that we had created earlier.


Conclusion

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

For more such posts related to Python, stay tuned and till then, Happy Learning!! 🙂


References

By admin

Leave a Reply

%d bloggers like this: