
- Docker - Home
- Docker - Overview
- Docker - Installing on Linux
- Docker - Installation
- Docker - Hub
- Docker - Images
- Docker - Containers
- Docker - Registries
- Docker - Compose
- Docker - Working With Containers
- Docker - Architecture
- Docker - Layers
- Docker - Container & Hosts
- Docker - Configuration
- Docker - Containers & Shells
- Docker - Dockerfile
- Docker - Building Files
- Docker - Public Repositories
- Docker - Managing Ports
- Docker - Web Server
- Docker - Commands
- Docker - Container Linking
- Docker - Data Storage
- Docker - Volumes
- Docker - Networking
- Docker - Security
- Docker - Toolbox
- Docker - Cloud
- Docker - Build Cloud
- Docker - Logging
- Docker - Continuous Integration
- Docker - Kubernetes Architecture
- Docker - Working of Kubernetes
- Docker - Generative AI
- Docker - Hosting
- Docker - Best Practices
- Docker - Setting Node.js
- Docker - Setting MongoDB
- Docker - Setting NGINX
- Docker - Setting ASP.Net
- Docker - Setting MySQL
- Docker - Setting Go
- Docker - Setting Rust
- Docker - Setting Apache
- Docker - Setting MariaDB
- Docker - Setting Jupyter
- Docker - Setting Portainer
- Docker - Setting Rstudio
- Docker - Setting Plex
- Docker Setting - Flame
- Docker Setting - PostgreSql
- Docker Setting - Mosquitto
- Docker Setting - Grafana
- Docker Setting - Nextcloud
- Docker Setting - Pawns
- Docker Setting - Ubuntu
- Docker Setting - RabbitMQ
- Docker - Setting Python
- Docker - Setting Java
- Docker - Setting Redis
- Docker - Setting Alpine
- Docker - Setting BusyBox
- Docker Setting - Traefik
- Docker Setting - WordPress
- Docker Useful Resources
- Docker - Quick Guide
- Docker - Useful Resources
- Docker - Discussion
Building a Web Server Docker File
Docker web servers are just web servers that are packaged in Docker containers. During containerization, most Docker containers bundle with an operating system, a runtime environment, web server software like Nginx or Apache, and your application code to run your web server. This provides a uniform portable environment for your web server and can be ported and executed identically regardless of the underlying infrastructure.
In this chapter, lets understand more about web servers running on Docker containers.
What is a Web Server?
A web server is a combination of a software and hardware designed to provide web content in response to an end-user request. It, therefore, runs particular programs that deal with incoming requests from clients i.e. browsers, by responding to the request with the desired web page or other resources like images, videos, or files. The hardware component involves a physical computer or a virtual machine on which the software, static files, and associated databases are kept.
A request goes to the web server associated with the domain when a user types a website's address in their browser. The web server processes the request, looks for the requested web page or resource, and returns it to display the response in the requesting user's browser within this context. Web servers support a variety of protocols; the most common protocol for sending web content is HTTP - Hypertext Transfer Protocol. Nowadays, most web servers also implement other features such as security, caching, and server-side scripting to produce dynamic web content.
Popular Docker Web Servers
Docker simplifies web server deployment and eases its administration by scaling, isolation, and much more. Here are the top 5 most popular Docker web servers used by developers both in development and production environments.
Nginx
Nginx is a light, high-performance web server for creating heavily loaded projects. It is well-known for its stability, richness in features, and low resource consumption.
Key Features of Nginx
- Reverse Proxy and Load Balancing − Reverse proxy and load balancing allow the incoming traffic to be efficiently distributed across servers.
- High Performance − It is capable of handling a large number of parallel connections, and it does this with very minimal usage of resources.
- Security − It has security patches, is regularly updated, and provides rate limiting and IP blocking.
- Ease of Configuration − An easy configuration syntax that lets you easily set up and maintain.
- Scalability − Ideal for scaling web applications, both vertically and horizontally.
Apache HTTP Server
Apache HTTP Server remains one of the most well-known web servers due to its flexibility and module richness.
Key Features of Apache HTTP Server
- Modularity − Rich modules for a lot of functionalities, security, URL rewriting, and authentication.
- Cross-Platform − Runs on a variety of operating systems, making it a versatile choice for different environments.
- Community Support − Excellent support from communities with detailed documentation.
- Customizability − It is heavily customizable to meet any specific requirement using .htaccess files and configuration directives.
- Stability − Has an excellent record of stability and reliability in serving Web content.
Caddy
Caddy is the web server for the modern generation, used for automatic HTTPS, and general ease of use.
Key Features of Caddy
- Automatic HTTPS − Obtains and auto-renews TLS certificates, easing securing websites.
- Ease of Configuration − Simple configuration file format that reduces setup complexity.
- HTTP/2 Support − Native support for HTTP/2 to improve performance and user experience.
- Cross-platform − Can run on almost any existing operating system, such as Linux, macOS, and Windows.
- Integrated Reverse Proxy − It has a reverse proxy mechanism that makes the system suitable for microservices architectures.
Lighttpd
Lighttpd is a light web server but also optimized for speed-critical environments.
Key Features of Lighttpd
- Performance − Designed to be fast and consume low resources, ideal for high-performance applications.
- Small Footprint − Light on memory and CPU usage, which can be deployed on resource-constrained devices.
- Security − Supports all modern security features, including TLS, and comprises a collection of tools that protect against common web attacks.
- Easy to Use − Its application configuration and setup process are simple.
- Flexible − It could be used for everything from static sites to complex and dynamic content for a wide range of web applications.
Tomcat
Apache Tomcat is an open-source implementation of Java Servlet, JavaServer Pages, and Expression Language technologies.
Key Features of Apache Tomcat
- Java Support − Specifically designed to serve Java web applications, making it a popular choice for Java developers.
- Extensibility − It could be highly extensible to most frameworks and libraries.
- Performance − Best for running Java applications efficiently.
- Integration − It easily integrates with other Apache projects, like Apache HTTP Server.
- Robustness − Well-tested, stable, large user base, and active community behind.
The web servers that we discussed are popular in the Docker ecosystem due to their performance, flexibility, and comprehensive feature sets. They can suit different needs and requirements, which makes the task of finding the right solution for specific needs much easier.
How to Run an Apache HTTP Server in Docker?
You can easily run an Apache HTTP Server in Docker. This requires pulling the Apache image from Docker Hub, creating a container, and configuring the server. Here is a step-by-step guide to get you started.
Step 1: Install Docker
You can get started by downloading and installing Docker from Docker's official website.
Step 2: Pull the Apache HTTP Server Image
Fire up a terminal and run the below command to pull the official Apache HTTP Server Docker image from Docker Hub.
$ docker pull httpd

Step 3: Create a Directory for Your Website
To store the website files, create a directory on your local machine. For example −
$ mkdir ~/my-apache-website $ cd ~/my-apache-website
Step 4: Create a Simple HTML File
You need an HTML file to serve. You can do so by creating an index.html file in your website directory to test the server.
echo "<!DOCTYPE html><html><body><h1>Hello from Apache!</h1></body></html>" > index.html

Step 5: Run the Apache Container
You can run a Docker container from the Apache HTTP Server image. You will also have to mount your website directory to the container.
$ docker run -dit --name my-apache-server -p 8080:80 -v ~/my-apache-website:/usr/local/apache2/htdocs/ httpd:latest

Heres what each part of the above command does −
- -dit − These flags are used to run a container in detached mode (in the background) and interactive terminal.
- --name my-apache-server − You can use this option to name the container my-apache-server.
- -p 8080:80 − If you want to access the server from your local browser, you can map port 8080 on your local machine to port 80 in the container.
- -v ~/my-apache-website:/usr/local/apache2/htdocs/ − In order to mount the local website directory to the containers Apache document root, you can use the -v volume flag.
- httpd:latest − This specifies the image to be used to create the container (Apache HTTP Server, latest version).
Step 6: Verify the Apache Server is Running
Once your container is up and running, you can open a web browser and navigate to http://localhost:8080. You should be able to see the content of the index html file, which in this case is - "Hello from Apache!".
Step 7: Manage the Apache Container
If you want to manage your running container, you can use the below Docker commands.
Stop the Container
To stop the container −
$ docker stop my-apache-server

Start the Container
To start the container again −
$ docker start my-apache-server

View Container Logs
To view the logs for the Apache server −
$ docker logs my-apache-server

Remove the Container
To remove the container when you no longer need it −
$ docker rm my-apache-server

Step 8: Customize Apache Configuration (Optional)
In case you want to customize the Apache configuration, you can create a custom configuration file and mount it to the container. For example, create a custom httpd.conf file −
$ mkdir ~/my-apache-config $ cp /path/to/original/httpd.conf ~/my-apache-config/httpd.conf
Then run the container with the custom configuration −
$ docker run -dit --name my-apache-server -p 8080:80 -v ~/my-apache-website:/usr/local/apache2/htdocs/ -v ~/my-apache-config/httpd.conf:/usr/local/apache2/conf/httpd.conf httpd:latest
Which is the best web server compatible with Docker Containers?
Its difficult to identify a single 'best' web server compatible with Docker containers since each of them has its specific requirements and needs for accomplishing specific tasks related to a project. The widely used ones, Nginx and Apache, also work well with Docker and are known for their rich set of features. Applications that require high performance and low resource usage can be served by Nginx and it is also very suitable for static content and load balancing. In contrast, Apache is feature-rich and flexible, suitable for higher-level applications where dynamic web content needs to be used with extensive module support. Other options like Caddy and Traefik are equally getting popular due to their ease of use and very modern features.
Conclusion
In this chapter, we have discussed how to create and run a web server in a Docker container. We have understood the various Docker commands needed to work with web servers in containers. You can follow the steps mentioned in this guide to quickly set up an Apache server, manage your website files, and configure them according to your needs.