This project demonstrates using Dockerode with custom headers to maintain sticky sessions to Docker daemons through HAProxy.
- Four Docker daemons running in separate containers
- HAProxy for load balancing and maintaining sticky sessions
- Node.js client using Dockerode with custom headers
-
Ensure you have Docker installed on your system.
-
Clone this repository and navigate to the project directory.
-
Build and start the Docker Compose stack:
docker compose up sut --build
-
The Node.js client will automatically start and attempt to list containers every 5 seconds.
-
You should see in the logs that each client consistently connects to the same Docker daemon.
- The Node.js client generates a unique client ID and sends it as a custom header (
X-Balena-Build-ID
) with each request. - The Node.js client selects a build group, either A or B, and sends it as a custom header (
X-Balena-Build-Group
) with each request. - HAProxy uses the build group header to select a backend group, like a subset of Docker daemons.
- HAProxy uses the build ID header to maintain sticky sessions, ensuring that each client always connects to the same Docker daemon.
- You can verify this by checking the logs and seeing that each client ID consistently lists the same server.
You can modify the nodejs-client/index.js
file to perform different Docker operations or change the frequency of requests. After making changes, rebuild and restart the stack:
docker compose up sut --build
This setup demonstrates that HAProxy can maintain sticky connections between Dockerode clients and Docker daemons based on a custom header.