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
2
Answers
Your setup looks like this:
Try the following
docker-compose.yml
file for a proper networking and communication between thewebapp
andlb
services: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
webnet
and 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/haproxy
container is correctly set up to route traffic to thewebapp
service. 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:80
or*:80
to 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.cfg
file into a standard HAProxy container, replacingdockercloud/haproxy
for debugging purposes:In
haproxy.cfg
, explicitly define the backend to point to yourwebapp
service, making sure it matches the service name and port within the Docker network:I think there are some issues with your docker file.
links
is no more used and is dropped by dockers nowdepends_on
is to be used.Additionally you may be required to use a custom
haproxy.cfg