HAProxy + Docker Setup Guide
HAProxy is really easy to run in docker. Let’s start by pulling the official docker image: docker pull haproxy.
Create a dockerfile in your working directory. Our dockerfile is going to be very simple. Pull HAProxy and copy in our configuration file. Here’s an example:
FROM haproxy:latest # you can also pin a specific version here COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
That’s all there is to it. Very simple and straightforward.
Setup HAProxy Config
Next, create an haproxy.cfg file. Below is a sample you can start from. You can see our HAProxy configuration guide for more specific use-cases and setup instructions.
global log /log local0 maxconn 4000 defaults mode http log global timeout client 30s timeout connect 10s timeout server 240s frontend ft_web bind *:80 mode http default_backend bk_web backend bk_web balance leastconn server web_1 10.0.0.1:80 check server web_2 10.0.0.2:80 check
Running HAProxy in Docker
Next, build and run your container:
docker build -t lb-haproxy . docker run -d --name haproxy --sysctl net.ipv4.ip_unprivileged_port_start=0 -p 80:80 lb-haproxy
You can now send requests to port 80 on the host machine and they will be routed by HAProxy to your 2 backends.