Community driven content discussing all aspects of software development from DevOps to design patterns.
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.
To deploy a website on a dockerized Apache httpd web server, follow these steps:
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.
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:
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.
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.
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:
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:
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:
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.
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:
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.
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.
In Docker, you build images while you run containers. From one Docker image, many instances of containers can be spawned.
The Docker httpd container serves files from the htdocs folder. The full path to the htdocs folder in the Docker httpd image is:
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:
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 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.
Tools for enterprise architects get criticized for their complexity, but they are getting better. A recently released module from...
Observability helps developers and IT operations teams identify and fix system issues. And with the right setup, it can improve ...
As distributed software systems grow in scale and complexity, things like monitoring and debugging can become a tangled mess. ...
Tabnine's code completion assistant adds support for 11 languages with a major AI overhaul.
Experts weigh in on which in-demand programming languages offer the best job prospects.
The data a test summary report compiles can provide great benefits to a development team -- and, in turn, business value to the ...
Alfa Romeo F1 Team Orlen uses Zadara to store and access race and technical data so that it can improve race analysis and ...
FinOps practices can guide an organization toward more effective cloud cost management. How much do you know about key FinOps ...
How do you choose the cloud programming language that best suits your current and future needs? Explore 11 popular options and ...
Researchers say a critical flaw in the Atlassian Confluence Data Center and Server is now being used to spread ransomware in the ...
Federal government officials say Russian sanctions decreased cyber attacks on the U.S. over the past few months but could ...
Data sovereignty regulations, combined with a tsunami of data growth and increased cloud usage, have created a perfect storm that...
Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates ...
There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service ...
AWS users face a choice when deploying Kubernetes: run it themselves on EC2 or let Amazon do the heavy lifting with EKS. See ...