1 Docker 安装

# step 1: 安装必要的一些系统工具
$ yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
$ yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
$ sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
$ yum makecache fast
$ yum -y install docker-ce
# Step 4: 开启Docker服务
$ service docker start
# 查看版本号
docker --version

2 Docker-compose 安装

$ curl -L https://www.gitlink.org.cn/attachments/entries/get_file?download_url=https://www.gitlink.org.cn/api/lengleng/mirror/raw/docker-compose-linux-x86_64?ref=master -o /usr/local/bin/docker-compose

$ chmod +x /usr/local/bin/docker-compose

$ docker-compose version

3 Yapi 安装

3.1 获取 Yapi v1.12.0 版本包软件

$ cd /opt/src
$ wget https://codeload.github.com/YMFE/yapi/tar.gz/refs/tags/v1.12.0 -O yapi-1.12.0.tar.gz

3.2 创建 Yapi 目录

$ mkdir -pv /data/yapi/{conf,log,docker-compose}
$ cp yapi-1.12.0.tar.gz /data/yapi/docker-compose/
$ cd /data/yapi/docker-compose

3.3 提供镜像打包和服务配置文件

3.3.1 Dockerfile

$ vim Dockerfile
FROM node:11 as builder

RUN apt-get install -y  git python make openssl tar gcc
ADD yapi-1.12.0.tar.gz /home/
RUN mkdir /api && mv /home/yapi-1.12.0 /api/vendors
RUN cd /api/vendors && \
    npm install --production --registry https://registry.npm.taobao.org

FROM node:11

MAINTAINER evescn
ENV TZ="Asia/Shanghai" HOME="/"
WORKDIR ${HOME}

COPY --from=builder /api/vendors /api/vendors
EXPOSE 3000

COPY docker-entrypoint.sh /api/
RUN chmod 755 /api/docker-entrypoint.sh

ENTRYPOINT ["/api/docker-entrypoint.sh"]

3.3.2 docker-entrypoint.sh

$ vim docker-entrypoint.sh
#!/bin/bash
set -eo pipefail
shopt -s nullglob

node /api/vendors/server/app.js

exec "$@"

3.3.3 docker-compose.yaml

$ vim docker-compose.yaml
version: "3"

services:
  yapi:
    build:
      context: ./
      dockerfile: ./Dockerfile
    image: devops/yapi:v1.12.0
    container_name: yapi
    environment:
      - VERSION=1.12.0
      - LOG_PATH=/tmp/yapi.log
    ports:
      - 3001:3000
    volumes:
      - /data/yapi/log/yapi.log:/tmp/yapi.log # log dir
      - /data/yapi/conf/config.json:/api/config.json
    depends_on:
      - mongo
    networks:
      - YapiNet

  mongo:
    image: mongo:6.0.4
    container_name: mongo
    restart: always
    ports:
      - 27018:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=hmgsoft
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/yapi/mongodb/data:/data/db
      - /data/yapi/mongodb/log:/var/log/mongodb
    networks:
      - YapiNet

networks:
  YapiNet:
    driver: bridge

3.3.4 config.json

$ vim config.json
{
    "port":"3000",
    "adminAccount":"zhuqx@hmgsoft.com",
    "timeout":120000,
    "db":{
        "servername":"mongo",
        "DATABASE":"yapi",
        "port":27017,
        "user":"root",
        "pass":"123456",
        "authSource":"admin"
    },
    "mail":{
        "enable":false,
        "host":"smtp.qiye.aliyun.com",
        "port11":465,
        "from":"alertmanager@hmgsoft.com",
        "auth":{
            "user":"alertmanager@hmgsoft.com",
            "pass":"NKhKdaPeuuRgvpWi"
        }
    },
    "ldapLogin":{
        "enable":false,
        "server":"ldap://l-ldapt1.com",
        "baseDn":"CN=Admin,CN=Users,DC=test,DC=com",
        "bindPassword":"password123",
        "searchDn":"OU=UserContainer,DC=test,DC=com",
        "searchStandard":"mail",
        "emailPostfix":"@163.com",
        "emailKey":"mail",
        "usernameKey":"name"
    }
}

3.4 打包镜像

$ docker-compose build

3.5 提供服务配置文件信息

$ cp config.json /data/yapi/conf/config.json

3.6 启动服务

$ docker-compose up -d

3.7 初始化数据

3.7.1 初始化 YAPI 账号

$ docker ps
$ docker exec -it yapi /bin/bash

root@50716dd50f80:/# ls
root@50716dd50f80:/# cd /api/vendors/
root@50716dd50f80:/api/vendors# npm run install-server
log: mongodb load success...
初始化管理员账号成功,账号名:"zhuqx@163.com",密码:"ymfe.org"

3.7.2 初始化 MongoDB

use yapi
db.createUser(
  {
    user: "root",
    pwd: "hmgsoft",
    roles: [{ role: "readWrite", db: "yapi" }]
  }
)

3.8 登录

地址:http://127.0.0.1:3001

账号:zhuqx@163.com / ymfe.org

4 备份

4.1 授权数据库用户

db.grantRolesToUser("root",[{role:"dbOwner", db:"yapi"}])

4.2 备份脚本

apt-get update
apt-get install -y vim

docker exec -it mongo sh

mkdir backup
vim /backup/backup_yapi.sh

backup_yapi.sh

#!/bin/bash

BACKUP_DIR=/backup

DATE=$(date +"%Y-%m-%d")

mongodump --db yapi --username root --password hmgsoft --authenticationDatabase admin --archive=/backup/yapi-$DATE.gz
#授权
chmod +x /backup/backup_yapi.sh
apt-get update
apt-get install -y cron
crontab -e

#每天凌晨3点执行备份脚本
0 3 * * * /backup/backup_yapi.sh

4.3 将容器内备份文件复制到宿主机

mkdir -p /data/yapi/backup

vim /data/yapi/backup/backup_yapi.sh

backup_yapi.sh

#!/bin/bash

DATE=$(date +"%Y-%m-%d")

docker cp mongo:/backup/yapi-$DATE.gz /data/yapi/backup
#授权
chmod +x /data/yapi/backup/backup_yapi.sh
crontab -e

#每天凌晨4点执行备份脚本
0 4 * * * /data/yapi/backup/backup_yapi.sh

5 恢复

docker exec -i mongo sh -c 'mongorestore --db yapi --username root --password 123456 --authenticationDatabase admin --archive' < /data/yapi/backup/yapi-2023-12-26.gz