hY8vVpf3tyR57Xib
hY8vVpf3tyR57Xib

Reputation: 3905

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

Upvotes: 15

Views: 33184

Answers (3)

Deepak Bajaj
Deepak Bajaj

Reputation: 232

Incase you want your service to communicate via docker compose, Please follow the below

Since they are already in the same network you may use :- http://<Service_Name>:InternalPortNumber

  1. Replace the Service_Name with the Name mentioned in your docker compose file. For Example alice in the above question

  2. Replace the InternalPortNumber with the Port number exposed in your docker file. For Example 5000 in the above question

Upvotes: 0

Viswesn
Viswesn

Reputation: 4880

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.

Upvotes: 2

yamenk
yamenk

Reputation: 51738

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 .

Upvotes: 30

Related Questions