0
votes

I have two containers linked together by Docker compose and I can successfully ping the container containing MongoDB from the one containing my node.js app using the hostname. However, when it comes to actually connecting to the database, node.js tells me the connection is refused. I am using Docker for Mac if that helps.


    mongoose.connect("mongodb://mongo", {server: {auto_reconnect: true}}).catch(error => {
            console.log("DB CONNECTION ERROR");
            console.log(error)
        });


Here is my docker-compose.yml file:

    version: "3"

    services:
      mongo:
        image: mongo:latest
        volumes:
          - ./db:/data/db
        restart: always
        expose:
          - 27017
        ports:
          - 27017:27017
        container_name: mongo

      goose:
        depends_on:
          - mongo
        build: .
        volumes:
          - ./app/server/templates:/usr/src/app/app/server/templates
        expose:
          - 3005
        restart: always
        ports:
          - 3005:3005
        container_name: goose-compose

3
Can you quote the actual error message, for clarity? Are you sure the MongoDB container has started up and isn't in an initializing state (or an initialize-and-crash loop)? - David Maze
@DavidMaze, it gives me a econnrefused error. - jamestheasiangenius

3 Answers

0
votes

Expose ports without publishing them to the host machine - they’ll only be accessible to linked services. Only the internal port can be specified.

src: docker docs

If you want to take this approach, using bridge network mode (which is a default, as far as I remember) and expose, you have to use links, which will be deprecated very soon (or already are), or network (or networks - I don't remember correctly) property.

It is also worth to take a look at networking in compose part of docs.

[edit]
Btw, expose might even be omitted, since you are mapping containers' ports to host anyways (I think). Please do correct me with this one if I'm wrong.

Cheers.

0
votes

I was also facing the same issue with spring boot app and mongo db on windows. Try providing the mongo uri in Dockerfile in the format: data.mongodb.uri=mongodb://:27017/ under ENTRYPOINT.

Hope this helps.

0
votes

I have a similarly built app using flask and mongodb in two separate containers. One difference that I notice between your compose and mine, is that you have volumes mentioned for each container, but no volume definition, like so:

version: "3"
services:
  web:
    image: jzakilla/bookfinderpy
    command: nginx -g "daemon off;"
    container_name: webapp
    ports:
      - 80:80
      - 443:443 # expose internal container ports to host
  db:
    image: mongo
    container_name: mongodb
    ports:
      - 27017:27017
    volumes:
      - book_db:/data/db
volumes:
  book_db:
    driver: local  

Without a volume section, and a driver to tell the container how to access the data I would imagine the container would spin up, but not actually be able to start the mongo service, thus giving you a denied error.