5

I have problems to connect my application which is running as a docker container to a mongoDB. I know that 'links' is deprecated for new version docker-compose, so I try use custom network. My docker-compose.yml:

version: "3.3"

services:

  app:
    container_name: app
    build: ./app
    depends_on:
      - "mongodb"
    networks:
      - my-network
    command: pm2-docker start pm2.config.js --watch
    volumes:
      - ./app/logs:/opt/app/logs
      - ./app:/opt/app
    ports:
      - "3010:80"

  mongodb:
    image: mongo
    container_name: mongodb
    restart: always
    environment:
      MONGO_INITDB_ROOT_PASSWORD: pw
      MONGO_INITDB_ROOT_USERNAME: login
      MONGO_INITDB_DATABASE: dbname
    volumes:
      - ./mongodb/db:/data/db
    ports:
      - "27017:27017"
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

Dockerfile for my app:

FROM node:10.7.0


RUN npm i -g pm2
RUN pm2 install pm2-logrotate \
    && pm2 set pm2-logrotate:max_size 50M \
    && pm2 set pm2-logrotate:compress true \
    && pm2 set pm2-logrotate:workerInterval 60

RUN mkdir -p /tmp
COPY package*.json /tmp/
RUN cd /tmp \
    && npm i \
    && npm cache clean --force

RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
WORKDIR /opt/app
COPY . /opt/app/
VOLUME [ "/opt/app/logs/" ]

ENV PORT 80
ENV MONGODB_AUTH mongodb://login:[email protected]:27017/dbname
EXPOSE 80

CMD [ "pm2-docker", "start", "pm2.config.js", "--env", "production" ]

When I run MongoClient.connect(process.env.MONGODB_AUTH, { useNewUrlParser: true });, I get the following error message in my application:

MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

I can't understand what I'm doing wrong. This is console output for docker network ls:

NETWORK ID          NAME                    DRIVER     SCOPE
4731ec3d6886        bridge                  bridge     local
e5a6bf78bd5f        docker_gwbridge         bridge     local
aae21167a37a        host                    host     local
d6ba0dfe01a3        myproject_default       bridge     local
34af02bd434d        myproject_my-network    bridge     local
5911111233bf        none                    null     local

This is output for docker network inspect myproject_my-network:

[
    {
        "Name": "myproject_my-network",
        "Id": "34af02bd434d7fe8b6755f584b45eeb0a47fafb087d7f44023598d6ae8bb79a4",
        "Created": "2018-08-05T22:21:06.938675177+03:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "my-network",
            "com.docker.compose.project": "myproject"
        }
    }
]

When I try using 172.20.0.1 instead of 127.0.0.1, I get the same error. I will glad for any help with my problem

1
  • It sounds like you're starting MongoDB automatically from within your same Docker instance, and, if so, your configuration looks ok. SUGGESTIONS: 1) Look here: docs.docker.com/samples/library/mongo/…, 2) Verify that MongoDB itself is up and running (for example, use mongo shell. Finally, 3) focus on why NodeJS (also on the same Docker instance) is unable to connect. Commented Aug 5, 2018 at 20:37

1 Answer 1

7

It seems that you are running mongodb inside a container named mongodb.

MongoDB is listenning to port 27017 of the container, and your container publish it on host's port 27017.

But inside your application container, no app is listenning on port 27017 of localhost.

So your error is normal, you shouldn't be able to connect to mongo this way. Instead, you can try:

ENV MONGODB_AUTH mongodb://login:pw@mongodb:27017/dbname

You will still need to access mongo with mongodb://login:[email protected]:27017/dbname from your host though.

There is an example in documentation of docker compose :

Within the web container, your connection string to db would look like postgres://db:5432, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:8001. From https://docs.docker.com/compose/networking/

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.