1

I can't connect to my local MongoDB on Ubuntu 20.04 from within a container created with docker-compose.

docker-compose.yml:

version: "3.1"
services:
  server:
    extra_hosts:
      - "host.docker.internal:host-gateway"
    restart: always
    build:
      context: ..
      dockerfile: docker/server.Dockerfile
    ports:
      - "8084:3000"

Dockerfile:

FROM node:18.16.0-alpine3.16

WORKDIR /app

COPY . /app
COPY .env.prod /app/.env

RUN npm install

COPY . /app

EXPOSE 8084

CMD ["npm", "start"]

docker-compose execution: docker-compose -p service -f docker-compose.yml up -d

The connection string in my env is: mongodb://<user>:<pw>@host.docker.internal:27017/my-db-name

I'm able to connect to the Mongo instance on the server with: mongo mongodb://<user>:<pw>@localhost:27017/my-db-name

I added the following lines to my /etc/mongod.conf:

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,172.17.0.1

I've already seen From inside of a Docker container, how do I connect to the localhost of the machine? but can not get it to work.

The error message in my the container logs is: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

I reconstructed the scenario on macOS and it worked just fine using host.docker.internal. Any ideas what could be wrong? Thank you in advance.

kwe
  • 45
  • 1
  • 7

0 Answers0