I'm new to Docker and have been trying to get a simple docker-compose setup working However, I'm facing an issue:
although I can directly access the web application on the exposed port 82, I can't seem to reach it via HAProxy on the exposed port 81. I'm currently using Docker version 25.0.3.
Can anyone help me figure out what I might be missing in my configuration?
version: '3'
services:
webapp:
image: httpd
ports:
- 82:80
lb:
image: 'dockercloud/haproxy:latest'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
links:
- webapp
ports:
- 81:80
Your setup looks like this:
Try the following
docker-compose.ymlfile for a proper networking and communication between thewebappandlbservices:As suggested, the
links:option is obsolete and not necessary for modern Docker networking. Containers in the same network can discover and communicate with each other by service names.This defines a custom network
webnetand assigned both services to it. That makes sure they can communicate using the service name as the hostname.Make sure your HAProxy configuration within the
dockercloud/haproxycontainer is correctly set up to route traffic to thewebappservice. HAProxy needs to know the correct service name and port to forward requests appropriately.And check that HAProxy is configured to listen on
0.0.0.0:80or*:80to make sure it accepts connections from all IP addresses, at least for testing.If auto-discovery is failing and you have specific routing needs, consider manually specifying your HAProxy configuration. You can mount a custom
haproxy.cfgfile into a standard HAProxy container, replacingdockercloud/haproxyfor debugging purposes:In
haproxy.cfg, explicitly define the backend to point to yourwebappservice, making sure it matches the service name and port within the Docker network:Then make sure all your containers (especially the
webappservice) have the correct labels thatdockercloud/haproxyexpects for auto-discovery. And check the logs of the HAProxy container for any errors or warnings that might indicate why it is not correctly identifying and configuring backends based on the running containers. ()docker logs [haproxy-container-id]to check the logs).If the auto-discovery feature is not working as expected with the
dockercloud/haproxyimage, consider two alternatives:dockercloud/haproxyimage with a custom Dockerfile, where you can adjust or replace the startup script to better handle your discovery requirements or fix issues with the current discovery mechanism.As a compromise between fully manual configuration and dynamic auto-discovery, you can use a simple automation script that updates the HAProxy configuration based on the current state of running containers. That would involve:
writing a script that uses Docker CLI commands (e.g.,
docker container ls) to discover running containers and generate an HAProxy configuration file based on this information.scheduling the script at regular intervals or triggering it based on specific events (e.g., container start/stop) to keep the HAProxy configuration updated.
If you are running in a more complex environment and need robust service discovery and load balancing, consider Docker Swarm Mode, or even Kubernetes.