Docker 安装系列 Docker已安装。
1、场景Yapi使用的MongoDB用户信息
1.1 创建自定义 Docker 网络
首先,创建一个自定义的 Docker 网络,以便 MongoDB 和 YApi 容器可以相互通信
[root@flexusx-328569 data]# docker network create yapi-network
48a8836621729c10bf96d0bf7c1735daad7449ae0c648a632b47fe3053f2a768
[root@flexusx-328569 data]#
1.2 部署 MongoDB 容器
接下来,启动 MongoDB 容器,并将其加入到刚才创建的 yapi-network
中。同时,设置环境变量以初始化管理员用户
docker pull mongo:5.0
[root@flexusx-328569 data]# docker run -d --restart=always --network yapi-network -p 27017:27017 --name mongo-yapi -v /data/mongdb-yapi/data:/data/db -e MONGO_INITDB_ROOT_USERNAME=hvadmin -e MONGO_INITDB_ROOT_PASSWORD=Hv123456 -d mongo:5.0
f438729acf674b299adb6beeb64f5864d246ad40f443f5f3eb77a71066014949
[root@flexusx-328569 data]# docker exec -it f438 mongo
MongoDB shell version v5.0.30
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cfcb003a-13b5-4ca7-a30f-184bb65cfed1") }
MongoDB server version: 5.0.30
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, seehttps://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forumshttps://community.mongodb.com
> use admin;
switched to db admin
> db.auth("hvadmin","Hv123456");
1
> show users;
{"_id" : "admin.hvadmin","userId" : UUID("9e703b9f-e7b6-426e-98fa-8e118e4c4029"),"user" : "hvadmin","db" : "admin","roles" : [{"role" : "root","db" : "admin"}],"mechanisms" : ["SCRAM-SHA-1","SCRAM-SHA-256"]
}
>
1.3 创建用户
db.createUser({user: 'yapi',pwd: 'yapi123',roles: [{ role: "dbAdmin", db: "yapi" },{ role: "readWrite", db: "yapi" }]});
> use yapi;
switched to db yapi
> db.createUser({user: 'yapi',pwd: 'yapi123',roles: [{ role: "dbAdmin", db: "yapi" },{ role: "readWrite", db: "yapi" }]});
Successfully added user: {"user" : "yapi","roles" : [{"role" : "dbAdmin","db" : "yapi"},{"role" : "readWrite","db" : "yapi"}]
}
> show users;
{"_id" : "yapi.yapi","userId" : UUID("a652d417-8f92-46f6-a55a-0e0092939dac"),"user" : "yapi","db" : "yapi","roles" : [{"role" : "dbAdmin","db" : "yapi"},{"role" : "readWrite","db" : "yapi"}],"mechanisms" : ["SCRAM-SHA-1","SCRAM-SHA-256"]
}
> db.createCollection("tseng1");
{ "ok" : 1 }
>
> db.getCollectionNames();
[ "tseng1" ]
> exit
bye
[root@flexusx-328569 data]#
测试连接
2、下载
[root@flexusx-328569 data]# docker pull yapipro/yapi:latest
latest: Pulling from yapipro/yapi
cbdbe7a5bc2a: Pull complete
8f3938f7d3bd: Pull complete
6e3c12f5dc10: Pull complete
ce0cb7a9eeee: Pull complete
33778c009b63: Pull complete
0a69a898b7c7: Pull complete
Digest: sha256:163e5d6219107c9eae0e0cd5d2cdbb78d7ef82d992850b1d7c920d8ee0371f9b
Status: Downloaded newer image for yapipro/yapi:latest
[root@flexusx-328569 data]#
3、查看下载
[root@flexusx-328569 data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xuxueli/xxl-job-admin 2.4.2 776d09cf1493 2 weeks ago 236MB
mongo 5.0 ed1e02a705a3 5 weeks ago 723MB
eclipse-mosquitto latest fdc6f47a31a7 6 weeks ago 14.9MB
nacos/nacos-server v2.4.3 fc86d4833afb 7 weeks ago 286MB
nginx 1.26.2 f87a52ac001c 3 months ago 188MB
yapipro/yapi latest f42f7299bbef 3 years ago 248MB
redis 6.0.6 1319b1eaa0b7 4 years ago 104MB
mysql 8.0.18 ed1ffcb5eff3 4 years ago 456MB
[root@flexusx-328569 data]#
4、查看 latest 对应的版本
[root@flexusx-328569 data]# docker image inspect yapipro/yapi:latest | grep -i version"DockerVersion": "","NODE_VERSION=12.22.3","YARN_VERSION=1.22.5",
[root@flexusx-328569 data]#
5、配置文件
[root@flexusx-328569 data]# mkdir -p /data/yapi
[root@flexusx-328569 data]# vim /data/yapi/config.json
[root@flexusx-328569 data]# cat /data/yapi/config.json {"port": "3000","adminAccount": "admin@yapi.com","timeout":12000,"db": {"servername": "mongo-yapi","DATABASE": "yapi","port": 27017,"user": "yapi","pass": "yapi123","authSource": ""},"mail": {"enable": false},"closeRegister": true
}
[root@flexusx-328569 data]#
配置解析
1.
port
(端口)
- 描述:指定YApi应用监听的HTTP端口。
- 值:
"3000"
,表示YApi将监听在3000端口上。2.
adminAccount
(管理员账户)
- 描述:指定初始管理员用户的邮箱地址。在首次安装或初始化时会创建此用户。
- 值:
"yapiadmin@yapi.com"
,表示管理员的邮箱为yapiadmin@yapi.com
。3.
timeout
(超时时间)
- 描述:设置请求处理的最大等待时间,单位为毫秒。
- 值:
120000
,表示请求处理的最大等待时间为120秒(2分钟)。4.
db
(数据库配置)
- 描述:包含连接到MongoDB数据库所需的所有信息。
- 字段解释:
"servername"
:MongoDB服务器的主机名或IP地址。在这个例子中,它被设置为"mongodb"
,这通常意味着MongoDB服务在同一Docker网络中的另一个容器,并且该容器的名称为mongodb
。"DATABASE"
:数据库名称,这里是"yapi"
。"port"
:MongoDB服务的端口号,默认是27017
。"user"
:用于认证的用户名,这里是"yapi"
。"pass"
:用于认证的密码,这里是"yapi123"
。"authSource"
:认证源数据库,默认为空字符串,表示使用默认的认证机制。如果需要指定特定的认证数据库,可以在这里填入相应的数据库名称,例如"admin"
。5.
- 描述:配置是否启用邮件发送功能,以及相关参数。
- 字段解释:
"enable"
:布尔值,表示是否启用邮件功能。当前设置为false
,意味着邮件功能未启用。
6、启动容器
[root@flexusx-328569 yapi]# docker run -d --name yapi --restart always -p 3000:3000 -v /data/yapi/config.json:/yapi/config.json -e MONGO_SERVER=mongo-yapi -e MONGO_PORT=27017 -e MONGO_DB=yapi -e MONGO_USER=yapi -e MONGO_PASS=yapi123 yapipro/yapi:latest server/app.js
666a50fe667566dcafb0db066e68262863f8bd503466b70b52c3823ba25392d2
[root@flexusx-328569 yapi]#
7、查看容器状态
[root@flexusx-328569 yapi]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
666a50fe6675 yapipro/yapi:latest "node server/app.js" 2 seconds ago Up 2 seconds 0.0.0.0:3000->3000/tcp yapi
0e7735b7df0d mongo:5.0 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:27017->27017/tcp mongo-yapi
6ecff9da64ef nginx:1.26.2 "/docker-entrypoint.…" 2 weeks ago Up 2 weeks 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx
5372d6f6c213 mysql:8.0.18 "docker-entrypoint.s…" 2 weeks ago Up 3 days 33060/tcp, 0.0.0.0:3308->3306/tcp mysql3308.8.0.18
72e4a9a96a7f redis:6.0.6 "docker-entrypoint.s…" 2 weeks ago Up 2 weeks 0.0.0.0:16379->6379/tcp redis16379
[root@flexusx-328569 yapi]# docker logs -f yapi
log: -------------------------------------swaggerSyncUtils constructor-----------------------------------------------
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3000/
8、测试、访问:http://ip地址:3000
账号“admin@yapi.com”(config.json 中配置的 adminAccount),默认密码:yapi.pro