How to Setup a Simple Apache Web Server in a Docker Container

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.
Date: 2020-04-06
Setting Up a Static Website with Apache and Docker: A Comprehensive Guide
In today's digital landscape, deploying and managing web applications efficiently is crucial. Docker, a containerization technology, simplifies this process significantly. This article explains how to host a simple static website using Apache web server within a Docker container, providing a clear, step-by-step guide for beginners.
Understanding the Components: Docker, Apache, and Static Websites
Before diving into the process, let's briefly understand the key components involved. Docker is a platform that packages applications and their dependencies into self-contained units called containers. These containers ensure consistent execution across different environments, making deployment and scaling much easier. Apache is a widely used open-source web server that handles requests for web pages. A static website, in contrast to a dynamic one, contains content that doesn't change frequently and doesn't require server-side processing; the pages are simply served directly from the server's file system.
Creating the Foundation: The Dockerfile
To deploy our static website within a Docker container using Apache, we begin by creating a file called Dockerfile. This file acts as a set of instructions for Docker to build the container image. This image will encapsulate the Apache web server and our website's files. Think of the Dockerfile as a recipe that outlines precisely how Docker should assemble the necessary components. The actual content of the Dockerfile would specify which base image (a pre-built image containing a basic operating system and necessary software) to use—likely an image with Apache pre-installed. It would then outline the steps to copy our website's files into the container's designated directory accessible by the Apache server. This might involve commands to copy files from our local project directory to the designated location within the container. Finally, it might define the port that Apache will listen on within the container.
Building the Docker Image
Once the Dockerfile is created, we use the Docker build command. This command reads the instructions in Dockerfile and creates a new image. This image isn't a running application yet; it's a blueprint for a container. Imagine constructing a house; the blueprint is the image. The actual house, once built and occupied, is the container. The build process may take some time, depending on the size of the website and the speed of your internet connection, as Docker pulls down the necessary base images and compiles the necessary elements. Upon successful completion, the build command creates a new, custom image that's ready to be used.
Running the Container: Bringing Your Website to Life
With the image built, we can now run a Docker container based on that image. This involves using the Docker run command. This command takes the image we just built and creates a running instance—a container. The command would specify the image name to use, the port mapping (linking a port on the host machine to a port inside the container—usually port 80 for HTTP or 443 for HTTPS), and potentially any environment variables needed by Apache. The most important aspect is defining port mapping. This is how our local machine can access the web server running inside the container. By mapping a port on our local machine to the port that Apache listens on inside the container, we can access the website by visiting a specific address, commonly http://localhost:80 in a local development environment.
Verifying the Deployment: Accessing Your Website
Once the container is running, accessing the website should be straightforward. Simply open your web browser and navigate to the specified address (like http://localhost:80 or http://localhost:8080, if a different port was used). You should see your static website displayed, confirming that Apache is serving the content correctly within the Docker container. If you encounter any issues, checking the Docker logs for the container might provide useful information for troubleshooting.
Understanding the Importance of Containerization
This approach using Docker offers many advantages. It promotes consistency and reproducibility. The same container, once built, should run identically on any system with Docker installed. This eliminates the common problems associated with environment-specific configurations, where code works on one machine but fails on another. It also simplifies the deployment process. Instead of setting up Apache manually on a server, we build a container, ship it to the desired server, and run it, a much more streamlined and less error-prone procedure. The isolation provided by containers improves security and stability. If one application within a container fails, it doesn't necessarily affect other applications or the underlying host operating system.
Beyond the Basics: Expanding Capabilities
While this approach covers hosting a simple static website, it's easily extensible. More complex websites might need additional components within the Docker container. This could involve adding a database, a backend application server, or other supporting services. With Docker Compose, you can orchestrate multiple containers that work together, allowing for greater complexity and sophisticated applications. In essence, you can build a complete application ecosystem within Docker containers, making the development and deployment process drastically simpler and more manageable.
Conclusion
Docker offers an elegant solution for deploying and managing web applications. By creating a Docker image that encapsulates Apache and a static website, we’ve developed a robust and portable way to deploy our web content. This approach reduces the complexity of server administration, improves consistency, and increases the overall efficiency of the development process. This method, although showcased with a static website example, readily extends to more complex applications, highlighting the power and flexibility of Docker in modern web development.