How to create user in mongodb with docker-compose

后端 未结 8 1192
天命终不由人
天命终不由人 2020-12-23 20:28

I\'m trying to create some kind of script that will create a docker with mongodb and automatically create a user.

I can usually manage my docker images with docker-c

8条回答
  •  醉话见心
    2020-12-23 21:12

    file: docker-compose.yaml

    mongo:
        image: mongo:latest
        volumes_from:
            - data
        ports:
            - "27017:27017"
        command: --auth
    
        container_name: "db_mongodb"
    
    data:
        image: mongo:latest
        volumes:
            - /var/lib/mongo
            - ./setup:/setup
        command: "true"
        container_name: "db_mongodb_data"
    

    file: .buildMongo.sh

    #!/bin/sh
    docker-compose down
    docker-compose up -d
    sleep 1
    docker exec db_mongodb  mongo admin /setup/create-admin.js
    docker exec db_mongodb  mongo myDb /setup/create-user.js -u admin -p admin --authenticationDatabase admin
    

    The create-admin.js and create-user.js files are commands that you use using the mongo shell. So they must be easy for you to understand. The real direction is like the jzqa answer, "environment variables". So the question here is how to create a user. I think this answers that point at least, you can check the complete setup here https://github.com/Lus1t4nUm/mongo_docker_bootstrap

提交回复
热议问题