Make docker containers talk to each other with custom network - Docker for Mac

I cannot make a Sails container and a Postgress container to communicate. ping-ing by the host names works, which means that the containers can “see” each other, but when I make calls to the database from the server app, I get connection refused error, that is why I assume the problem is in the port exposure. Even by specifically exposing the port of the Postgres container, it still doesn’t work.

If I run the server app locally on the host machine, everything works fine, which makes me assume the exposure of the DB container port is all fine.

On Docker for Mac the docker0 interface, that underlies the default bridge network, is not implemented, so I cannot use this network, to which the containers are subscribed by default. And I want the containers to communicate only internally, within the Docker env, and not use the host machine as middleware. That is why I had to create a custom network, to which to subscribe the containers.

Here is the docker-compose.yml configuration:

version: '2'

services:
  sails-test:
    build: .
    ports:
      - "8001:1337"
    volumes:
      - ./app:/app
    command: node app
    container_name: sails-server
    networks:
      - default

  postgresdata:
    image: busybox
    volumes:
      - /var/lib/postgresql
    container_name: sails-postgres-data

  postgres:
    image: postgres:latest
    ports:
      - "8002:5432"
    expose:
      - "5432"
    volumes_from:
      - postgresdata
    container_name: sails-postgres
    networks:
      - default

networks:
  default:
    external:
      name: test-network

Inspecting the test-network shows that both containers are well subscribed to it.

I need to use a custom network, but not link-ing, because I’ve got other containers, from separate compose configurations, that I subscribe to the custom network, and thus link-ing will not do the trick (since it’s useful only for containers that are spinned in the same compose network).

Any ideas how to make these guys talk to each other?

same problem +1

I even tried to map the port out, still doesn’t work.

This is my situation. I have a docker-compose.yml

version: '2'
services:
  elasticsearch:
    container_name: elasticsearch
    image: elasticsearch:5
    volumes:
      - ./elasticsearch/data:/usr/share/elasticsearch/data
      - ./elasticsearch/config:/usr/share/elasticsearch/config
    tty: true
  kibana:
    container_name: kibana
    image: kibana:5
    depends_on:
      - elasticsearch
    tty: true
  logstash:
    container_name: logstash
    image: logstash:5
    command: logstash -f /config-dir/logstash.conf
    volumes:
      - ./logstash/config:/config-dir
    depends_on:
      - elasticsearch
    tty: true

I can spin up all instances with no problem

docker-compose up -d

Now I hop into the kibana container

docker exec -it kibana bash

Within the kibana container I can curl the elastisearch container (I had to do apt-get update;apt-get install -y curl) first.

root@d0096e0f70c9:/# curl elasticsearch:9200
{
  "name" : "5v9EVRG",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "QqbR0tbHTyK4LvtGVh7zXw",
  "version" : {
    "number" : "5.0.0",
    "build_hash" : "253032b",
    "build_date" : "2016-10-26T05:11:34.737Z",
    "build_snapshot" : false,
    "lucene_version" : "6.2.0"
  },
  "tagline" : "You Know, for Search"
}

But however the kibana container logs kept complaining the connection to the http://elasticsearch:9200 is refused

| => docker logs kibana
  log   [08:18:02.347] [info][status][plugin:kibana@5.0.0] Status changed from uninitialized to green - Ready
  log   [08:18:02.760] [info][status][plugin:elasticsearch@5.0.0] Status changed from uninitialized to yellow - Waiting for Elasticsearch
  log   [08:18:02.876] [info][status][plugin:console@5.0.0] Status changed from uninitialized to green - Ready
  log   [08:18:02.996] [error][elasticsearch] Request error, retrying
HEAD http://elasticsearch:9200/ => connect ECONNREFUSED 172.18.0.2:9200
  log   [08:18:03.041] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:03.050] [warning][elasticsearch] No living connections
  log   [08:18:03.064] [error][status][plugin:elasticsearch@5.0.0] Status changed from yellow to red - Unable to connect to Elasticsearch at http://elasticsearch:9200.
  log   [08:18:03.680] [info][status][plugin:timelion@5.0.0] Status changed from uninitialized to green - Ready
  log   [08:18:03.694] [info][listening] Server running at http://0.0.0.0:5601
  log   [08:18:03.699] [error][status][ui settings] Status changed from uninitialized to red - Elasticsearch plugin is red
  log   [08:18:05.574] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:05.575] [warning][elasticsearch] No living connections
  log   [08:18:08.764] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:08.791] [warning][elasticsearch] No living connections
  log   [08:18:11.306] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:11.310] [warning][elasticsearch] No living connections
  log   [08:18:13.825] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:13.971] [warning][elasticsearch] No living connections
  log   [08:18:16.506] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:16.514] [warning][elasticsearch] No living connections
  log   [08:18:19.204] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:19.210] [warning][elasticsearch] No living connections
  log   [08:18:21.754] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:21.762] [warning][elasticsearch] No living connections
  log   [08:18:24.276] [warning][elasticsearch] Unable to revive connection: http://elasticsearch:9200/
  log   [08:18:24.278] [warning][elasticsearch] No living connections
  log   [08:18:27.884] [info][status][plugin:elasticsearch@5.0.0] Status changed from red to green - Kibana index ready
  log   [08:18:27.890] [info][status][ui settings] Status changed from red to green - Ready

This is so weird, because kibana container can definitely reach the elasticsearch container, but the kibana application itself have trouble connecting to the elasticsearch container