How to Setup Apache Web Server in a Docker Container

This tutorial will explain how to install an Apache web server inside a docker container. First, we should get an idea about Docker Containers and Apache Web Servers.

What is a Docker Container?

Developers often face problems when deploying their applications as sometimes their development environments do not match their production environment. The introduction of Containers solved the problem.

A container includes an entire runtime environment. It bundles up the code and dependencies such as system libraries, system tools, and settings. This process eliminates the underlying infrastructure, differences in operating system distributions, the application platform, and dependencies.

Containerizing is different from virtualization technology. In virtualization technology, the deployed package is a virtual machine, including an entire operating system and the application. A physical server will have a hypervisor on top of which we can install multiple virtual machines with different operating systems.

Docker provides the Docker engine in which we can host multiple containerized applications. These applications run on a single operating system, and each container shares the operating system’s kernel with the other containers. They use fewer resources than virtual machines as all containers share a single operating system kernel. Docker can run on any Linux, Windows, or macOS machine. 

setup apache in docker

What is an Apache Web Server?

Apache Web Server maintained by the Apache Software Foundation is a popular open-source web server creation, deployment, and management tool. Its features include an authentication mechanism, database support, server-side scripting, and support for multiple programming languages. Apache’s ability to handle large amounts of traffic with minimal configuration is one of its advantages. Apache runs on Linux, macOS, and Windows. Web hosting companies use it to provide shared/virtual hosting. Apache Web Server, by default, supports and can differentiate between multiple hosts that reside on the same machine.

Installing Docker Ubuntu 20.04

Step 1

Before installing anything in Ubuntu, running the following commands and preparing the environment is always better.

				
					sudo apt-get update 
sudo apt-get upgrade 
				
			

sudo apt-get update gets you an updated list of packages from the Internet while sudo apt-get upgrade installs available upgrades of all packages currently installed on the system from the sources configured via sources.list file.

Step 2

For Step 3, we need to use cURL to transfer data using various network protocols such as HTTP, HTTPS, FTP, etc. To check whether it is available in your machine, run the following. type curl and run the command. If it is available, you will get the following message.

setup apache web server in docker

Otherwise, you will get a message as curl command not found.

In that case, you can install cURL by running the following command. 

				
					sudo apt install curl
				
			

Step 3

Run the following command to install docker on your machine. The command will download and run a shell script to add the Docker repository to your system and install the package.

				
					curl -fsSL https://get.docker.com | sh
				
			
install apache in docker

If you get any errors, you can fix them by running sudo apt-get update –fix-missing. We used -fsSL options of cURL when running this command. The purpose of each option is as below.

-f – Fail silently without outputting a return error.

-s – Run in silent mode without showing progress or error messages.

-S – When used with -s, cURL shows an error message if it fails.

-L – If the requested page is not available in the location, redo the request on the new place.

sh to executes the downloaded shell script.

Step 4

Run the following commands to start the Docker engine and check the status of the Docker engine. 

				
					systemctl start docker
systemctl status docker
				
			
installing apache in docker

The highlighted section in the image above shows whether your Docker engine is active & running properly. systemctl command controls the systemd system (a collection of system management daemons, libraries, and utilities) and service manager.

Step 5

Run the following command to see a list of available commands.

				
					docker
				
			
apache container docker

If you want to know how to install Docker AWS, here is a god guide on it. 

Setting Up an Apache Container

In this tutorial, we will create a container named myContainer by downloading and using one of the standard containers available in the Docker ecosystem. We will use an instance of Apache 2.4 container and the httpd:2.4 image from Docker Hub. We will serve a simple web page (myPage.html) from /home/user/website/ directory (you can add your path here). We will first make a request to our public IP address on port 8080. That request will get redirected to port 80 on the container. To do this, we map /home/user/website/ to /usr/local/apache2/htdocs/ on the container.

Step 1

Run the following command to instantiate the container. (Note: – Make sure to change your container name and path if necessary). You will also need to use sudo as you may need root privileges to proceed.

				
					sudo docker run -dit --name myContainer -p 8080:80 -v /home/user/website/:/usr/local/apache2/htdocs/ httpd:2.4
				
			
setup apache container in docker

Step 2

Check the status of the container by running the following command.

				
					sudo docker ps

				
			
apache web server in docker

If you are getting the above stats, that means your container is up and running.

Step 3

Create a simple web page named myPage.html inside /home/user/website directory by running the following command.

				
					sudo vi /home/user/website/myPage.html
				
			

Add the content below, into myPage.html

				
					


    
    <p>Docker-Apache</p>


    <b>Hello World!</b>   






				
			

Step 4

The next step is to go and view your web page in the browser. But before that, we need to find the IP address of your Docker Engine. The structure of a Docker network is as below.

docker architecture

To get the IP address of each container run

				
					sudo docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myContainer
				
			
apache configuration docker

The container is, by default, assigned an IP address for every Docker network it connects to. Each Docker network has a default subnet mask used as a pool to give away the IP addresses. Docker uses the default 172.17.0.0/16 as the subnet for container networking. However, in Docker for Linux (standard distribution), the host’s IP address is 172.17.0.1 (on the main network of docker).

To make sure you have the correct IP, run ifconfig in the terminal. Your IP will be listed as highlighted in the image below.

docker apache container

Go to your browser and type X.X.X.X:8080/myPage.html (X.X.X.X is the IP address of your host).

docker hello world

Additional commands

Once you have finished the work, you can stop the container by running the command below.

				
					sudo docker stop myContainer
				
			

To remove the container run

				
					sudo docker rm myContainer
				
			

If you are not creating any more apache containers, you can remove the httpd:2.4 image that you used to create the container by running

				
					sudo docker image remove httpd:2.4
				
			

To stop the docker engine, run

				
					systemctl stop docker
				
			

Conclusion

Docker containers let developers efficiently develop and deploy their applications. It allows you to make portable container images of your application to run in any environment, such as on-premises Kubernetes to Azure ACI, Google GKE, AWS ECS, and more. You can also integrate it with other tools such as VS Code, CircleCI, and GitHub.

We hope this tutorial has guided you through the basics. There is a lot more to learn. For more tips and guidelines, visit Docker documentation here.

Avatar for Shanika Wickramasinghe
Shanika Wickramasinghe

Senior Software Engineer at WSO2 which is the 6th largest Open Source Software Company in the World. My main skills are machine learning and software development. I have 5+ years of experience as a Software engineer.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x