Docker Community Forums

Share and learn in the Docker community.

Unable to communicate within the docker containers

I have a loopback-1 application which is running on port 3000, loopback-2 application running on port 4000 and a mongo database running on port 27017.

Loopback-1 has a MongoDB connector.


  "name": "myDb",
  "connector": "mongodb",
  "url": "",
  "host": "localhost",
  "port": 27017,
  "user": "",
  "password": "",
  "database": "MyDatabase"

Loopback-1 has a rest api connector to connect to loopback-2.


“name”: “samplerest”,
“connector”: “rest”,

“operations”: [
“template”: {
“method”: “GET”,
“url”: “http://localhost:4000/ping
“functions”: {

docker-compose.yml file is as follows:

version: “2”
container_name: todo-list
image: manjunathsubra/todo-list
restart: always
build: ./loopback4-mongodb-rest-testing/todo-list
- “3000:3000”
- mongo
- loopback4-basic
- mongo
- loopback4-basic
container_name: loopback4-basic
image: manjunathsubra/loopback4-basic
restart: always
build: ./loopback4-getting-started
- “4000:4000”
container_name: mongo
image: mongo
- ./data:/data/db
- “27017:27017”

Docker container which runs loopback-1 is unable to access either loopback-2 or mongo.
Error thrown from the container:

todo-list | Unhandled error in GET /ping: 500 Error: connect ECONNREFUSED
todo-list | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
todo-list | Connection fails: MongoNetworkError: connect ECONNREFUSED

I have also tried modifying host from “localhost” to “” in all datasources, same error was thrown.

Could you please help in identifying is there any way to communicate within the docker containers.

Solutions I have tried:

  • Modify the host to be “container name” in all my datasources. This worked when I tried 'docker-compose up --build", however this solution did not work when I tried after pulling and running the images from docker hub.
  • I have tried with modifying host to be IP Address of my machine. It works perfectly. But I feel this solution is not a valid one.

hey So this solution is the valid one
as when set your host to localhost, it tries to connect inside the container where service is not running at port.

and another solution can be you can create a docker network and make all containers part of that network and then put host as container name of the container to access the service of.

docker-compose automatically creates a private network for these services. They find each other with their names, so for example mongo is at mongo:27017 (not localhost:27017). You only have to publish the ports you need from the outside.

1 Like

Is there any to configure IP addresses dynamically as and when I run docker images? Because we don’t want to have a static IP used anywhere in the project.

1 Like

The point is - I am thinking from the production perspective.
I had used docker-compose in the development and created images for my services. I have pushed those images to docker hub.
If I have to deploy those images on other machine, I will pull those images and start with running the containers for each of the images.
The IP address provided while development and the one which in the production differs.
I need to find a way to dynamically configure IP addresses while running the docker images.

Are you sure you have read my comment above?

1 Like