Docker构建YApi镜像, Docker安装YApi, Docker部署YApi
概述
YApi 是高效、易用、功能强大的 api 管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。可以帮助开发者轻松创建、发布、维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只需利用平台提供的接口数据写入工具以及简单的点击操作就可以实现接口的管理。
YApi源码:https://github.com/YMFE/yapi
官方提供了很简单的安装工具,但由于我的安装环境在云端,想要使用docker安装,参考使用 alpine 版 docker 镜像快速部署 yapi, 修改了Dockerfile, 并完善了完整的生产可用的环境搭建过程。
Dockerfile: https://github.com/Ryan-Miao/docker-yapi
基本环境
- Yapi镜像基于
node:11
- 宿主机要安装docker
- MongoDb的dockers版本为4
docker网络
我们将采用Docker专门为Yapi提供一个MongoDb,由于docker kill重启可能会改变ip,这将使得Yapi也必须重新部署,因此,我们需要一个固定ip的mongo。所以,必须提供自定义docker网络。
docker network create --subnet=172.18.0.0/16 tools-net
安装MongoDb
Docker安装MongoDb涉及数据文件导出和备份,防止docker容器关闭后数据丢失。完整步骤参见:docker安装mongodb并备份.
简略步骤为:
安装
docker run \
--name mongod \
-p 27017:27017 \
-v /data/opt/mongodb/data/configdb:/data/configdb/ \
-v /data/opt/mongodb/data/db/:/data/db/ \
--net tools-net --ip 172.18.0.2 \
-d mongo:4 --auth
设置admin root用户
docker exec -it mongod mongo admin
>db.createUser({ user: 'admin', pwd: 'admin123456', roles: [ { role: "root", db: "admin" } ] });
设置yapi用户
db.auth("admin", "admin123456")
db.createUser({
user: 'yapi',
pwd: 'yapi123456',
roles: [
{ role: "dbAdmin", db: "yapi" },
{ role: "readWrite", db: "yapi" }
]
});
构建YAPI镜像
我们将基于node:11的debian镜像,部署YApi. 首先要构建Yapi镜像。
下载源码:
wget -O yapi.tgz http://registry.npm.taobao.org/yapi-vendor/download/yapi-vendor-$1.tgz
其中$1是版本,可以在github找到: https://github.com/YMFE/yapi/releases
新建Dockerfile
FROM node:11 as builder
RUN apt-get install -y git python make openssl tar gcc
ADD yapi.tgz /home/
RUN mkdir /api && mv /home/package /api/vendors
RUN cd /api/vendors && \
npm install --production --registry https://registry.npm.taobao.org
FROM node:11
MAINTAINER ryan.miao
ENV TZ="Asia/Shanghai" HOME="/"
WORKDIR ${HOME}
COPY --from=builder /api/vendors /api/vendors
COPY config.json /api/
EXPOSE 3001
COPY docker-entrypoint.sh /api/
RUN chmod 755 /api/docker-entrypoint.sh
ENTRYPOINT ["/api/docker-entrypoint.sh"]
其中,同目录下需要以下几个文件:
config.json
{
"port": "3001",
"adminAccount": "ryan.miao@demo.com",
"db": {
"servername": "172.18.0.2",
"DATABASE": "yapi",
"port": "27017",
"user": "yapi",
"pass": "yapi123456",
"authSource":"admin"
},
"mail": {
"enable": false,
"host": "smtp.163.com",
"port": 465,
"from": "***@163.com",
"auth": {
"user": "***@163.com",
"pass": "*****"
}
},
"ldapLogin": {
"enable": true,
"server": "ldap://192.168.5.3:389",
"baseDn": "cn=admin,dc=demo,dc=com",
"bindPassword": "admin",
"searchDn": "dc=demo,dc=com",
"searchStandard": "mail",
"emailPostfix": "@demo.com",
"emailKey": "mail",
"usernameKey": "sn"
}
}
不需要ldap的可以去掉。
docker-entrypoint.sh
#!/bin/bash
set -eo pipefail
shopt -s nullglob
if [ "$1" = '--initdb' ]; then
node /api/vendors/server/install.js
fi
if [ "$1" = '--help' ]; then
echo "Usage:"
echo "初始化db并启动: docker run -d -p 3001:3001 --name yapi --net tools-net --ip 172.18.0.3 yapi --initdb"
echo "初始化后的账号为config.json 配置的邮箱, 密码为ymfe.org"
echo "直接启动: docker kill yapi && docker rm yapi && docker run -d -p 3001:3001 --name yapi --net tools-net --ip 172.18.0.3 yapi"
exit 1;
fi
node /api/vendors/server/app.js
exec "$@"
构建
docker build -t yapi .
综合以上,同目录下文件:
build.sh config.json docker-entrypoint.sh Dockerfile sources.list
编写build.sh
function usage(){
echo "usage: sh build.sh <version>"
echo "yapi的版本: https://github.com/YMFE/yapi/releases"
echo "我们将从这里下载: http://registry.npm.taobao.org/yapi-vendor/download/yapi-vendor-\$1.tgz"
}
usage
version=1.5.7
if [ -n "$1" ]; then
version=$1
fi
echo -e "\033[32m download new package (version $version) \033[0m"
wget -O yapi.tgz http://registry.npm.taobao.org/yapi-vendor/download/yapi-vendor-$version.tgz
echo -e "\033[32m build new image \033[0m"
docker build -t yapi .
执行构建:
sh build.sh 1.5.7
运行YAPI
docker run yapi --help
Usage:
初始化db并启动: docker run -d -p 3001:3001 --name yapi --net tools-net --ip 172.18.0.3 yapi --initdb
初始化后的账号为config.json 配置的邮箱, 密码为ymfe.org
直接启动: docker kill yapi && docker rm yapi && docker run -d -p 3001:3001 --name yapi --net tools-net --ip 172.18.0.3 yapi
查看:
# docker logs --tail 10 yapi
log: mongodb load success...
初始化管理员账号成功,账号名:"ryan.miao@demo.com",密码:"ymfe.org"
log: 服务已启动,请打开下面链接访问:
http://127.0.0.1:3001/
log: mongodb load success...
Nginx反向代理
YApi页面使用websocket, 所以,nginx配置需要支持ws:
upstream yapi {
server 192.168.5.9:80 weight=5;
}
server {
listen 80;
server_name yapi.demo.com;
access_log /data/log/nginx/yapi/access.log main;
location / {
proxy_pass http://yapi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
}
}
挂域名
购买域名,指向我们的nginx即可。
关注我的公众号