Home > Software engineering >  How to connect to concrete database in docker-compose
How to connect to concrete database in docker-compose

Time:11-30

I have next setup of docker-compose.yaml

name: test-app-to-delete

services:
  mongo:
    image: mongo
    restart: always
    networks:
      - mongo
    volumes:
      - db:/data/db:rw
    environment:
      MONGO_INITDB_ROOT_USERNAME: 'user'
      MONGO_INITDB_ROOT_PASSWORD: 'password'
      MONGO_INITDB_DATABASE: 'db-name'

  mongo-express:
    image: mongo-express
    restart: always
    networks:
      - mongo
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: user
      ME_CONFIG_MONGODB_ADMINPASSWORD: password
      ME_CONFIG_MONGODB_URL: mongodb://user:password@mongo:27017/db-name

networks:
  mongo:

volumes:
  db:

So issue become a problem when I'm adding db-name to this configuration.

Errors:

Could not connect to database using connectionString: 
mongodb://user:password@mongo:27017/db-name

Just for note: I'm recreated database before run this configuration, so issue not in cached data.

CodePudding user response:

You can try mongodb://user:password@localhost:27017/db-name?authSource=admin instead of your current connection string. And make sure to use base64 converted values for username and password

  • Related