How to Setup a Simple Apache Web Server in a Docker Container
Welcome readers, in this tutorial, we will discuss how to host a static webpage running on Apache web server on Docker.
1. Introduction to Docker
In the present world, Docker is an important term,
- Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
- Is a standard for Linux Containers
- A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux
1.1 Docker Terminology
- Image: Representation of Docker container i.e. a JAR or WAR file in Java
- Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
- Engine: The code that manages, creates and runs the Docker containers
- Hub: A public developers registry to distribute their code
- Repository: A collection of Docker related images i.e. different versions of the same application
1.2 Docker Command Basics
Here’s an example command.
1.3 Need for using Docker
- For environment replication, while the code runs locally on the machine
- For numerous deployment phases i.e. Dev/Test/QA
- For version control and distributing the application’s OS within a team
1.4 Setting up Docker
If someone needs to go through the Docker installation, please watch this video.
To start with this tutorial, we are hoping that users at present know how to create a simple hello-world spring-boot application.
2. How to Setup a Simple Apache Web Server in a Docker Container
Here is a systematic guide for implementing this lab tutorial.
2.1 Application pre-requisite
To host a static site on Apache server, developers need to create a
Dockerfile that will, in turn, create an image file (Containing the static webpage) for our application. To create an image file, we’ll add the
Dockerfile in the project’s root directory as shown in the project structure and add the following code to it.
# Pull the httpd image from DockerHub repository to build our application as a base FROM httpd:2.4 # Copy the static page from the target directory to apache2 docs COPY ./public-html/ /usr/local/apache2/htdocs/
2.2 Build the image
Open the terminal and navigate to the project directory and execute the below commands to build the application and the image.
# To prepare the image with a given name > docker build -t my-apache2 .
If everything goes well, the image will be created. We can use the
docker images command to list the newly created image as shown in Fig. 3.
2.3 Starting the Docker container
Now we can run the below command to start the container and can hit the application URL (
http://localhost:80/) as shown in Fig. 4.
docker run -p 80:80 --name my-apache2-1 my-apache2
Try out these commands in your development environment to practice and learn.
That is all for this tutorial and I hope the tutorial will help you to set up an Apache web server on Docker. Happy learning and do not forget to share!
4. Download the source code
You can download the full source code of this example here: How to Setup a Simple Apache Web Server in a Docker Container