How do I run Apache 2 on Alpine in Docker?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Running Apache HTTP Server (httpd) on Alpine Linux in Docker produces a lightweight web server container — typically 50-60 MB compared to 200+ MB for Debian-based images. Alpine uses apk for package management and the package is named apache2, not httpd. The key differences from Debian-based setups are the package names, config file locations, and the need to run Apache in the foreground for Docker.
Basic Dockerfile
Build and run:
With Custom HTML Content
With PHP Support
With SSL/TLS (HTTPS)
Custom Apache Configuration
The default config location on Alpine is /etc/apache2/httpd.conf:
Example custom virtual host (my-site.conf):
Alpine vs Debian Apache Paths
| Item | Alpine | Debian/Ubuntu |
| Package name | apache2 | apache2 |
| Binary | httpd | apache2 or apache2ctl |
| Main config | /etc/apache2/httpd.conf | /etc/apache2/apache2.conf |
| Site configs | /etc/apache2/conf.d/ | /etc/apache2/sites-available/ |
| Document root | /var/www/localhost/htdocs/ | /var/www/html/ |
| Modules | /usr/lib/apache2/ | /usr/lib/apache2/modules/ |
| PID file | /run/apache2/httpd.pid | /run/apache2/apache2.pid |
Using the Official httpd Image
Docker Hub provides an official httpd image (Debian-based), and an Alpine variant:
The official image is pre-configured and production-ready. Use it unless you need custom Alpine packages.
Docker Compose
Mount the content directory as a volume for development — changes are reflected without rebuilding.
Enabling Modules
Common Pitfalls
- Missing
/run/apache2directory: Apache on Alpine requires this directory to store the PID file. Withoutmkdir -p /run/apache2, Apache fails to start with a cryptic error about not being able to open the PID file. - Not running in foreground: Docker containers exit when the main process exits. Apache must run with
-D FOREGROUNDto stay in the foreground. Without this flag, Apache forks to the background and Docker thinks the container is done. - Package name confusion: On Alpine, the package is
apache2but the binary ishttpd. On Debian, both the package and the common command areapache2. - PHP version in package names: Alpine uses versioned PHP packages (
php83, notphp). The available version depends on your Alpine release. Check withapk search phpto find the correct package names. - Log output: By default, Apache logs to files in
/var/log/apache2/. For Docker, redirect logs to stdout/stderr by symlinking:ln -sf /dev/stdout /var/log/apache2/access.log && ln -sf /dev/stderr /var/log/apache2/error.log.
Summary
- Install Apache on Alpine with
apk add --no-cache apache2 - Create
/run/apache2directory for the PID file - Run with
httpd -D FOREGROUNDto keep the container alive - Document root is
/var/www/localhost/htdocs/(not/var/www/html/) - Use
httpd:2.4-alpinefrom Docker Hub for a pre-configured Alpine Apache image - Add
-v ./html:/var/www/localhost/htdocs:rofor development volume mounts
Related reading
- How do I run private docker images on Google Container Engine
- How do I scale up my cluster on Google Container Engine / Kubernetes?
- How do I set environment variables during the docker build process?
- How do I set hostname in docker-compose?
- How do I specify nvidia runtime from docker-compose.yml?
- How do I specify the model_config_file variable to tensorflow-serving in docker-compose?
- How do I start tensorflow docker jupyter notebook
- How do I tell if my container is running inside a Kubernetes cluster?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.