version: '3' services: mongo: image: mongo container_name: mongo hostname: mongo restart: always networks: - db ports: - "27017:27017" environment: TZ: Asia/Shanghai MONGO_INITDB_DATABASE: test MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: 123456 volumes: - /etc/localtime:/etc/localtime - ./data:/data/db - ./init/:/docker-entrypoint-initdb.d/ command: mongod mongo-express: image: mongo-express container_name: mongo-express restart: always links: - mongo:mongo depends_on: - mongo networks: - db ports: - "27018:8081" environment: ME_CONFIG_OPTIONS_EDITORTHEME: 3024-night ME_CONFIG_MONGODB_SERVER: mongo ME_CONFIG_MONGODB_ADMINUSERNAME: root ME_CONFIG_MONGODB_ADMINPASSWORD: 123456 ME_CONFIG_BASICAUTH_USERNAME: root ME_CONFIG_BASICAUTH_PASSWORD: 123456 networks: db: driver: bridge
# init.sh
#!/usr/bin/env bash echo "Creating mongo users..." mongo admin --host localhost -u root -p 123456 --eval "db.createUser({user: 'admin', pwd: '123456', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]});" mongo admin -u root -p 123456 << EOF use hi db.createUser({user: 'test', pwd: '123456', roles:[{role:'readWrite',db:'hi'}]}) EOF echo "Mongo users created."