Aditya
Aditya

Reputation: 1344

Linked docker containers can't communicate with each other

I am using docker-compose to run my application stack.

The application stack is:

  1. Mongo
  2. Rest Service (referred as rest-service hereafter)
  3. UI Service (referred as ui-service hereafter)

Below is the snippet of my docker-compose:

services:
  mongodb:
    image: mongo:3
    container_name: mongodb
    ports:
      - "17027:27017"

  rest-service:
    build: ./rest-service/
    container_name: rest
    ports:
      - "15000:5000"
    command: "/opt/app/conf/config.yml"
    links:
      - mongodb:mongo

  ui-service:
    build: ./ui-service/
    container_name: ui
    ports:
     - "18080:8080"
    links:
      - rest-service:rest
    environment:
      NODE_ENV: development

The problem I am facing over here is that my rest-service can talk to mongo container (I mean on port(27017 on docker container)), since mongo is linked to restService. But ui-service cant talk to rest-service(I mean on port(5000 on docker container)).

If I try to make ui-service talk to rest-service on host port (I mean port 15000 which listens on port 5000 of docker container), it works. Hence, I am not able to understand why this happens.

Any help would be highly appreciated.

Upvotes: 3

Views: 3010

Answers (1)

enderland
enderland

Reputation: 14185

Remove the ports parts unless you want to access the port from outside this container network. Linked containers do not need to explicitly expose ports to each other and as you found out, it exposes them to the host. You need to either not expose the ports or only access the ports you are using, but through localhost:1234 syntax (not container-name:1234).

Make sure you understand how you are referencing the ports, if you are using container name you will likely need link but if you don't want to do this, will need to use localhost and the host port.

Using link automatically allow linked containers to access ports of the container. This means any port on your mongodb can be accessed via it's container port, even if it's not exposed.

When you explicitly expose them, they are exposed to your localhost for docker, which is why some of your stuff works.

Upvotes: 1

Related Questions