Docker Task: Set Up and Configure Apache Webserver in Docker
In this task, we’ll walk through the steps to set up and configure an Apache web server using Docker. This task is aimed at demonstrating how to containerize a simple web server, making it easy to deploy and manage in any environment.
Prerequisites
Before we get started, ensure that you have:
- Docker installed on your system
- Basic knowledge of Docker commands
Step 1: Create a Dockerfile
A Dockerfile
defines the environment in which our Apache web server will run. Let's create one to set up Apache.
- Create a new directory for your project:
mkdir apache-docker
cd apache-docker
2. Create a Dockerfile
:
touch Dockerfile
3. Add the following content to your Dockerfile
:
# Use the official Ubuntu image as the base
FROM ubuntu:latest
# Install Apache web server
RUN apt-get update && apt-get install -y apache2
# Expose port 80 to allow external traffic
EXPOSE 80
# Start Apache server
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
This Dockerfile does the following:
- Uses the latest Ubuntu image as the base.
- Installs Apache using
apt-get
. - Exposes port 80 (the default HTTP port).
- Configures Apache to run in the foreground.
Step 2: Create a Custom Web Page (Optional)
To customize your Apache server, you can serve your own HTML page.
- Create a directory for HTML content:
mkdir html
2. Create an index.html
file inside the html
directory:
touch html/index.html
3. Add the following content to index.html
:
<!DOCTYPE html>
<html>
<head>
<title>Apache Webserver in Docker</title>
</head>
<body>
<h1>Hello from Dockerized Apache Webserver!</h1>
</body>
</html>
Step 3: Modify the Dockerfile to Serve the Custom HTML Page
Now, let’s modify the Dockerfile
to copy the index.html
file into the Apache server directory.
- Modify the
Dockerfile
to include the following line:
# Copy custom HTML file to Apache's default directory
COPY ./html/index.html /var/www/html/index.html
The modified Dockerfile
should look like this:
# Use the official Ubuntu image as the base
FROM ubuntu:latest
# Install Apache web server
RUN apt-get update && apt-get install -y apache2
# Copy custom HTML file to Apache's default directory
COPY ./html/index.html /var/www/html/index.html
# Expose port 80 to allow external traffic
EXPOSE 80
# Start Apache server
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Step 4: Build the Docker Image
Now, let’s build the Docker image.
Run the following command in the apache-docker
directory
docker build -t apache-webserver .
This will build an image with the tag apache-webserver
.
Step 5: Run the Apache Webserver in a Docker Container
After building the image, you can run the container:
Run the container:
docker run -d -p 8080:80 apache-webserver
Step 6: Verify the Apache Server
Open your web browser and navigate to http://localhost:8080
. You should see the custom web page with the message "Hello from Dockerized Apache Webserver!"
Conclusion
You’ve successfully set up and configured an Apache web server in Docker! This simple setup can be further extended by adding more configurations or integrating it with other services like databases, proxies, etc. Docker makes it easy to encapsulate the web server into a portable and scalable container.