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.
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.
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 updategets 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.
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
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
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.
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.
Check the status of the container by running the following command.
sudo docker ps
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/websitedirectory by running the following command.
sudo vi /home/user/website/myPage.html
Add the content below, into myPage.html
Docker-Apache
Hello World!
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.
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.
Go to your browser and type X.X.X.X:8080/myPage.html (X.X.X.X is the IP address of your host).
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.
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.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.