Home > Software design >  Create Mongo docker image and add user to database
Create Mongo docker image and add user to database

Time:12-07

I am trying to find a way to create a docker container that when created has the db user profile created automatically. I am using a dockerfile and thought i could add the commands to create the user in it.

Anyone done this before and know what I am doing wrong here. I am no expert at Docker.

FROM mongo:latest

RUN mongo &&\
    use tewtdb &&\
    db.createUser({user: '<user>', pwd: '<pwrd>', roles[{role: 'dbOwner', db: 'tewtdb'}]})

EXPOSE 27017

CMD ["mongod"]

enter image description here

CodePudding user response:

shell

docker run --name mongodb -d -e MONGO_INITDB_ROOT_USERNAME=user -e MONGO_INITDB_ROOT_PASSWORD=pwd -v D:/mdb:/data/db -p 27117:27017 mongo:5.0

dockerfile-compose

version: '3'
services:
  standalone:
    image: mongo:5.0
    ports:
      - 27117:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=user
      - MONGO_INITDB_ROOT_PASSWORD=pwd
    volumes:
      - D:/mdb:/data/db
docker-compose -f docker-compose.yml up -d
  • Related