Docker: Configuring Apache server and setting up python interpreter inside docker container.

Yashraj Oswal
6 min readNov 22, 2020

Introduction to docker

Docker is container based open-source technology which has the capability to launch any Operating system within a seconds. What you need is to pull the image container from docker pool and run the container. In this era of faster growing technology, even a seconds of delay can cause a big loss. Talking about traditional way of booting an new Operating system, which we also address as bare-metal requires atleast 30–40 mins to bootup and get ready to use. So docker is new way of launching an OS which not only saves time but has the capability to perform just as your normal operating system

So let us first know, How to install Docker on RHEL-8 Operating system:

Step1: You need to create a new repository file with extension filename.repo inside the location /etc/yum.repo.d/

Step2: Copy paste the following inside the file.

[docker-ce]

baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/

gpgcheck = 0

Step3: Run command dnf install docker-ce — nobest -y

Step4: Start the docker services :

Command: systemctl start docker

Done..! Docker is successfully installed and ready to use..!

Let us start with our tasks..!

Task:-

🔅Configuring HTTPD Server on Docker Container

🔅Setting up Python Interpreter and running Python Code on Docker Container

*To give internet access inside docker container..!*

We need to first change some firewall rules to provide internet access inside docker container, for that run following commands on your base OS

Now that we have done with providing internet access, lets start..!

Configuring HTTPD Server on Docker Container :

Check whether docker services are running or not..

Command: systemctl status docker

Let us pull an docker image:

Command: docker pull centos:7

If you didn't specify the version of image, it by default downloads the latest version.

To check available/downloaded images :

Command: docker images

Let us run an docker container and install apache server on it:

Command: docker run -i -t — name httpd-config centos:7

where -i -t in the command prompts the docker to provide an interactive terminal. You can provide any name to the container of your choice, or docker can also set it by default unique name for the container. And as you can observe in the image, as soon as I run the command, the name changes from root@localhost to root@docker container-id.

You can check the running docker container, open an new terminal and run the Command: docker ps

Installing httpd server:

Command: yum install httpd

Start httpd services:

As you can see, we caught-up with an error, in the docker centos container, systemctl command doesn’t work, so to start the httpd services, we need to go to the location and start it manually,

Command: /usr/sbin/httpd

Above location provides httpd services, so just running above command we can start the httpd services.

To check whether the httpd services has started or not:

Command: netstat -tnlp

Some user’s may caught-up with this error, because some of the packages are not pre-installed inside docker container, we can also install it manually, but first lets check whether ifconfig command is working or not

No, ifconfig is also not working.

To install the package, we need to know which package provides above programs, for that you can use yum whatprovides [filename], from here you will get file name, install it. net-tools package provides netstat and ifconfig program

Command: yum install net-tools

After installation you can check by running commands ifconfig, netstat

Command: netstat -tnlp shows that server has been started, and now we can create an html page and check it from our base OS browser.

Creation of Webpage in the apache server folder:

You need to go to the server’s active directory Path: cd /var/www/html/ and create .html extension file.

So we have created the webpage, and now we will test it on the base operating system browser,

To check on browser you need to use url: http://172.17.0.3/test.html

Use the ip address of docker instance and file name created in the docker server’s active directory, it will display the webpage we created as shown above.

Here we have successfully configured HTTPD Server inside Docker container..!

Now let us start with our next section, that is Setting up python interpreter inside docker instance.

Install python3 inside docker container:

Command: yum install python3

It will install following packages for python3.

Create an simple code, and run on docker instance:

As you can see we have created a .py file and using python3 filename.py, we have executed the code.

Now that we have configured apache server inside the container, let try to execute python code on base OS browser using API call, and common gateway interface (cgi-bin).

Goto path: cd /var/www/cgi-bin/

Now, we copied the same command.py file into our apache server’s cgi-bin folder, as we are going to run the linux command on web- browser, os.system() doesn’t work there, so we are making use of python module named subprocess. In the code we have added shebang, which is #!/usr/bin/python3 and added a line print(“content type: text/html”) which will help browser to display the formatted output. Next step is to make is executable, so that it can run on browser, and show us the o/p of the code.

Let us test the same on browser..!

URL : http://172.17.0.3/cgi-bin/command.py

As you can see we have successfully installed python3 interpreter inside docker container, and has executed in both ways i.e inside docker container itself, and using apache server via API call on base Operating system’s browser as well…!

Here we have successfully completed both the tasks..!

--

--