docker-compose安装mongodb

1. 配置docker-compose.yml

# 该Yaml文件改编自DockerHub中的配置文件
version: '3.8'
services:
mongo:
image: mongo:4.4.0 #根据需要选择自己的镜像
restart: always
ports:
- 27017:27017 #对外暴露停供服务的端口,正式生产的时候理论不用暴露。
volumes:
- /docker/mongodb/data/db:/data/db # 挂载数据目录
- /docker/mongodb/data/log:/var/log/mongodb # 挂载日志目录
- /docker/mongodb/data/config:/etc/mongo # 挂载配置目录
# command: --config /docker/mongodb/mongod.conf # 配置文件

2. 创建配置文件

# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 8
directoryForIndexes: true

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log

# network interfaces
net:
port: 27017
bindIp: 0.0.0.0

# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo

#replication:
replication:
oplogSizeMB: 51200
replSetName: rs0

3. 运行mongodb

docker-compose -f docker-compose.yml up -d

4. 进入mongodb

docker exec -it mongo /bin/bash

5. 添加用户

mongo
use admin
db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})
exit
exit