Docker local deployment YApi
This content takes the virtual machine [system: Centos7] as an example, and the steps of the cloud server are the same. Use Docker to build YApi, just pull the MongoDB image and YApi image.
Install Docker
Install
-
The yum package is updated to the latest:
yum update
-
Install the required software packages, yum-util provides yum-config-manager function, and the other two are devicemapper driver dependencies:
yum install -y yum-utils device-mapper-persistent-data lvm2
-
Set the yum source to Alibaba Cloud:
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-
Install Docker Community Edition:
yum -y install docker-ce
-
Check the Docker version number:
docker -v
Set up USTC mirror
-
Edit
/etc/docker/daemon.json
file:mkdir -p /etc/docker vim /etc/docker/daemon.json
Note: Install vim via yum:
yum -y install vim*
. -
daemon.json
Enter the following in the file :{ "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"] }
start docker
-
start docker
systemctl start docker
-
stop docker
systemctl stop docker
-
View docker status
systemctl status docker
-
Set up to automatically start docker at boot
systemctl enable docker
Docker install MongoDB
Create a mongo container data mount directory:
mkdir /usr/local/mongo
Pull the MongoDB 4.2.21 version Docker image:
docker pull mongo:4.2.21
Create a web plugin for yapi:
docker network create yapi
Start the MongoDB container, specify the network plug-in, map port, mount directory, initial database yapi, root account password:
#启动MongoDB容器
docker run -d \
--name mongodb \
--restart always \
--net=yapi \
-p 2717:27017 \
-v /usr/local/mongo:/data/db \
-e MONGO_INITDB_DATABASE=yapi \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=root \
mongo:4.2.21
Enter the MongoDB container:
docker exec -it mongodb /bin/bash
Switch the admin database:
use admin;
Authenticate with the root account password:
db.auth("root ", "root");
Switch yapi database:
use yapi;
Create a user: yapi, and grant operational permissions:
db.createUser({
user: 'yapi',
pwd: 'yapi123456',
roles: [
{
role: "dbAdmin", db: "yapi" },
{
role: "readWrite", db: "yapi" }
]
});
Execute the exit command twice to exit to the terminal:
exit
Docker install YApi
Create a local configuration file config.json
for container mounting:
vim /usr/local/yapi/config.json
Add the following:
{
"port": "3000",
"adminAccount": "****@qq.com", // 管理员账号名,用于登录
"timeout":120000,
"db": {
"servername": "mongo",
"DATABASE": "yapi",
"port": 27017,
"user": "yapi",
"pass": "yapi123456",
"authSource": ""
},
"mail": {
// 邮箱功能
"enable": true,
"host": "smtp.qq.com",
"port": 465,
"from": "****@qq.com",
"auth": {
"user": "****@qq.com",
"pass": "****password"
}
}
}
Note: db.user and db.pass are the username and password of mongodb. If mongo authentication is not enabled, please delete these two options.
Docker search query yapi image:
docker search yapi
The pull command pulls the image:
docker pull yapipro/yapi
Initialize the yapi database table:
docker run -d --rm \
--name yapi-init \
--link mongodb:mongo \
--net=yapi \
-v /usr/local/yapi/config.json:/yapi/config.json \
yapipro/yapi \
server/install.js
Start the yapi container, specify the container name, port number, and mount the configuration file /usr/local/yapi/config.json
:
docker run -d \
--name yapi \
--link mongodb:mongo \
--restart always \
--net=yapi \
-p 3000:3000 \
-v /usr/local/yapi/config.json:/yapi/config.json \
yapipro/yapi \
server/app.js
Login to YApi
Access URL [ http://192.168.1.14:3000/ ]:
Login with account password:
Note: Initialize the value of adminAccount in the config.json configuration above to initialize the administrator account. The initial password is yapi.pro, which can be modified in the personal center after logging in.