Designed By Rahul Sil.

Configuring Webserver in a Docker Container 🚢

Rahul Sil
5 min readMar 15, 2021

--

Today is the world of containerization where all the applications are being deployed in containers.

Containers help us in the microservices world were every part of an application is deployed inside containers. Docker is one such containerization technology which helps us with containers. The best part of using containerization technologies like Docker, Podman, CRIO is that the containers/OS are launched and provisioned is a few seconds.

This speed in provisioning of OS helps us in the field where the servers are terminated often frequently and needs to be provisioned fast enough so that the users do not face any delay in the services.

📌 In this blog I am going to discuss about a setup where a webserver is running inside a docker container. 🤩

I am going to use RedHat Enterprise Linux 8 for doing this practical.

First lets install docker in our system. For that we need to configure our yum repository so that yum command can install the docker software.

Go to the location /etc/yum.repos.d/ and create a file with the name docker.repo

You can give any name to the file but the extension of the file should be .repo

Now in the docker.repo file copy the below statements.

[docker-ce]
name=DOCKER REPO
baseurl=
https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck=0

The baseurl given above contains the docker software and all other dependent softwares that docker requires. yum will go to this url and download all the required packages.

gpgcheck=0 so that no signature check is done.

Normally in RedHat Enterprise Linux we need to install the Docker Enterprise Edition, but using this method we can install Docker Community Edition which is free.

After you have created the repo file, then run the below command to install docker.

yum install docker-ce --nobest

The option “- -nobest” is used so that no broken packages are installed and all the dependent packages are in working state.

After the installation is done then start the docker services in your system.

systemctl enable docker --now

This command will start the docker services and also make it permanent so that after restart of the system the docker services are still running and we do not have to manually start the docker services every time.

Docker 🚢 is now successfully installed and permanently started.

After this, if you run the command “docker images” you will see no container images is present. We will install images now but this tells us that docker is running successfully.

I am going to use centos:latest image for this practical. You can use any other image as well. You can check all the available container images in docker hub.

To pull the centos image run the following command -

docker pull centos:latest

🎯 You can use any other version of centos as well.

After the image download is done you can confirm the same using the following command -

docker images

Now let’s start a container …..

docker run -dit --rm --privileged=true --name webserver centos:latest "/usr/sbin/init"

docker run command is basically used to run a container.

-dit” — To run the container in detached(d) mode and have an interactive(i) terminal(t).

- -rm” — To remove the container as soon as it is stopped.

- -name” — To give a name of your choice to the container.

Now inside the docker containers “systemctl” commands does not work as it is not booted up using systemD. So in order to make the systemctl command available inside the container we use the “- -priviledged=true” option and pass the “/usr/sbin/init” path also.

After running the above command you will see that a container ID is displayed as the output. This also means that the container is launched and provisioned within a second.

docker ps

Using the above command you can see that the container is in the running state.

docker exec -it webserver bash

After you have run the above command you will land up in the containers terminal.

Now you can start configuring the HTTPD webserver inside the container.

From now run the commands in the container terminal.

yum install net-tools ncurses httpd php -y

This will install the httpd software, php software. net-tools and ncurses packages for getting the ifconfig and clear commands inside the container, normally they are not there as container images are created having minimum commands available.

systemctl enable httpd --now

This will start and enable the httpd webserver which can be confirmed using the netstat command.

[root@ec49910bd2ac /]# systemctl enable httpd --nowCreated symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.[root@ec49910bd2ac /]# netstat -tnlpActive Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 258/httpd

As we can see that the webserver is running inside the container at port 80.

Let’s write a PHP page which prints the IP address of the server where it is running.

PHP Code.

Create a index.php page in the /var/www/html/ location.

After this get the IP address of the container using ifconfig.

In my case the container IP = 172.17.0.2

Now go to the docker host system and open the browser. Write the container IP in the URL box.

As you can see that the webpage displays the container IP, this confirms that our webserver was successfully configured inside a Docker container.🤩

I hope you liked this article.💖

Would definitely like to hear your views on this and feedbacks so that I can improve on those points in future articles. 🙌 Comment your views below.

You can also check my LinkedIn profile and connect with me.

Follow me on medium as I will come up with articles on various technologies like Cloud Computing, DevOps, Automation and their integration.

That’s all for now. Thank You !! 😊✌

--

--

Rahul Sil

I am a tech enthusiasts. I love exploring new technologies and creating stuff out of them !! ✌