How to setup Apache HTTP webserver on top of Docker Container?

Durvesh Palkar
7 min readMar 10, 2021

--

✒ Before Containers

Before containerization came into the picture, the leading way to isolate, organize applications and their dependencies was to place each and every application in its own virtual machine. These machines run multiple applications on the same physical hardware, and this process is nothing but Virtualization.

But virtualization had few drawbacks such as the virtual machines were bulky in size, running multiple virtual machines lead to unstable performance, boot up process would usually take a long time and VM’s would not solve the problems like portability, software updates, or continuous integration and continuous delivery.

These drawbacks led to the emergence of a new technique called Containerization. Now let me tell you about Containerization.

✒ What is Containerization?

Containerization is a type of Virtualization which brings virtualization to the operating system level. While virtual machines abstracts the OS from the hardware, Containers are one step ahead by abstracting the applications from the OS.

Reasons to use Containers

Following are the reasons to use containers:

  • Containers have no guest OS and use the host’s operating system. So, they share relevant libraries & resources as and when needed.
  • Processing and execution of applications are very fast since applications specific binaries and libraries of containers run on the host kernel.
  • Booting up a container takes only a fraction of a second, and also containers are lightweight and faster than Virtual Machines.

✒ What is Docker?

Docker is a platform which packages an application and all its dependencies together in the form of containers. This containerization aspect ensures that the application works in any environment.

As you can see in the diagram, each and every application runs on separate containers and has its own set of dependencies & libraries. This makes sure that each application is independent of other applications, giving developers surety that they can build applications that will not interfere with one another.

So a developer can build a container having different applications installed on it and give it to the QA team. Then the QA team would only need to run the container to replicate the developer’s environment.

Docker Image: In layman terms, Docker Image can be compared to a template which is used to create Docker Containers. So, these read-only templates are the building blocks of a Container. You can use docker run to run the image and create a container.

Docker Images are stored in the Docker Registry. It can be either a user’s local repository or a public repository like a Docker Hub which allows multiple users to collaborate in building an application.

Docker Container: It is a running instance of a Docker Image as they hold the entire package needed to run the application. So, these are basically the ready applications created from Docker Images which is the ultimate utility of Docker.

✒ APACHE HTTP software

For this demonstration, we are going to use a software from Apache community for configuring a system as a webserver.

Apache is the most widely used web server software. Developed and maintained by Apache Software Foundation, Apache is an open source software available for free. It runs on 67% of all webservers in the world. It is fast, reliable, and secure. It can be highly customized to meet the needs of many different environments by using extensions and modules. Most WordPress hosting providers use Apache as their web server software. However, WordPress can run on other web server software as well.

What is a Web Server?

Wondering what the heck is a web server? Well a web server is like a restaurant host. When you arrive in a restaurant, the host greets you, checks your booking information and takes you to your table. Similar to the restaurant host, the web server checks for the web page you have requested and fetches it for your viewing pleasure. However, A web server is not just your host but also your server. Once it has found the web page you requested, it also serves you the web page. A web server like Apache, is also the Maitre D’ of the restaurant. It handles your communications with the website (the kitchen), handles your requests, makes sure that other staff (modules) are ready to serve you. It is also the bus boy, as it cleans the tables (memory, cache, modules) and clears them for new customers.

So basically a web server is the software that receives your request to access a web page. It runs a few security checks on your HTTP request and takes you to the web page. Depending on the page you have requested, the page may ask the server to run a few extra modules while generating the document to serve you. It then serves you the document you requested. Pretty awesome isn’t it.

Having this introductory knowledge about Docker containers and Apache HTTP is enough. In this article we’ll see how to install docker in your local system and deploy a simple webserver application on top of docker container.

✒ Step 1: Installing Docker and starting docker service

I’ll be using RHEL 8 as my base Operating system, however you can use any linux distro according to your comfort as most of the linux commands will be similar. In RedHat, we commonly use the package manager called yum. Before installing docker, we have to first create a yum repository for docker as shown in the below image.

And then install docker using yum install docker-ce --nobest command. To start the docker service use the command: systemctl start docker

✒ Step 2: Downloading container image and launching a container

You will find the docker images for almost all the OS versions and flavours on hub.docker.com which is a public repository/ registry of container images. We will download a centos (latest version) image using the docker pull centos:latest . To see all the downloaded images in your local system use the command docker images .

Now we’ll launch a docker container from this centos image and login to it, usingdocker run -i -t --name <container_name> centos:latest . Here option -i means we want to interact with container, and -t is to provide us with the terminal of this container. We can give any unique name to our container, however if not provided then they’ll assign some unique name to it by default.

✒ Step 3: Installing and Configuring Apache HTTP software inside the container

We’ll again use yum to install apache HTTP inside the container. The software name for RHEL is "httpd”. To install: yum install httpd

My Base OS is RHEL8 Virtual Machine. Run the following commands in your base OS if the above yum command gives some error related to ‘mirror list’ and ‘cannot find a valid baseurl’:

systemctl restart firewalld

# Masquerading allows for docker ingress and egress

firewall-cmd — zone=public — add-masquerade — permanent

# Specifically allow incoming traffic on port 80/443

▪️firewall-cmd — zone=public — add-port=80/tcp

▪️firewall-cmd — zone=public — add-port=443/tcp

# Reload firewall to apply permanent rules

▪️firewall-cmd — reload

#Restart docker

▪️systemctl restart docker

Use the above commands and it will work.

Now let’s create a simple html page for testing. The document root in case of apache httpd is /var/www/html i.e. it is the default location to keep html files.

✒ Step 4: Starting apache httpd service and checking webpage

We usually use systemctl start httpd to start httpd webserver service. But due to some reason, we cannot use (bydefault) systemctl commands inside docker container. However systemctl command uses some internal command to start the httpd service which is /usr/sbin/httpd . We will use this to start the webserver service. (ignore the warning)

We’ll use curl command to check our webpage. For this we must know the IP of the container, and for which we use ifconfig command. However this command is not initially available in the container. We have to install a software viz net-tools which provides the ifconfig command.

Now check the IP of this container and use this IP to see the website.

Anddd…. we’ve did it. The website is working great. You can also check typing the IP in the browser of your HostOS.

(In the above image since I’m using curl command, the bold <b> tag is also printed, it will show bold effect incase you try it with browser)

--

--