I was testing things for my own and I had problem: Trying to connect a nodeexpress container(app) to a mongo container(database), I can connect to mongo from MongoCompose if I connect to localhost:27017
but cant into the container of nodeexpress with mongoose configuration url to connect database like this 'mongodb://localhost:27017/dbtest'
.
So I look up at SO some solutions (like this) and answers what I see was instead of 'mongodb://localhost:27017/dbtest'
I need to write the name of the my container 'mongodb://mymongo:27017/dbtest'
, but for me this didnt work, only recieve ECONNREFUSED error.
Containers was in the same network, here is my dockerfile and docker-compose file.
Dockerfile
#node 8.16.2
FROM node:8.16.2
COPY . /app
WORKDIR /app
RUN npm install
EXPOSE 3000
CMD ["npm","start"]
docker-compose.yaml
version: "3.7"
services:
db:
image: mongo
ports:
- 27017:27017
networks:
- testing
app:
build:
context: .
dockerfile: Dockerfile
networks:
- testing
networks:
testing:
I solved this problem like this mongodb://172.17.0.1:27017/dbtest
where 172.17.0.1
is the Gateway of the network that are the containers.
Can someone explain this behavior and if it is correct ? Platform Linux