I just started working with docker-compose and am currently struggling with communication between the different services.
I have 2 services, alice
and bob
. I want these to be able to send http requests to each other. As far as I understood, services should be able to reach each other by using the servicename as hostname.
Unfortunately, alice
in my example is not able to reach bob
on http://bob:5557, and bob
is not able to reach alice
on http://alice:5556.
What am I not understanding correctly? Is it even possible to make http requests between services?
This is my docker-compose.yml file:
version: '3'
services:
alice:
build: blockchain
ports:
- "5556:5000"
environment:
NAME: Alice
bob:
build: blockchain
ports:
- "5557:5000"
environment:
NAME: Bob
For containers to communicate with other, they need to be part of the same “network”. Docker creates a virtual network called bridge by default, and connects your containers to it. In the network, containers are assigned an IP address, which they can use to address each other.
A Docker network lets your containers communicate with each other. If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network.
As clearly documented in Networking in Compose
Networked service-to-service communication use the CONTAINER_PORT
Thus you should use the container ports to communicate between the containers.
http://bob:5000
and http://alice:5000
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With