我正在尝试一个带有一个数据库和一些集合的 Mongo 容器:
mongodb:
build: './docker/mongo'
restart: always
container_name: mongo
restart: unless-stopped
command: >
bash -c "mongod --auth
&& mongo admin -u mongodbuser -p mongodbpass < entrypoint.js"
environment:
MONGO_INITDB_ROOT_USERNAME: mongodbuser
MONGO_INITDB_ROOT_PASSWORD: mongodbpass
MONGO_INITDB_DATABASE: admin
MONGODB_DATA_DIR: /data/db
MONDODB_LOG_DIR: /dev/null
volumes:
- ./mnt/mongo:/data/db
ports:
- 27017:27017
我的 entrypoint.js 是:
db = db.getSiblingDB('test_db')
db.createCollection("test_col_1")
db.createCollection("test_col_2")
db.createCollection("test_col_3")
和我的 Dockerfile:
FROM mongo:latest
COPY entrypoint.js /entrypoint.js
RUN chmod +x /entrypoint.js
但是当我启动容器并尝试在 docker 中访问 mongo whit 命令时 mongo admin -u mongodbuser -p mongodbpass
显示错误:
MongoDB shell version v4.4.5
connecting to: mongodb://127.0.0.1:27017/admin?
compressors=disabled&gssapiServiceName=mongodb
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
如果我将命令更改为:
command: mongod --auth
工作正常,但我需要数据库和集合。有人知道我做错了什么吗?