How to run a Web Server and Python Interpreter in the Docker Container

Shivam Upadhyay
6 min readJun 2, 2021

Hello everyone today I would like to tell you how to configure a web server (mainly HTTPD of Apache) and the python interpreter inside a container in docker. So lets start…..

What is a Web Server?

So many of you might think that what is a web server or what is a server. Some people think that server is a computer and if it so then why is it called a server instead of a computer or system….these are some of the confusions that students generally have, so let me clarify for you. A server is a program that is installed on your computer system that helps people from outside to make changes or do some work on your computer and the program that helps you to publish any web page or web app on the internet so that people around the web might see it is called a Web Server and whatever they do on that web page is done by that server program on your computer. For eg., If anyone accessing your web page (dynamic) writes some information then this information is stored inside your computer by the webserver. So this was an overview of the web server in Layman’s words.

What is Docker?

Many of you might have heard about docker but not had a clear idea about it. So Docker is a tool which is used to launch light weight OS (so called containers) in just 1 or 2 sec. meaning using docker you can install, boot and launch the whole OS in just 1 or 2 sec. Isn’t it amazing!! It works upon containerization technology in which docker tool is installed upon the base Operating System like Windows, Linux, etc. and using it we can launch multiple OS and use them using their CLI(command line interface). It not only minimizes compute like RAM taken by the new OS but also makes the work faster. So this was an overview of Docker .

What is Python?

You all must have heard about the python programming language which is considered as one of the most used and strong language for its use in various fields and of course easy to learn feature with less syntax difficulties and all. So to run python language and do coding stuff in it we need a python interpreter that interprets the language so that using python we can interact with our OS and let it perform various stuffs in our OS. I would not go in depth about python coding but this was an overview that to run python code we need a python interpreter.

Now what I would do in this demo?

I would basically launch a new docker container on my Linux OS (which would act as my Base OS) and would install the Apache web server and the python interpreter to run any python code on my docker container. So lets get started….

So as you can see below this is my Linux terminal!!

Here docker version 18.06 is already installed in it. (If someone wants to install docker then they can use yum command : “yum install docker-ce”)

Now you can check if the docker services are running or not by using the following command:

systemctl status docker

As you all can see my docker services are already running. If someone’s docker services are stopped they can run the following command to start the services.

systemctl start docker

Now you can check the docker images by running the following command:

docker images

As you all can see that I have Centos 7,8 and ubuntu OS images so now I will launch the container for latest version of Centos by this command:

docker run -it centos:latest
The highlighted text is the terminal of Centos OS

As you can all see I have got the terminal of Centos latest version i.e I am inside the Centos docker container. Now I will install the Apache webserver i.e httpd inside this container using yum command as shown:

yum install httpd

You all can check the if the httpd server is installed or not by running the following command:

yum list httpd
The command shows that httpd is already installed

Now we will go to the “/var/www/html” folder and will create a html file with the help of vi editor as shown:

cd /var/www/html
vi shivam.html

After opening the file with vi editor we can write the content that we want to showcase on our web page as shown:

Now after saving the file we have to start our web server but as the systemctl command is not supported in docker container so we will directly go to the location of the program and run it as show:

/usr/sbin/httpd

As you all can see that the service has been started with ip 172.17.0.2 with the warning. Now we can check if our web page is displayed on our web browser. For that we have to type the ip along with page name in the new tab of our browser as shown:

http://172.17.0.2/shivam.html

So the configuration of the web server has been done now we have to install the Python interpreter . For this we can use the yum command as shown:

yum install python3
Python Interpreter is installing

After it is installed you can use live interpreter(like REPL) to run any code you want as shown or can also create any python script file and run it.

print code written in REPL(live interpreter)

So that was all the demo. Thank you all of you!! :)

--

--