Docker Community Forums

Share and learn in the Docker community.

Unable to communicate within the docker containers

#1

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.

db.datasource.json:

{
  "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.

rest-datasource.json:

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

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

docker-compose.yml file is as follows:

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

Issue:
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 127.0.0.1:4000
todo-list | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
todo-list | Connection fails: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

I have also tried modifying host from “localhost” to “127.0.0.1” 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.
(Nitishmowall) #2

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.

(Tekki) #3

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.

#4

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.

#5

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.

(Tekki) #6

Are you sure you have read my comment above?