Apache Docker httpd Dockerfile example
The Apache web server is one of the most popular open source HTTP servers out there.
And Docker is the most popular container runtime among DevOps teams.
Given the pair’s popularity, it’s no wonder the need to dock Apache web servers is a common requirement for many cloud-native deployments.
Steps to dockerize Apache httpd websites
To deploy a website to a dockerized Apache httpd web server, follow these steps:
- Install Docker (prerequisite)
- Pull down the official Apache httpd image from DockerHub
- Copy your website to htdocs folder of apache image
- Create a custom image based on the updated Apache httpd Docker image
- Run your dockerized Apache http hosted website on port 80
Create Apache http Docker file
There are several ways to accomplish these seven steps, although using a Dockerfile is the most common.
To do this, simply create an extensionless file named Dockerfile on your hard drive. Next, place all the files you want to deploy to the dockerized Apache httpd server in a folder named website.
Apache httpd Dockerfile example
Here is an example httpd Dockerfile which will copy files from a folder named /website/ on your local computer and place these files in the Apache Docker image website hosting directory.
FROM httpd:2.4 COPY ./website/ /usr/local/apache2/htdocs/
The first line of this Apache Dockerfile instructs Docker to pull version 2.4 of the official DockerHub httpd image.
The second line asks Docker to copy all files from a subfolder named /website/ and move them into the htdocs Docker image folder httpd.
In this Apache Dockerfile example, the /website The folder listed in the COPY command is relative to the folder where the docker build command that uses the Dockerfile will be run. A full path to the folder containing all your website files can be provided instead.
Note that this command will not create a subfolder named /website in the dockerized httpd image. All copied files will be served from the root folder when running the custom Apache Docker image.
Create your Apache Docker image
With the Dockerfile placed in the parent directory of the websites folder, run the following command from the folder where the Dockerfile resides. This command will dockerize Apache with your website packaged in the custom image.
[email protected] /c/example/rock-paper-docker $ docker build -t apache-docker-example .
Apache Docker file location
Note the period at the end of the command. This endpoint tells the docker command to look for the Apache Dockerfile in the current directory. If the command is not run in the same folder as the Dockerfile, a full path must be provided.
To verify that the docked Apache image was created, run the docker images command. You should see an image named apache-docker-example listed.
[email protected] /c/example/rock-paper-docker $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE apache-docker-example latest 0859b847a8a5 2 days ago 144MB
Run your website on Apache in Docker
Once the image is created, the last step is to run the dockerized Apache http server in a container. Run the following command to start a container that runs your custom httpd image on port 80.
[email protected] /c/example/rock-paper-docker docker run -d --name httpd-docker-01 -p 80:80 apache-docker-example
The dockerized Apache website, which in this case is a friendly little guessing game, will now be accessible on port 80 of your browser.
How to avoid conflicts with port 80
If port 80 is busy, or if you want to create a second container based on your custom httpd Docker image, run the following command which exposes the container’s internally used port 80 to the externally accessible port 88 of your local computer.
[email protected] /c/example/rock-paper-docker docker run -d --name httpd-docker-01 -p 88:80 apache-docker-example
Explanation of Docker Apache run command
The docker run command creates a container based on a Docker image and then runs that container.
The docker run command -D The switch allows the container to run as a background daemon process. If this was not added to the command, the container would not release control of the terminal window or command prompt that issued the run command until the anchored Apache website container end.
Docker containers are assigned long hexadecimal codes that identify them. the -Name The option allows you to assign a friendly name to the container so that it can be easily referenced in future commands.
Docker httpd port mapping
The httpd Docker image internally runs the Apache web server on port 80. However, docker does not expose the ports of internally running processes by default. the -p The command exposes port 80 used internally by Docker to an open port on the local machine.
Note that the port to use on the local machine comes first, and the port used internally comes second. So -p 88:80 maps port 80 used internally by Docker to port 88 on the local machine. Transposing these numbers will cause the dockerized httpd container to fail.
The last part of the run command is the name of the custom, rooted Apache image we created. In our case, the name of the custom Docker image is apache-docker-example. This is the name we gave to the image when the docker build command was issued.
In Docker, you build images while you run containers. From a Docker image, many container instances can be generated.
— Cameron McKenzie (@cameronmcnz) May 19, 2022
Where is htdocs in Docker httpd images?
the htdocs folder is where the Docker httpd container serves files. The full path to the htdocs folder in the httpd Docker image is:
This folder can be updated on the fly by adding files with the docker-cp ordered. The following command would add a file named rps.html in the root folder from which Apache serves the files:
docker cp ./rps.html httpd-docker-01:/usr/local/apache2/htdocs
Whenever a running dockerized Apache httpd container is updated, a new image must be created to persist the changes. To create a new image based on a Docker container running the Apache httpd Docker image, simply issue a commit docker ordered:
docker commit httpd-docker-01 apache-docker-example:latest
Docker and Kubernetes are the standard for deploying cloud-native applications. If your deployment architecture includes a static website that runs on an open source file server, you can rest assured that the process you need to follow to create a Dockerfile, dockerize Apache, and host your deployed website in a container is a relatively simple undertaking.