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.
Prerequisites
- A server instance running Linux.
- Docker with non-root access enabled.
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.
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 port80
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.
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.
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>
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.
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.
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
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.
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?
Đăng ký liền tay Nhận Ngay Bài Mới
Subscribe ngay
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !
Add Comment