How to dockerize Apache httpd websites example

Apache Docker httpd Dockerfile example

The Apache Web Server is one of the most popular, open source HTTP servers in existence. And Docker is the most popular container runtime amongst DevOps teams.

Given the popularity of the pair, it’s no wonder why the need to dockerize Apache web servers is a common requirement of many cloud native deployments.

Here’s how to do it.

Steps to dockerize Apache httpd websites

To deploy a website on a dockerized Apache httpd web server, follow these steps:

  1. Install Docker (prerequisite);
  2. Pull down the official Apache httpd image from DockerHub;
  3. Copy your website into the Apache image’s htdocs folder;
  4. Build a custom image based on the updated Apache httpd Docker image; and
  5. Run your dockerized Apache http hosted website on port 80

Create the Apache http Dockerfile

There are several ways to accomplish these seven steps, but the most common one is to use a Dockerfile.

Simply create an extension-less file named Dockerfile on your hard drive. Then put all of the files you wish to deploy to the dockerized Apache httpd server in a folder named website.

Apache httpd Dockerfile example

The following httpd Dockerfile example copies files from a folder named /website/ on your local machine and place those files in the website hosting directory of the Apache Docker image:

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 httpd image from DockerHub.

The second line instructs Docker to copy all of the files from a subfolder named /website/ and move them into the htdocs folder of the httpd Docker image.

Apache and Docker tutorials

Master the fundamentals of Apache, Docker and Kubernetes technology:

The complete Apache Dockerfile example for this custom httpd Docker image is only two lines long.

In this Apache Dockerfile example, the /website folder listed in the COPY command is relative to the folder in which the docker build command that uses the Dockerfile will be run. A full path to the folder that contains all of the files for your website can be provided instead.

Note that this command does not create a subfolder named /website in the dockerized httpd image. All of the copied files are served up from the root folder when the customized Apache Docker image runs.

In this example, the Apache Dockerfile resides in the parent directory of the website folder.

Build your Apache Docker image

With the Dockerfile placed in the parent directory of the websites folder, issue the following command, which dockerizes Apache with your website packaged within the custom image:

[email protected] /c/example/rock-paper-docker
$ docker build -t apache-docker-example .

Apache Dockerfile location

Note the period at the end of the command. That trailing dot is an instructs 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, you must provide a full path.

To verify creation of the dockerized Apache image, issue the docker images command. This should list an image named apache-docker-example:

[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

With the image created, the final step is to run the dockerized Apache http server in a container. Issue 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 number-guessing game, is now accessible on port 80 in your browser.

This dockerized Apache website runs on port 80 of the localhost address.

How to avoid port 80 conflicts

If port 80 is occupied, or if you want to spin up a second container based on your custom Docker httpd image, the following command exposes the container’s  internally used port 80 to the externally accessible port 88 of your local machine:

[email protected] /c/example/rock-paper-docker
docker run -d --name httpd-docker-01 -p 88:80 apache-docker-example

Docker Apache run command explained

The docker run command creates a container based on a Docker image and subsequently runs that container.

The docker run command’s -d switch allows the container to run as a background daemon process. Without this, the container does not release control of the terminal window or command prompt that issued the run command until the dockerized Apache website container terminates.

Docker containers are assigned long, hexadecimal codes that identify them. The --name option allows you to assign a friendlier name to the container so you can easily reference it 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 any internally running processes by default. The -p command exposes Docker’s internally used port 80 to an open port on the local machine.

Note the port to be used on the local machine comes first, and the port used internally comes second. So -p 88:80 maps Docker’s internally used port 80 to port 88 on the local machine. If you transpose these numbers, the dockerized httpd container fails.

The last part of the run command is the name of the custom, dockerized Apache image we created. In our case the custom Docker image name is apache-docker-example, which we assigned to the image when the docker build command was issued.

Where is htdocs in Docker httpd images?

The Docker httpd container serves files from the htdocs folder. The full path to the htdocs folder in the Docker httpd image is:

/usr/local/apache2/htdocs/

You can update this folder on the fly by adding files with the docker cp command. The following command adds a file named rps.html into the root folder from which Apache serves files:

docker cp ./rps.html httpd-docker-01:/usr/local/apache2/htdocs

Any time a running dockerized Apache httpd container is updated, create a new image to preserve the changes. To create a new image based on a Docker container running the Apache httpd Docker image, simply issue a docker commit command:

docker commit httpd-docker-01 apache-docker-example:latest

Docker and Kubernetes is the standard for the deployment of cloud-native applications. If your deployment architecture includes a static website that runs on an open source file server, follow the relatively straightforward processes above to create a Dockerfile, dockerize Apache and host your deployed website in a container.

 

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close