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.
Once created, create a new file in the directory to store the configuration as follows:
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:
# 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:
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:
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 rm public_app
Cleaning Up
To remove the Docker image that we created, run the command as follows:
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.