Docker

Run the Apache Web Server within a Docker Container

In this tutorial, we will guide you through the process of setting up and running the Apache web server within a Docker container. This allows you to quickly deploy and serve your application in just few steps.

Requirements:

All you need for this tutorial is to ensure that you have Docker installed on your system. You may also require a network access and sufficient permissions to create and run the containers on your host system.

Apache Server through a Dockerfile

To reduce the complexity of setting up a web server using a container, Docker provides us with a Dockerfile where we can define the requirements and configuration settings for our container.

Start by creating a directory to store the related files and directories for the Apache setup.

$ mkdir apache_docker

 
Once created, create a new file in the directory to store the configuration as follows:

$ touch Dockerfile

 
To run Apache in a Docker container, we need to create a custom Docker image. This image includes Apache and any additional configurations or files that you need.

In the Dockerfile, specify the configuration file for the image as follows. Use an official Apache base image from Docker Hub:

FROM httpd:2.4
# Copy Apache configuration
# COPY ./apache.conf /usr/local/apache2/conf/httpd.conf

# Copy app files into the container
COPY ./public-html/ /usr/local/apache2/htdocs/

 
The given Dockerfile uses the official Apache image from Docker Hub as a base image. We then define the Apache configuration if required. This allows you to specify how you wish the web server to run. This includes the features such as SSL configuration, virtual hosts, etc.

Building the Docker Image

Once we are satisfied with the definitions, we can proceed and build the image by running the “docker build” command as follows:

$ docker build -t apache_docker

 
Ensure to replace the apache_docker image with the name that you wish to assign to your newly built image.

Running the Container

Now that we have the custom Apache image that contains our application, we can create a container from it using the following command:

$ sudo docker run -d -p 80:80 --name public_app apache_docker

 
The command should start a Docker container named “public_app” based on the apache_docker container. We also map the port 80 of the container to port 80 on the host system.

Accessing the Web Server

You can access the Apache web server by opening your browser and navigating to the address.

Stop and Remove the Containers

To stop and remove a running container, use the commands as provided in the following:

$ docker stop public_app
$ docker rm public_app

 

Cleaning Up

To remove the Docker image that we created, run the command as follows:

$ docker rmi public_app

 
Make sure that you removed all containers using this image before attempting to delete it. Otherwise, Docker will return an error.

Conclusion

In this fundamental tutorial, we covered the process of creating and running the Apache web server using Dockerfile.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list