Configuring HTTPD Server & Python Interpreter on Docker Container

Akhil Sukhnani
5 min readMar 15, 2021

--

Hola readers! Welcome back to another article where we’ll be learning to create a container environment that has python and an HTTP webserver working on it, so without any further ados let's get started.

In the current setup, I’ve used Docker as a Container Engine(used to launch containers) so before moving ahead I would like to brief you about Docker.

Docker is a popular open-source project written in go and developed by Dot cloud (A PaaS Company). It is basically a container engine that uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system.

And according to Wikipedia “Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels”.

Now let’s get started with our agenda, only prerequisite for setting up the following environment is an Operating System with active internet connectivity.

Step:-1 Install Docker on your base OS, which in my case in RHEL8

First of all, I’ll check if the docker software is available in the pre-configured repository.

By which we came to know that it is not available by default, so we have to create a custom yum repository for Docker.

/etc/yum/repos.d

This is the default path for yum repositories and here we’ll create a repo by any name like this:-

[docker]
baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck=0

After this, we’ll install the software using the yum or dnf command.

yum install docker-ce --nobest -y

Docker will get installed after it and we can verify using the following command:-

rpm -q docker-ce

Step2:-> Starting the docker services and launching the container

systemctl start docker

This command will start the docker services on your system

systemctl enable docker

And this command will make the docker service permanent on our system even after a reboot.

systemctl status docker

This command will tell the status of docker services on the respective system.

Now it's time to launch the container, but to launch any container we need a docker image for it which can be found on DockerHub and a customized image can also be created locally.

To download any image from docker we use the docker pull command like this

Syntax:-> docker pull <image_name>:<version>docker pull centos:7

To view all the images that are downloaded locally use the command:-

docker images

So our centos image is downloaded and now we are ready to launch the container.

 Syntax:-> docker run -it --name containername imagesname:imageversion

And after running this command you will land inside the container, where we need to set up a webserver and python interpreter.

To setup Web server in any environment you need to follow these basic steps:-

  1. Install the software
yum install httpd -y

This will install an apache webserver inside the container

2. Configure

cd /var/www/htmlcat > index.html
<Some text here>

This will create a demo page for the webserver

3.Start the services

/usr/sbin/httpd

This command will start the services for httpd inside the container because we cant use the systemctl command inside the container.

After performing these simple steps our web server inside the container would b be up and running, which can be verified again by the curl command from the baseOS

So goto baseOS and run the command

curl <ip_of_container>/index.html

And now we need to set up the python environment for which you can either use the same container or can use the same container as well, it depends upon the use case.

docker run -it --name pythonos  centos:7

After this, we have to install the python interpreter using the yum command

yum install python3 -y

python3 will get installed on the container after running the above command

python3

It will open the live-interpreter of python on the container.

[root@5472bda675eb ~]# vi demo.py
[root@5472bda675eb ~]# cat demo.py

Creating a demo testing python code file.

And now we can run this program file by using the command:-

python3 <location of the file>

The program ran successfully…!

Conclusion

Created and tested the above-mentioned environment successfully and I hope, you enjoyed this article and learned something new from this, feel free to connect with me on LinkedIn and comment your queries here, stay tuned for more interesting articles…!

--

--