How to Install Apache in Docker

January 17, 2023

Introduction

Docker speeds up and streamlines application deployment by introducing containers based on preconfigured images. Containers are convenient for applications such as web servers, which need to preserve their configurations across different web browsers, target OS environments, and CPU architectures.

This article shows two methods to deploy an Apache web server using Docker.

How to install Apache in Docker.

Prerequisites

Run Apache Docker via Docker Hub Image

The simplest way to install an Apache web server in Docker is to run a container using a preconfigured Docker Hub image. While this procedure does not let you customize the image for your deployment, it provides an excellent way to test a web server.

Follow the steps below to deploy an Apache container based on the Docker Hub image.

Download the Apache Image for Docker

The official Apache image on Docker Hub is httpd. Download the image to your system with the docker pull command:

docker pull httpd

The command output shows the pull progress and reports when the download finishes.

Pulling the Apache httpd image from Docker Hub.

Start the Apache Container

Type the docker run command below to create and start a Docker container based on the httpd image:

docker run -d --name [container-name] -p 80:[host-port] httpd

The command contains multiple options.

  • The -d option tells Docker to run the container in the detached mode, i.e., as a background process.
  • The --name flag lets you name the container. If you exclude this flag, Docker generates a random phrase for a name.
  • The -p option allows you to map the TCP port 80 of the container to a free open port on the host system.

The following example uses the httpd image to create and run a container named apache and publish it to port 80 on the host system.

docker run -d --name apache -p 80:80 httpd

If there are no errors, Docker outputs the ID of the new container.

Running an Apache Docker container using the Docker Hub httpd image.

Check if Apache is Running

Test the new Apache installation by opening a web browser and typing the server's IP address:

http://[server-ip-address]

Alternatively, connect with the Apache installation on the same machine by typing localhost and the host port you assigned to the container.

http://localhost:[host-port]

Apache displays the default page, confirming the successful deployment.

Testing the Apache deployment with Docker.

Note: Stop a running docker container by typing:

docker stop [container-name-or-id]

For more Docker management commands, refer to our Docker Commands Cheat Sheet.

Run Apache via Dockerfile

Another way to deploy an Apache instance in Docker is to use a customized image created with Dockerfile. Follow the steps below to create the image and run a container based on it.

Create a Directory for Apache Image

Start by placing all the content relevant to the image into a dedicated directory.

1. Create and go to the apache directory.

mkdir apache && cd apache

2. In the directory, create or copy the files you want to include in the image.

For example, include a new landing page by creating an index.html file in a text editor:

nano index.html

Write some HTML in the file.

<h1>Test</h1>
<p>This is a test page for the Apache deployment in Docker</p>
Editing a test page for the Apache deployment in Docker.

Save and exit the file.

Note: phoenixNAP Bare Metal Cloud servers deploy in minutes and allow you to automate and orchestrate server provisioning.

Build Dockerfile

Next, create the Dockerfile for the new image. Dockerfile contains all the necessary instructions for Docker to assemble a specific image.

1. Use a text editor to create the file.

nano Dockerfile

2. Write the image configuration.

FROM httpd:latest
COPY index.html /usr/local/apache2/htdocs
EXPOSE 80

The example above uses the latest httpd image as a template and modifies it by copying index.html from the local directory to the /usr/local/apache2/htdocs directory in the image. Furthermore, container port 80 is exposed to make it available for mapping to a host port.

Editing Dockerfile for an Apache Docker image.

Save the file and exit.

3. Use docker build to create a Dockerfile-based image.

docker build -t [image-name] .

The output shows Docker applying the steps from the Dockerfile.

Building an Apache Docker image with Dockerfile.

Run Apache Dockerfile as a Container

Run an Apache container with the image you made:

docker run -d --name [container-name] -p 80:[host-port] [image-name]

The example below creates a detached container named apache using the apache:v1 image created with Dockerfile. The container port 80 is mapped to host port 80.

docker run -d --name apache -p 80:80 apache:v1
Runing an Apache container using the image made with Dockerfile.

Verify if Apache is Running

Test the new Apache installation by navigating to the server's IP address in a web browser:

https://[server-public-ip-address]

Apache serves the test page included in the custom image, confirming the successful deployment.

Testing the Apache Dockerfile deployment.

Note: If the browser displays any other page besides the one above, clear the cache and reload the page.

Conclusion

The tutorial covered the two ways to deploy an Apache web server using Docker. The first method included using the official image, while the second covered the steps necessary to create your custom Apache image.

Learn more about Apache and other LAMP components by reading What Is LAMP Stack?

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Web Server vs. Application Server
January 17, 2023

Both web and application servers respond to user requests for content. Yet, there are several key differences between the two...
Read more
Apache vs Nginx - Detailed Comparison
October 5, 2023

In this comparison article, you will read about the differences between the two most popular open-source web servers today – Apache and Nginx.
Read more
How to Deploy NGINX Reverse Proxy on Docker
March 14, 2024

The easiest way to set up and manage reverse proxies is to use Nginx and Docker.
Read more
Apache 403 Forbidden: Reasons and How to Fix It
May 30, 2024

Despite Apache web server's ease of use, it’s not uncommon to encounter a ‘403 Forbidden’ error after setting up a website.
Read more