Set up and configure the Apache webserver in Docker.

Vaibhav ParekhVaibhav Parekh
3 min read

Setting up and configuring an Apache web server in Docker is a straightforward process. Below is a step-by-step guide that will help you get an Apache server running in a Docker container.

Step-by-Step Guide to Setting Up Apache in Docker

Prerequisites

  1. Docker Installed: Ensure Docker is installed on your machine. If you haven't installed it yet, you can follow the official installation guide for your operating system.

  2. Basic Knowledge of Docker Commands: Familiarity with basic Docker commands will be helpful.

Step 1: Pull the Apache HTTP Server Image

Open your terminal and run the following command to pull the official Apache image from Docker Hub:

docker pull httpd

Step 2: Create a Custom HTML Page (Optional)

To serve a custom HTML page, you can create an index.html file. Create a directory for your Apache web server files and create an HTML file inside it:

mkdir my-apache-server
cd my-apache-server

Create an index.html file:

echo "<h1>Welcome to My Apache Server!</h1>" > index.html

Step 3: Run the Apache Container

Run the Apache container using the following command. This command also mounts the directory containing your HTML file to the Apache document root:

docker run --name my-apache -v $(pwd)/index.html:/usr/local/apache2/htdocs/index.html -p 8080:80 -d httpd

Explanation of the command:

  • --name my-apache: Assigns a name to the container (my-apache).

  • -v $(pwd)/index.html:/usr/local/apache2/htdocs/index.html: Mounts the index.html file to the Apache document root.

  • -p 8080:80: Maps port 80 of the Apache container to port 8080 on the host machine.

  • -d: Runs the container in detached mode (in the background).

Step 4: Verify the Container is Running

You can check if the Apache container is running with the following command:

docker ps

You should see an entry for my-apache in the list of running containers.

Step 5: Access Apache

Open a web browser and navigate to http://localhost:8080. You should see the message "Welcome to My Apache Server!" confirming that your Apache server is running inside a Docker container.

Step 6: Stopping and Removing the Container

To stop the Apache container, run:

docker stop my-apache

To remove the container completely:

docker rm my-apache

Step 7: Customizing Apache Configuration (Optional)

If you want to customize the Apache configuration, you can create a custom httpd.conf file and mount it to the container.

  1. Create a custom configuration file named httpd.conf in the my-apache-server directory:

     echo "ServerName localhost" > httpd.conf
    

    (You can add more configurations as needed.)

  2. Run the container with the custom configuration:

     docker run --name my-apache -v $(pwd)/index.html:/usr/local/apache2/htdocs/index.html -v $(pwd)/httpd.conf:/usr/local/apache2/conf/httpd.conf:ro -p 8080:80 -d httpd
    

Conclusion

You have successfully set up and configured an Apache web server in Docker. This setup allows you to quickly deploy and manage your web applications in isolated environments. Docker simplifies the process of running services like Apache, enabling you to focus on development without worrying about the underlying infrastructure.

0
Subscribe to my newsletter

Read articles from Vaibhav Parekh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vaibhav Parekh
Vaibhav Parekh

I am a Cloud enthusiast . Delivering continuous learnings through blogs . You have any doubts you can contact me directly don't hesitate . We all have a start and initial days of learning !!