0

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

Community
  • 1
  • 1
Schwarz54
  • 964
  • 1
  • 9
  • 18

1 Answers1

0

Where did you get the name mymongo from? You have defined the name of mongodb service as db in your compose file. So use the connection string 'mongodb://db:27017/dbtest'

version: "3.7"
services:
   db:        ---------------> This is the name of your mongo service
    image: mongo
    ports:
        - 27017:27017
    networks:
        - testing
   app:
    build:
        context: .
        dockerfile: Dockerfile
    networks: 
        - testing
networks: 
    testing:
Shashank V
  • 10,007
  • 2
  • 25
  • 41
  • still not working doing that, `MongoNetworkError: connect ECONNREFUSED 172.17.0.3:27017`, its only work me with the Gateway of network. – Schwarz54 Jan 31 '20 at 08:38
  • Where is the error coming from? This should be used from inside your app container. – Shashank V Jan 31 '20 at 12:12
  • I use this url `'mongodb://db:27017/dbtest'` (your answer) to connect (with mongoose) my app to database , and console still show me the error of the last comment. Only works if I use `'mongodb://172.17.0.1:27017/dbtest'`, that is the Gateway of the network. – Schwarz54 Jan 31 '20 at 12:21
  • So you are getting this error after you run `docker compose up`? – Shashank V Jan 31 '20 at 12:23
  • Yes, connection between database and app works only if I put the network gateway, and I want to know if this is correct and why other solutions like yours dont work to me. – Schwarz54 Jan 31 '20 at 12:26
  • Are you sure mongo container is up before the app is trying to connect to it? Can you declare that your `app` service depends on `mongo` using `depends_on` field in compose file and check if it works. You might also want to add a retry in case mongo takes time to boot up. - https://docs.docker.com/compose/compose-file/#depends_on – Shashank V Jan 31 '20 at 14:26