docker-compose: connection refused between containers, but service accessible from host
When you bind ports like this '9002:6379'
you're telling Docker to forward traffic from localhost:9002
-> redis:6379
. That's why this works from your host machine:
redis-cli -h localhost -p 9002 -n 0
However, when containers talk to each other, they are all connected to the same network by default (the Docker bridge or docker0
). By default, containers can communicate with each other freely on this network, without needing any ports opened. Within this network, your redis
container is listening for traffic on it's usual port (6379
), host isn't involved at all. That's why your container to container communication works on 6379
.