Zerg
Zerg

Reputation: 789

Accesing another service using its url from inside the docker-compose network

I am hosting 3 services using docker-compose.

    version: '3.3'

services:

  service-a:
    container_name: service-a
    network_mode: default
    ports:
      - 8001:8001
      - 8080:8080


  service-b:
    container_name: service-b
    network_mode: default
    ports:
      - 8180:8080 
    links:
      - service-a:srv_a


  service-api:
    container_name: service-api
    environment: 
      - SERVER_URL=http://localhost:8180/myserver
     - 8001:8001
    links:
      - service-b: srv_b     

However the service-api which is a spring boot application can't access the service-b despite the link.

I can do that when using the browser.

What can I do to investigate the reasons for the lack of connectivity?

Should the link be somehow used in the server_url variable?

Upvotes: 9

Views: 18778

Answers (1)

Thomasleveil
Thomasleveil

Reputation: 103915

Each Docker container has it's own IP address. From the service-api container perspective, localhost resolve to its own IP address.

Docker-compose provides your containers with the ability to resolve other containers IP addresses from the docker compose service names.

Try:

  service-api:
    environment: 
      - SERVER_URL=http://service-b:8080/myserver

note that you need to connect to the container internal port (8080) and not the matching port published on the docker host (8180).

Upvotes: 31

Related Questions