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
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 .
The Alice and Bob that you named in the docker-compose were docker name (docker run --name) not the hostname of the dockers; I would request you to defined "hostname" key in the docker-compose file to define hostname of the dockers; See example below:
version: "3.0"
services:
lab1:
image: ubuntu:version0
container_name: lab1
entrypoint: /bin/bash
stdin_open: true
hostname: lab1
lab2:
image: ubuntu:version0
container_name: lab2
entrypoint: /bin/bash
stdin_open: true
hostname: lab2
Once you defined 'hostname' in the docker-compose file then try to ping the containers using hostname; it should succeed first.
Coming next to making use of port; You binded Alice port 5000 to host port 5556 and Bob port 5000 to host port 5557; From host to reach to the specific docker port you need to use the port 5556 or 5557 to reach Alice or Bob containers respectively whereas if you want to reach the port of the container inside container itself then you need to use the actual port that was used by the containers to communicate; that is port 5556 or 5557 to reach Alice or Bob respectively from containers.
来源:https://stackoverflow.com/questions/47648792/communicating-between-different-docker-services-in-docker-compose