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.

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“.

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.

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!