Setting up HTTPD server over a Docker container

Rishi Agrawal
5 min readMar 23, 2021

This blog is about how we can configure docker container image with the apache webserver. I will be showing first the manual way then the automated way.

This is the part of the task 7.2(a) given by the Vimal Sir in class, the problem statement is as follows:

ARTH — Task 7 👨🏻‍💻

Task Description 📄

Configuring HTTPD Server on Docker Container

Let’s start with some basics..

What is Docker Containers?

Docker containers or containers are the independent OS that are launched as a process over the base OS. According to Docker official documentation, a container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

The containerization is said to be next virtualization, and all the enterprises are adopting it rapidly. The beauty of the containers is it launch within few seconds, that's why it is highly time and cost saver.

Little about HTTPD server:

HTTPD server or Apache webserver which we can configure on our local system to work as a webserver. We are going to create a webserver over a docker image.

Setup config:

I am using RHEL 8 as my base OS for docker which is installed over the VMware. Just this. No extra requirement.

We are going to discuss two methods,

Method 1: Manual Method

Method 2: Using Docker File

Method 1 : Manual method

First make sure your that docker is installed in your system properly, i.e. quick run docker ps command to verify. you will get the output like this.

This command shows if any container is running in background or not.

Now, let’s launch the CentOS container, to launch we use the command:

[root@localhost ~]# docker run -it --name webconm centos

If you already have the image downloaded, it will launch in a second. But if you don’t have the image, the command will download for you and it will take some time.

After launching the container you will see a change in terminal, it changes from root@localhost ~ to root@<id of docker container> \ this means you are inside the docker container.

Let’s proceed further, now we have to setup httpd(same as we do for RHEL).

First install httpd and net-tools, using yum:

# yum install httpd net-tools -y

This will install the required programs which we are going to use.

After the installation completes, migrate to /var/www/html to add the index.html file.

# cd /var/www/html
# cat > index.html
Hello from the docker container, If you can see this from your browser that means, you have configured it successfully.
^C
# cat index.html
Hello from the docker container, If you can see this from your browser that means, you have configured it successfully.
# cd

After this its time to start service, normally we start the service with the systemctl command, let’s try it out then.

# systemctl start httpd

Did the service start? Answer will obviously no. So how to solve this problem? Is there any solution? Do we need to install more tools but you said we have already installed all the requirements.

Yes, there is the solution, and we don’t need to install anything. For, sometime lets come out of the docker(do not exit open new terminal), and lets dig deeper inside the systemctl command.

I already have httpd installed over the base OS, I am checking the status using the command:

[root@localhost ~]# systemctl status httpd

The output I got as follows:

From above see, what systemctl is doing in background running /usr/lib/system/httpd.service so lets migrate to this program and see if we can find something’

[root@localhost ~]# vim /usr/lib/systemd/system/httpd.service

It opens the file like this:

If you read carefully, under the service tag you can find this line

ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND

Believe me this is most amazing line, I got amazed by this line, when vimal sir was teaching.

So, what does this line mean? This means when we run the systemctl start httpd it runs the /usr/sbin/httpd in the background for us.

Now, lets come back to docker container:

So, we don’t have have systemctl to run /usr/sbin/httpd for us, so we have to run it manually. This means to start the service we need to give the command:

# /usr/sbin/httpd 

Now, from ifconfig get the IP address, and open the firefox, go to the IP, you will see the webserver is configured. So my IP is 172.17.0.2

Yay! Our web server is up and running!

DockerHub link to image: click here

Method 2:

We can use the Dockerfile to build a image and launch the container from it.

I am attaching the Dockerfile that I have created to build the image, if you want to see the code on GitHub, click here.

After this run the command

[root@localhost ~]# docker build -t <name>:<tag> <Path to Dockerfile>

This will build your image and you can run using docker run and see if working or not.

Thanks a lot for reading this article! See you in next one where I setup the python interpreter in the docker image!

--

--

Rishi Agrawal

Aspiring MLOPS engineer with Multi-cloud and Flutter/MERN