To run Python interpreter/code over the Docker container

This blog is continuation to previous blog, how to setup the HTTPD server on the docker container, but this time we will create a docker container that can run the Python Code.

This blog is part of the Task 7.2(b) given by Vimal Daga sir in class, and problem statement is as follows:

ARTH — Task 7 👨🏻‍💻

Task Description 📄

Setting up Python Interpreter and running Python Code on Docker Container

Use-Case of Python enabled container that has a python interpreter and can run the code, is to run any python code like ML models and all other .py files. The container is a go to for testing any python code. Lets see how to make and at the end I will attach Dockerfile and Docker Hub image so that you can try yourself.

I am using my regular RHEL 8 instance, launched over the VMware with the docker installed over it.

Let’s go with the process of building the desired container image:

Step1: Run docker ps to check if the docker is running or not.

If you get the table as in the Image above, Docker is running successfully.

Step2: I am using the Base Image as CentOS so, lets launch a CentOS container, I am giving the random name say pycontainer you can give anything.

# docker run -it --name pycontainer centos

This will launch the container and rest of the commands we have to give inside the Container.

Step3: Now we are going to configure the python interpreter. So using the the YUM the package manager for the CentOS, Lets install python3 and pip3 and a CLI text editor vim

# yum install python3 python3-pip vim -y

This command will install the required software for the image. And we are done. now create a file using vim or run live interpreter.

Demo 1: Running the live python interpreter:

Demo 2: running the python file

Want to try my docker container, click here

Now, a question comes, can we launch the python interpreter without going inside the image? Answer is yes we can, but first lets commit the image so that we can use in future.

To commit the image, open the new terminal, do not exit the image running. Give the commands to commit (I am creating the commit for the dockerhub)

# docker commit pycontainer centos-py

This command will form the image that we can see inside the docker images

Now, answer to the question: Docker provides the command line feature that we can use.

# docker run -it --name ospy1 centos-py python3 

And a python terminal comes up, i.e. a live python interpreter. This is what we need.

To check that it is inside the container, hostname should vary, and the VM has hostname localhost.localdomain while it show a alpha-numeric ID that means it is inside the container.

But you will say, isn’t the thing you are telling is too manual? Is there a other way of this which is less manual or a Dockerfile that can do it?

My answer is yes, and I am going to tell this next, heading to Dockerfile method:

First one is the Dockerfile for launching the live python interpreter:

For setup and more details: CLICK HERE

Second one is running a python file: (NOTE: Life of container == Life of process i.e. the container will terminate after showing the output.)

For setup and more details: CLICK HERE

Thanks for reading, Hope to see you all in the next blog!

Aspiring MLOPS engineer with Multi-cloud and Flutter/MERN