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.