How to Configure a Web Server on Docker and Why.

Arjun Chauhan
5 min readNov 1, 2020

--

I have recently started learning about containerization. Before beginning I would like to talk a little about containerization and why we need this technology.

A regular setting and booting of new OS requires about 20 to 30 minutes. This causes a lot of wastage of time and energy because in technical use cases we have to boot multiple OS for different tasks.

So a common question that arise is that why we need to launch so many OS. Performing tasks on different OS increases security. For example let’s say a new application being tested has a bug that renders the whole OS crashed. This could create huge problems if we use our main system for this task. So we use containers to create an isolated system to perform these tasks. There are many more benefits although I wont be talking about containers more in this article. A story for another day.

So what is docker?

Docker is a software platform for building applications based on containers. It creates lightweight environment which although using the underlying kernel of host but are isolated from them. An OS that takes about 20 to 30 minutes to launch conventionally takes just 1 second to run on docker. So that saves a lot of time and energy in industry.

This is the reason why docker has become so popular since it’s launch in 2013 by Solomon Hykes.

So since we have enough information about what docker is let’s get right into setting up the WebServer on docker. I would be setting up Apache Web Server .

The main steps include:

  1. Installing docker services.
  2. Starting the docker services
  3. Setting up the docker container
  4. Installing the Apache Server on docker
  5. Running the Apache Server
  6. Create a webpage for testing purpose

Installing docker services

Since I am using the Redhat 8 as host OS I am using yum to install the services.

yum install docker-ce — nobest -y

Starting the docker services

We have used the docker community edition since it is free to use. Before we start the container we need to start the services

systemctl start docker

To check if the docker services have started without any error we can use the below command

systemctl status docker

So it is clear that the services are up and running

Setting up a new docker Container

Now we will see the power of docker. Before we can boot a new container we need to have an image of OS. This image can be downloaded from docker.hub

I would be using the centos image. You can use the following command to directly download it

docker pull centos

So now we need to launch a new container.

docker run -it — name webserver centos

This command launches a new docker container with the name of “webserver” and providing us an interactive terminal which is decided by the “-it” command specified. Once this command is used you would be landed inside the new container.

The CLI changes from the one shown above to the one below it. The random number that appears in the CLI is the unique ID that every container has.

Installing the Web Server

The setting up of new OS took about 1 second. So now we can launch webserver on docker. To download the server we use

yum install httpd -y

This installs the httpd server on the system.

Now we need to run the server. On a host machine we could have used

“systemctl start httpd”.

The reason why this action cannot be completed is because the Process ID of the host machine is usually loaded with systemd but in docker by default the PID 1 is used by /bin/bash. I would not like to dwell further because we can go directly in the location where it is located and run it from there

/usr/sbin/httpd

It will run the httpd server. So for testing purpose I would be creating a new webpage. The httpd server by default looks for webpages at /var/www/html folder. So we need to create the html file at that location.

I created the following page

Once the file is saved you can view the file in the browser using the URL.

https://localhost/newpage.html

Not the most beautiful page in the world but we are just testing here.

Although virtual machines are an alternative to containers, the fact that containers do not contain an operating system (whereas virtual machines do) means that containers have much smaller footprints than virtual machines, are faster to create, and quicker to start.

In this tutorial we installed docker and created a container. We then launched a webserver in the container and created a webpage.

I would be writing more about docker so stay tuned if you liked this.

Thanks

--

--

No responses yet