"ConnectionError: connect ECONNREFUSED", cannot connect Node.js app and Elasticsearch database with Docker Compose

I am trying to make my Node.js app to talk to my Elasticsearch database when running on the same bridge network in Docker. I use a docker-compose.yml file for configuration. Here are the relevant bits and the error messages I get.

docker-compose.yml

version: '3'

services:
  express-app:
    build: /path/to/app
    container_name: my_app
    depends_on:
      - 'elasticsearch'
    environment:
     - NODE_ENV=local
     - ES_HOST=elasticsearch
     - PORT=3000
    ports:
      - 3000:3000
    networks:
      - backend

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
    container_name: es-master
    environment:
      - discovery.type=single-node
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - esdata:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - backend

volumes:
  esdata:
    driver: local

networks:
  backend:

I want both services (express-app and elasticsearch to be discoverable on the Docker bridge network that is created, “my_app_backend”, with their service names. The official Docker documentation states that a “service is reachable at the hostname”. My intention is to reach the service elasticsearch at the address “http://elasticsearch:9200“.

DB connection definition inside Node.js app (server/db/index.js)

const { Client } = require('@elastic/elasticsearch');
const hostname = process.env.ES_HOST || 'localhost';
const client = new Client({
  node: `http://${hostname}:9200`,
  log: 'error'
});

async function checkConnection () {
  let isConnected = false;
  console.log('Connecting to host', hostname);
  try {
    const health = await client.cluster.health({});
    console.log(health);
    isConnected = true;
  } catch (err) {
    console.log('process.env.ES_HOST', process.env.ES_HOST);
    console.log('Connection Failedn', err);
  }
}

checkConnection();

module.exports.client = client;

The message below shows the result of the Node.js app attempting to connect to Elasticsearch via the process.env.ES_HOST variable ‘elasticsearch’ (as defined in the docker-compose.yml file) and executed when the containers run. The error message shows a couple of console logs I put to check the variables had the right values.

The IP address shown after ECONNREFUSED is what the Docker engine replaces ‘elasticsearch’ with. This can also be found when I run a docker network inspect my_app_backend which outputs the container’s network addresses.

... (redacted)
"MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""

Error message

> NODE_ENV=production node ./server/index.js

Connecting to host elasticsearch
Server listening on port 3000!
process.env.ES_HOST elasticsearch
Connection Failed
 ConnectionError: connect ECONNREFUSED 172.20.0.3:9200
    at onResponse (/src/app/node_modules/@elastic/elasticsearch/lib/Transport.js:214:13)
    at ClientRequest.<anonymous> (/src/app/node_modules/@elastic/elasticsearch/lib/Connection.js:98:9)
    at ClientRequest.emit (events.js:311:20)
    at Socket.socketErrorListener (_http_client.js:426:9)
    at Socket.emit (events.js:311:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  name: 'ConnectionError',
  meta: {
    body: null,
    statusCode: null,
    headers: null,
    warnings: null,
    meta: {
      context: null,
      request: [Object],
      name: 'elasticsearch-js',
      connection: [Object],
      attempts: 3,
      aborted: false
    }
  }
}

Also, I want to note that the apps failed to connect on start, but when I went into the app container via docker exec -it express-app sh and edited the database connection code and hardcoded the IP address of the database (172.20.0.3), then re-started the node app, the app connected to the database successfully.

What am I doing wrong in this attempt to fully automate via docker-compose? Is this a quirk of Elasticsearch or am I missing something very obvious? Thank you in advance for your help!

Source: StackOverflow