0

I am trying to connect correctly to the mongo instance run by docker. I have the following config.yml

version: "3"
services:
  mongo-server:
    container_name: mongo-server
    image: mongo:4.4.2
    restart: always
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
    volumes:
      - mongo-data:/data/db

  app:
    container_name: app
    image: "node:12"
    working_dir: /home/node/app
    environment:
      - NODE_ENV=production
    volumes:
      - ./:/home/node/app
    ports:
      - "8888:8888"
    command: "npm run start"
    depends_on:
      - mongo-server

  mongo-express:
    container_name: mongo-admin
    image: mongo-express:0.54
    restart: always
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongo-server
      - ME_CONFIG_MONGODB_PORT=27017
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
      - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
      - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}
      - ME_CONFIG_BASICAUTH_USERNAME=${MONGO_EXPRESS_LOGIN}
      - ME_CONFIG_BASICAUTH_PASSWORD=${MONGO_EXPRESS_PASSWORD}
    depends_on:
      - mongo-server
volumes:
  mongo-data:

I have checked that the .env vars are being passed correctly with docker-compose config

From there I spin up the docker images by running: docker-compose up -d

I found out how to delete the volumes which was the first major issue. Now connecting to the database in my app is next. The supplied username and password aren't quite working.

The app is attempting to connect to the following string with mongoose connect:

`mongodb://${process.env.MONGO_ROOT_USER}:${process.env.MONGO_ROOT_PASSWORD}@mongo:27017/app`

But that isn't working and I am not quite sure why. If anyone has an idea of what it should be please let me know

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Josh Bowden
  • 892
  • 3
  • 12
  • 28
  • `mongodb://.....@/!\mongo/!\:27017/app`. => From what I can see in your compose file, your service name is `mongo-server`, not `mongo`. In your app log that you didn't show, you should likely see a `Name or service not known` errror or equivalent. But that's pure guess because you clearly did not provide enough debugging information besides `that isn't working`. – Zeitounator Dec 18 '20 at 23:49
  • Hey @Zeitounator, I am a bit new to docker. What could I provide to make it easier. I tried using the service name with the same authentication and I got `getaddrinfo ENOTFOUND undefined` back from the server. Are these `/!\` that you placed around the service name just for me to see which mongo you are referencing? – Josh Bowden Dec 19 '20 at 14:49

0 Answers0