0

this is my docker-compose codes

version: '3'
services:
  mongo:
    container_name: "mongo"
    image: "mongo:4.4.8"
    ports:
      - "27017:27017"
  web:
    image: docker-node-mongo
    build: .
    command: "node src/index.js"
    ports:
      - 4000:4000
    depends_on:
      - "mongo"


and this is my docker file

FROM node
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 4000
CMD "node" "src/index.js"

and this is my connection file


    const mongoose = require("mongoose")
    mongoose.connect("mongodb://mongo:27017/mongo-test", {
        useNewUrlParser: true,
        useCreateIndex: true
    })


and this error occur

MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017 web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1146:16) { web_1 | name: 'MongoNetworkError' web_1 | }]

I would be thankful if you help me .

2
  • Are you sure that you are actually trying to connect tomongo:27017? From the error message, it seems localhost instead. Do you need to rebuild the docker image? Commented Aug 20, 2021 at 16:46
  • Oops , thanks for your help , I rebuild it and now it`s ok Commented Aug 20, 2021 at 16:57

1 Answer 1

0

Try this,

Dockerfile

FROM node:alpine
RUN mkdir -p /usr/src/app  # -p create all parent folders
WORKDIR /usr/src/app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . .

docker-compose.yml

version: "3.8"

services:
  web:
    image: docker-node-mongo
    container_name: docker-node-mongo
    build: .  # path to your Dockerfile
    command: nodemon src/app.js # Use nodemon to reload on changes
    restart: always
    volumes:
      - ./src:/usr/src/app # To enable auto reload on changes
    ports:
      - "4000:4000"
    depends_on:
      - mongo_db

  mongo_db:
    image: mongo
    restart: always
    container_name: mongo_container # Use this in the connection url
    command: mongod -port 27017 --dbpath /data/db --replSet rs0
    ports:
      - "27017:27017"
    volumes:
      - mongo_storage:/data/db

volumes:
  mongo_storage:

Connection file

 const mongoose = require("mongoose")
 mongoose.connect("mongodb://mongo_container:27017/mongo-test?replicaSet=rs0", {
    useNewUrlParser: true,
    useCreateIndex: true,
 })

To enable replica set in mongodb,

docker compose up --build -d # to build your app in docker

docker exec -it mongo_db mongo # to enter in the mongo shell in docker

rs.initiate({ _id: "rs0", version: 1, members: [{ _id: 0, host : "mongo_db:27017" }] })

Please use this as a pointer to how to set up Node and MongoDB with Docker.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.