Fork me on GitHub

Docker 部署 YAPI

一、docker部署YAPI

1、运行 MongoDB

# 创建存储卷
docker volume create mongo-data

# 启动 MongoDB
docker run -d \
  --name mongo-yapi \
  -v mongo-data:/data/db \
  -e MONGO_INITDB_ROOT_USERNAME=anoyi \
  -e MONGO_INITDB_ROOT_PASSWORD=anoyi.com \
  mongo

2、获取 YAPI 镜像,其他版本:阿里云镜像仓库

docker pull registry.cn-hangzhou.aliyuncs.com/anoyi/yapi

3、自定义配置文件 config.json

{
  "port": "3000",
  "adminAccount": "admin@anoyi.com",
  "timeout":120000,
  "db": {
    "servername": "mongo",
    "DATABASE": "yapi",
    "port": 27017,
    "user": "anoyi",
    "pass": "anoyi.com",
    "authSource": "admin"
  }
}

4、初始化 YAPI 数据库索引及管理员账号

docker run -it --rm \
  --link mongo-yapi:mongo \
  --entrypoint npm \
  --workdir /yapi/vendors \
  -v /opt/data/tmp1/SCS/config.json:/yapi/config.json \
  registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
  run install-server

5、启动 Yapi 服务

docker run -d \
  --name yapi \
  --link mongo-yapi:mongo \
  --workdir /yapi/vendors \
  -p 3000:3000 \
  -v /opt/data/tmp1/SCS/config.json:/yapi/config.json \
  registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
  server/app.js

6、使用 YAPI

访问: http://localhost:3000
登录账号:admin@anoyi.com
密码:ymfe.org

 遇到的问题:

1)在镜像中启动服务报错:Error: EROFS: read-only file system, mkdir '/sys/fs/cgroup/cpu/safeify

2)断言功能不可用,assert.equal is not a function

解决方案:

进入yapi容器内部:docker exec -u root -it 94e610198b4c /bin/sh

进入容器指定目录/yapi/vendors/server/utils  修改sandbox.js文件内容

const Safeify = require('safeify').default;

module.exports = async function sandboxFn(context, script) {
    // 创建 safeify 实例
    const safeVm = new Safeify({
        timeout: 3000,
        asyncTimeout: 60000,
        unrestricted: true,
        unsafe: {
           modules: {
                assert: 'assert'
         }
       }


    });
    safeVm.preset('const assert= require("assert");')
    script += `; return {
        Function: this.Function,
        eval: this.eval,
        header: this.header,
        query: this.query,
        body: this.body,
        mockJson: this.mockJson,
        params: this.params,
        resHeader: this.resHeader,
        httpCode: this.httpCode,
        delay: this.delay,
        Random: this.Random,
        cookie: this.cookie
    }`;

    // 执行动态代码
    const result = await safeVm.run(script, context)

    // 释放资源
    safeVm.destroy()
    return result
}

重启容器问题解决: docker restart 94e610198b4c

参考链接:

https://www.jianshu.com/p/a97d2efb23c5  

 

  

 

  

  

posted @ 2023-08-17 23:51  橘子偏爱橙子  阅读(937)  评论(0编辑  收藏  举报