Docker-compose builds the latest version of MongoDB
1. Directory structure
.
└── docker_mongodb
├── docker-compose.yml
└── db
2. docker-compose.yml
version: "3.5"
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: always
ports:
- "27017:27017"
environment:
TZ: Asia/Shanghai
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: 123456
volumes:
- ./db:/data/db
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
network_mode: "bridge"
mongo-express:
image: mongo-express:latest
container_name: mongo-express
ports:
- "27018:8081"
environment:
ME_CONFIG_OPTIONS_EDITORTHEME: 3024-night
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: 123456
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: 123456
depends_on:
- mongodb
network_mode: "bridge"
3. Create the db folder
Directory and file screenshots are as follows
4. Start the service
# 进入docker_mongodb目录下
cd /系统目录/docker_mongodb
# 启动服务
docker compose up -d
5. Configure the MongoDB database
a. to enter the container
docker exec -it mongodb bash
b. Enter the user
mongosh -u admin -p 123456 --authenticationDatabase admin
c. Enter the admin database
use admin;
d. Create an admin user
db.createUser({
user: "admin",
pwd: "123456",
roles: [ {
role: "userAdminAnyDatabase", db: "admin" } ]
})
e. Create a test user, specific to a database, for example: django_pro
db.createUser({
user: 'django_pro',
pwd: 'djp123456',
roles: [{
role: "readWrite", db: "django_pro"}]
})
f. Create a database
use django_pro;
g. Write data test
db.info.save({
name: 'lili', age: '25'})
h. Query written data
db.info.find();
Remark
- mongo-express interface: http://localhost:27018/