Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Running a static site on Apache Server from Docker
Docker containers are widely used in development workflows for building, distributing, testing, and deploying applications. They enable developer collaboration, version management, and local testing by hosting applications on containerized servers.
This article demonstrates how to run a static website on an Apache server inside a Docker container and access it through your local browser. A static website consists of HTML, CSS, and minimal JavaScript without dynamic content rendering.
Project Structure
First, create the following directory structure −
project-folder/
??? Dockerfile
??? mysite/
??? index.html
Creating the Static Website
Create a simple HTML file in the mysite directory −
mysite/index.html
<html>
<head>
<title>TutorialsPoint Docker Demo</title>
</head>
<body>
<h1>Welcome to TutorialsPoint Docker Tutorials</h1>
<p>This static site is running on Apache server inside a Docker container.</p>
</body>
</html>
Creating the Dockerfile
The Dockerfile uses the httpd:2.4 base image, which contains Apache HTTP Server pre-installed −
FROM httpd:2.4 WORKDIR /usr/local/apache2/htdocs/ COPY ./mysite/ ./ EXPOSE 80
This Dockerfile performs the following operations −
FROM httpd:2.4− Pulls the Apache HTTP Server base imageWORKDIR− Sets the working directory to Apache's document rootCOPY− Copies website files frommysite/to the containerEXPOSE 80− Documents that the container listens on port 80
Building and Running the Container
Step 1: Build the Docker Image
sudo docker build -t static-site .
This command builds a Docker image named static-site using the current directory as build context.
Step 2: Run the Container
sudo docker run -p 8080:80 --name static-container static-site
The -p 8080:80 flag maps port 8080 on your local machine to port 80 inside the container. The --name option assigns a name to the running container.
Step 3: Access the Website
Open your browser and navigate to http://localhost:8080. You should see your static website content served by the Apache server running inside the Docker container.
Container Management Commands
Additional useful commands for managing your container −
# Run container in background (detached mode) sudo docker run -d -p 8080:80 --name static-container static-site # Stop the container sudo docker stop static-container # Start the container again sudo docker start static-container # View running containers sudo docker ps # Remove the container sudo docker rm static-container
Advantages
Portability − Container runs consistently across different environments
Isolation − Website runs in isolated environment without affecting host system
Quick Setup − Apache server setup is automated through Docker
Version Control − Docker images can be versioned and distributed easily
Common Use Cases
Testing static websites before deployment
Creating portable development environments
Serving documentation sites
Prototyping web applications
Conclusion
Docker containers provide an efficient way to serve static websites using Apache HTTP Server. This approach offers portability, isolation, and simplified deployment. When you modify the HTML files and rebuild the image, changes are immediately reflected in the served website, making it ideal for development and testing workflows.
