Web Server and Proxy Configuration
This section outlines how to configure web servers and proxy servers within Docker Compose projects.
Web Server Deployment
Docker Compose simplifies the deployment of web servers like Nginx and Apache.
Example: Nginx web server
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
- The
image
directive specifies the Nginx image. - The
ports
directive maps the container’s port 80 to the host’s port 80. - The
volumes
directive mounts a custom configuration file (nginx.conf
) into the container.
Example: Apache web server
version: "3.9"
services:
web:
image: httpd:latest
ports:
- "80:80"
volumes:
- ./apache.conf:/etc/httpd/conf/httpd.conf
- The
image
directive specifies the Apache image. - The
ports
directive maps the container’s port 80 to the host’s port 80. - The
volumes
directive mounts a custom configuration file (apache.conf
) into the container.
Reverse Proxy Configuration
Reverse proxies are essential for load balancing, security, and routing requests to different backend services.
Example: Nginx reverse proxy with load balancing
version: "3.9"
services:
proxy:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./proxy.conf:/etc/nginx/conf.d/default.conf
backend1:
image: myapp:latest
ports:
- "8080:8080"
backend2:
image: myapp:latest
ports:
- "8081:8081"
proxy.conf
would define the upstream servers and the load balancing algorithm (e.g., round robin).
Example: Nginx reverse proxy with SSL/TLS
version: "3.9"
services:
proxy:
image: nginx:latest
ports:
- "443:443"
volumes:
- ./ssl.conf:/etc/nginx/conf.d/default.conf
environment:
- NGINX_SSL_CERTIFICATE=/etc/nginx/ssl/cert.pem
- NGINX_SSL_CERTIFICATE_KEY=/etc/nginx/ssl/key.pem
backend:
image: myapp:latest
ports:
- "8080:8080"
ssl.conf
would configure the SSL/TLS certificate and key.
SSL/TLS Certificate Implementation
Docker Compose allows integrating SSL/TLS certificates for secure communication.
Example: Using a Let’s Encrypt certificate
version: "3.9"
services:
proxy:
image: nginx:latest
ports:
- "443:443"
volumes:
- ./ssl.conf:/etc/nginx/conf.d/default.conf
- /var/run/letsencrypt:/var/run/letsencrypt
command: ["/bin/bash", "-c", "certbot certonly --agree-tos --standalone -d example.com && nginx -g 'daemon off;'"]
backend:
image: myapp:latest
ports:
- "8080:8080"
- This example utilizes the
certbot
tool to automatically obtain and install a Let’s Encrypt certificate.