Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating between different docker services in docker-compose

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
like image 937
hY8vVpf3tyR57Xib Avatar asked Dec 05 '17 08:12

hY8vVpf3tyR57Xib


People also ask

How do you communicate between containers in Docker compose?

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.

Can 2 Docker containers talk to 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.


1 Answers

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 .

like image 102
yamenk Avatar answered Sep 19 '22 19:09

yamenk