HAProxy Load Balancer
High-performance TCP/HTTP load balancer.
Overview
HAProxy is a mature, battle-tested load balancer that has been the cornerstone of high-availability infrastructure since 2000. Originally developed by Willy Tarreau, HAProxy excels at distributing incoming requests across multiple backend servers while providing advanced health checking, SSL termination, and traffic routing capabilities. Unlike application-specific load balancers, HAProxy operates at both Layer 4 (TCP) and Layer 7 (HTTP), making it incredibly versatile for everything from database connection pooling to HTTP request routing. This Docker implementation leverages HAProxy 2.8 on Alpine Linux, providing enterprise-grade load balancing capabilities in a lightweight container that consumes minimal resources while delivering maximum performance. The configuration exposes standard HTTP/HTTPS ports along with HAProxy's built-in statistics dashboard, enabling real-time monitoring of backend server health and traffic distribution patterns. System administrators managing high-traffic applications, database clusters, or microservices architectures will find this setup particularly valuable for its low-latency performance and granular traffic control capabilities. HAProxy's stick tables enable session persistence, while its sophisticated ACL system allows for complex routing decisions based on headers, URLs, or client characteristics, making it superior to simpler reverse proxies when advanced traffic management is required.
Key Features
- Layer 4 TCP and Layer 7 HTTP load balancing with multiple algorithms including round-robin, least connections, and source IP hashing
- Advanced health checking with customizable HTTP, TCP, and MySQL health check methods
- SSL/TLS termination with SNI support and certificate management for HTTPS traffic
- Built-in statistics dashboard accessible on port 8404 for real-time monitoring of backend servers
- Session persistence using stick tables and cookie insertion for stateful applications
- Advanced ACLs for traffic routing based on headers, URLs, source IPs, and custom conditions
- Zero-downtime configuration reloads using HUP signal without dropping existing connections
- Rate limiting and connection queuing to protect backend servers from traffic spikes
Common Use Cases
- 1High-traffic web applications requiring distribution across multiple backend web servers
- 2Database connection pooling and load balancing for PostgreSQL or MySQL clusters
- 3API gateway functionality for microservices with path-based routing and health monitoring
- 4SSL termination proxy to offload encryption processing from backend application servers
- 5Blue-green deployments with traffic switching between different application versions
- 6Geographic or datacenter-based traffic routing using HAProxy's advanced ACL capabilities
- 7Legacy application modernization by adding load balancing without code changes
Prerequisites
- Minimum 256MB RAM recommended for production workloads (64MB minimum for testing)
- Valid HAProxy configuration file at ./haproxy/haproxy.cfg with frontend and backend sections defined
- Backend servers accessible from the HAProxy container network with health check endpoints
- Ports 80, 443, and 8404 available on the host system for HTTP, HTTPS, and statistics dashboard
- Understanding of HAProxy configuration syntax including frontend, backend, and listen sections
- SSL certificates properly mounted if HTTPS termination is required
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 haproxy: 3 image: haproxy:2.8-alpine4 container_name: haproxy5 restart: unless-stopped6 volumes: 7 - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro8 ports: 9 - "80:80"10 - "443:443"11 - "8404:8404"12 networks: 13 - haproxy-network1415networks: 16 haproxy-network: 17 driver: bridge.env Template
.env
1# HAProxy stats2HAPROXY_STATS_USER=admin3HAPROXY_STATS_PASSWORD=changemeUsage Notes
- 1Docs: https://www.haproxy.com/documentation/
- 2Create haproxy/haproxy.cfg with frontend/backend sections before starting
- 3Stats dashboard at http://localhost:8404/stats (enable in config)
- 4Check config: docker exec haproxy haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg
- 5Reload without downtime: docker kill -s HUP haproxy
- 6Health checks: option httpchk GET /health in backend section
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 haproxy:5 image: haproxy:2.8-alpine6 container_name: haproxy7 restart: unless-stopped8 volumes:9 - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro10 ports:11 - "80:80"12 - "443:443"13 - "8404:8404"14 networks:15 - haproxy-network1617networks:18 haproxy-network:19 driver: bridge20EOF2122# 2. Create the .env file23cat > .env << 'EOF'24# HAProxy stats25HAPROXY_STATS_USER=admin26HAPROXY_STATS_PASSWORD=changeme27EOF2829# 3. Start the services30docker compose up -d3132# 4. View logs33docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/haproxy/run | bashTroubleshooting
- 503 Service Unavailable errors: Check backend server health status in stats dashboard at :8404/stats and verify backend servers are running and accessible
- Configuration validation fails: Run 'docker exec haproxy haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg' to check syntax errors in haproxy.cfg
- Stats dashboard not accessible: Ensure 'stats enable' and 'stats uri /stats' are configured in the frontend or dedicated stats section
- SSL certificate errors: Verify certificate files are properly mounted and accessible, and check HAProxy logs for SSL handshake failures
- Backend servers showing as DOWN: Confirm health check URLs return HTTP 200 status and backend servers are listening on configured ports
- High CPU usage during traffic spikes: Increase 'maxconn' values and consider enabling connection queuing with 'maxqueue' parameter
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download