How to Install Apache in Docker

Introduction

Docker is a powerful tool for running applications in isolated containers. Apache is a popular web server that can be used to serve web content. Installing Apache in Docker is a great way to quickly and easily deploy a web server. This tutorial will walk you through the steps of installing Apache in Docker and setting up a basic web server. By the end of this tutorial, you will have a working Apache web server running in a Docker container.

How to Install Apache in Docker

1. Pull the Apache image from the Docker Hub:

docker pull httpd

2. Run the Apache container:

docker run -dit –name my-apache-app -p 8080:80 httpd

3. Check the running container:

docker ps

4. Access the Apache web server:

Open a web browser and type http://localhost:8080/

5. Stop the Apache container:

docker stop my-apache-app

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?

How to Install Apache in Docker

Installing Apache in Docker is a great way to quickly and easily deploy a web server. Docker is a container platform that allows you to quickly and easily deploy applications in a secure and isolated environment. Apache is a popular open-source web server that is used to serve web pages and other content over the internet.

Prerequisites

  • A computer running Linux or Windows with Docker installed.
  • A basic understanding of Docker and its commands.

Step 1: Pull the Apache Image

The first step is to pull the Apache image from the Docker Hub. To do this, open a terminal window and run the following command:

docker pull httpd:latest

This will pull the latest version of the Apache image from the Docker Hub. Once the image has been pulled, you can verify that it was successfully downloaded by running the following command:

docker images

This will list all of the images that are currently stored on your system.

Step 2: Create a Container

Once the image has been pulled, you can create a container from it. To do this, run the following command:

docker run -d -p 80:80 --name my-apache-container httpd:latest

This will create a container named “my-apache-container” and will map port 80 on the host machine to port 80 on the container. This will allow you to access the web server from the host machine.

Step 3: Test the Installation

Once the container has been created, you can test the installation by accessing the web server from a web browser. To do this, open a web browser and navigate to http://localhost. If everything is working correctly, you should see the default Apache web page.

Conclusion

Installing Apache in Docker is a great way to quickly and easily deploy a web server. By following the steps outlined in this article, you should be able to get your Apache web server up and running in no time.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?