TwistedOwl
TwistedOwl

Reputation: 1324

Docker MongoDB immadiately closes connection after receiving metadata

I'm trying to compose docker app with two containers:

  1. mongo
  2. app

Mongo container works just fine, meanwhile app cannot connect to mongo. Neither node.js app nor mongostat can. The weird part is, I tried to run this project on both computers with Win10 and it works normally on the other one.

These are logs from mongo container when I run node app.js or mongostat --uri "mongodb://mongo:27017/project" from app container:

2019-05-22T09:33:52.225+0000 I NETWORK  [conn17] received client metadata from 192.168.96.2:42916 conn17: { driver: { name: "nodejs", version: "3.1.10" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.9.125-linuxkit" }, platform: "Node.js v10.15.3, LE, mongodb-core: 3.1.9" }
2019-05-22T09:33:52.231+0000 I NETWORK  [conn17] end connection 192.168.96.2:42916 (0 connections now open)

This means both containers can see each other so .yml file should be fine. If the problem was with code then it shouldn't work on both computers.

Dockerfile:

FROM node:10.15.3-alpine
RUN apk update && apk --no-cache --virtual build-dependencies add python make g++ && apk del build-dependencies
RUN mkdir -p /home/node/app && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
COPY --chown=node:node . .
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]

docker-compose.yml:

version: "3.5"
services:
  app:
    container_name: app
    restart: always
    build: .
    ports:
      - "3000:3000"
    networks:
      - mongo
  mongo:
    restart: always
    container_name: mongo
    image: mongo
    expose: 
      - 27017
    volumes:
      - mongodata:/data/db
    ports:
      - '27017:27017'
    networks:
      - mongo
volumes:
  mongodata:
networks:
  mongo:
    external: true

snippet from app.js:

MongoClient.connect('mongodb://mongo:27017/project', {useNewUrlParser: true}, (err, client) => {
    if (err) throw err; //throws MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongo mongo:27017]
    console.log("connected");
    client.close();//at the moment this line is not being reached because of throw err;
});```

Upvotes: 0

Views: 1707

Answers (3)

TwistedOwl
TwistedOwl

Reputation: 1324

After hours of trying multiple things I have found solution: turn off Windows Firewall. That's it.

Thanks, I appreciate your help.

Upvotes: 0

mikesir87
mikesir87

Reputation: 1797

Since you are getting a getaddrinfo ENOTFOUND error, the mongo hostname isn't resolving. Usually, that happens for one of two reasons: 1) your containers aren't on the same network or 2) the other container isn't up and running yet. Seeing that they are on the same network, it sounds like it's something with the container being up.

To troubleshoot, I would start another container, put it on the network, and validate the mongo hostname resolves.

docker container run --rm -ti --network mongo ubuntu
$ apt update && apt install -y dnsutils
$ dig mongo

At this point, you should see the A record resolve to the database. If not, validate the mongo database container is up and running.

You can also try doing this within your app container as well. If that's working, then using something like waitforit should work. This is a common issue, as apps may start up before the database is either running or ready to accept connections.

As one other item of feedback, you don't need to expose the mongo port. This is making it accessible to the world, which most likely isn't what you want. You can still do container-to-container communication without exposing the port.

Upvotes: 1

Frank Louwers
Frank Louwers

Reputation: 643

Does it help if you insert a "sleep 10" in your application, before connecting to the mongo db? If so, adding something like wiatforit (https://github.com/maxcnunes/waitforit) might help.

Upvotes: 1

Related Questions