2

MongoServerSelectionError: getaddrinfo ENOTFOUND mongo

is the error I'm getting when I try to run docker-compose build.

This is my docker-compse.yml file

version: '3'
services:
  app:
    container_name: node-app
    build: .
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - ./uploads:/app/uploads
    links:
      - mongo
  mongo:
    container_name: mongo
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"
    command: mongod

as well as my Dockerfile

# https://docs.docker.com/samples/library/node/
ARG NODE_VERSION=12.10.0
# https://github.com/Yelp/dumb-init/releases
ARG DUMB_INIT_VERSION=1.2.2

# Build container
FROM node:${NODE_VERSION}-alpine AS build
ARG DUMB_INIT_VERSION

WORKDIR /home/node

RUN apk add --no-cache build-base python2 yarn && \
    wget -O dumb-init -q https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64 && \
    chmod +x dumb-init
ADD . /home/node
RUN yarn install && yarn build && yarn cache clean

# Runtime container
FROM node:${NODE_VERSION}-alpine

WORKDIR /home/node

COPY --from=build /home/node /home/node

EXPOSE 3000
CMD ["./dumb-init", "yarn", "start"]

My connection string in the code is mongodb://mongo:27017/{db_name}

When I run docker ps -a, I can clearly that my mongo image is there. I've googled this issue to no extent, and tried ridiculous combinations of connection strings to try and connect to mongo, but does anyone have any supplemental information or debugging advice to overcome this?

jabusir
  • 21
  • 5
  • You said you're hitting this during the build? Code in the Dockerfile can never connect to other containers; whatever step you're doing that depends on a database needs to happen at container startup time. – David Maze Dec 26 '20 at 11:25

1 Answers1

0

The issue is most probably that you start mongod without passing the --bind_ip_all parameter. By default, mongod only binds to 127.0.0.1 as also stated by other SO posts.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Martin Löper
  • 6,471
  • 1
  • 16
  • 40