文章目录
- 官网地址
- docker 网络
- 部署 MongoDB
- 部署 mongo-express
- docker-compose.yml
- MongoDB shell
官网地址
- https://www.mongodb.com/zh-cn
docker 网络
# 创建 mongo_network 网络
docker network create mongo_network
# 查看网络
docker network list
# 容器连接到 mongo_network 网络
docker network connect mongo_network mongo
# 断开容器 mongo_network 网络
docker network disconnect mongo_network mongo
部署 MongoDB
- https://hub.docker.com/_/mongo
# MongoDB镜像
docker pull mongo:4
docker run --name mongo \
--network mongo_network \
-p 27017:27017 \
-v $PWD/mongo/data:/data/db \
-e MONGO_INITDB_ROOT_USERNAME="root" \
-e MONGO_INITDB_ROOT_PASSWORD="4NuMDwIzp05BdKp7Bdmf" \
-d mongo:4
- 指定服务执行器与缓存大小
docker run --name mongo \
--network mongo_network \
-p 27017:27017 \
-v $PWD/mongo/data:/data/db \
-e MONGO_INITDB_ROOT_USERNAME="root" \
-e MONGO_INITDB_ROOT_PASSWORD="4NuMDwIzp05BdKp7Bdmf" \
-d mongo:4 \
--serviceExecutor adaptive --wiredTigerCacheSizeGB 1.2
部署 mongo-express
- https://hub.docker.com/_/mongo-express
# mongo-express镜像
docker pull mongo-express:1.0.2
docker run --name mongo-express \
--network mongo_network \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_SERVER=mongo \
-e ME_CONFIG_MONGODB_ADMINUSERNAME="root" \
-e ME_CONFIG_MONGODB_ADMINPASSWORD="4NuMDwIzp05BdKp7Bdmf" \
# 设置 mongo-express 登录账号密码
-e ME_CONFIG_BASICAUTH=true \
-e ME_CONFIG_BASICAUTH_USERNAME="admin" \
-e ME_CONFIG_BASICAUTH_PASSWORD="123456" \
-d mongo-express:1.0.2
- 查看默认登录账号密码
# 进入 mongo-express 容器内
docker exec -it mongo-express /bin/bash
# 查看config.default.js
cat /app/config.default.js
注意: 默认登录账号密码是 admin/pass,docker配置中已经将登录账号密码设置为 admin/123456
- http://localhost:8081/
docker-compose.yml
services:
mongo:
image: mongo:4
container_name: mongo
restart: always
volumes:
- /etc/localtime:/etc/localtime
- ./mongo/data:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 4NuMDwIzp05BdKp7Bdmf
command: --wiredTigerCacheSizeGB 1
# command: --serviceExecutor adaptive --wiredTigerCacheSizeGB 1.2
mongo-express:
image: mongo-express:1.0.2
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_OPTIONS_EDITORTHEME: "ambiance"
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: 4NuMDwIzp05BdKp7Bdmf
ME_CONFIG_MONGODB_SERVER: mongo
# ME_CONFIG_MONGODB_URL: mongodb://root:4NuMDwIzp05BdKp7Bdmf@mongo:27017/
ME_CONFIG_BASICAUTH: true
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: 123456
depends_on:
- mongo
- http://localhost:8081/
注意: 登录账号密码已改为 admin/123456
MongoDB shell
# 进入 mongo 容器内
docker exec -it mongo /bin/bash
# 进入 MongoDB shell
$ mongo
# 退出
$ exit