First, create a mongod config file. e.g.
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
Then run mongo.
docker run --name mongo-c1 -d \
--network my-vps-main-network \
-e MONGO_INITDB_ROOT_USERNAME=[mongoAdminName e.g. root] \
-e MONGO_INITDB_ROOT_PASSWORD=[some-initial-password] \
-v /workspace/www/mongo/mongod.conf:/etc/mongod.conf \
-v /workspace/www/mongo/data:/data/db \
-v /workspace/downloadCenter:/workspace/downloadCenter \
mongo -f /etc/mongod.conf
In future, to login to mongo, type:
docker exec -it mongo-c1 mongo -u root -p 'some-initial-password' --authenticationDatabase admin