Docker 之部署YAPI

前言

本小节我们学习一下使用Docker部署YAPI。

YAPI

YApi 是由去哪儿移动架构组推出的一款开源项目,是高效、易用、功能强大的 api 管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务。
官网:https://yapi.ymfe.org/,Yapi具有以下功能特性:

  • 权限管理
    扁平化权限设计,即保证了大型企业级项目的管理,又保证了易用性。
  • 可视化接口管理
    基于 websocket 的多人协作接口编辑功能和类 postman 测试工具,让多人协作成倍提升开发效率。
  • Mock Server
    易用的 Mock Server,MockServer 除支持普通的随机 mock 外,还增加了 Mock 期望功能,根据设置的请求过滤规则,返回期望数据。
  • 自动化测试(这个我比较关注)
    完善的接口自动化测试,保证数据的正确性,支持对 Response 断言。
  • 数据导入
    接口支持导入 swagger, postman, har 数据格式,方便迁移旧项目。
  • 插件机制
    强大的插件机制,满足各类业务需求。

案例

  • 安装Docker(略)

  • 从仓库中拉取Yapi镜像,docker pull命令

    docker pull silsuer/yapi
    
  • 运行镜像

    docker run --name yapi -dit -p 27017:27017 -p 9090:9090 -p 3000:3000 silsuer/yapi bash
    

    使用镜像实例并运行容器,–name是指定容器名,-dit是指后台运行容器,并返回容器id(或者-it,指在当前窗口以伪终端进入容器,这里如果只用-d,容器启动后就会马上停止),-p是指端口映射(主机端口:容器端口,27017是数据库端口,9090是yapi初始化配置端口,3000是yapi实际运行端口)

  • 进入容器

    [root@iZwz92qakfc7zvdr9h0gipZ yapi]# docker attach yapi
    root@efd41c325e16:/# 
    root@efd41c325e16:/# ls
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    
     docker exec -it yapi /bin/bash
    
  • 容器中部署安装Yapi

    • 启动MogoDB,Yapi部署需要数据库依赖

      root@efd41c325e16:/# service mongodb start
       * Starting database mongodb 
      
    • 先升级git源

      apt-get update
      
      root@efd41c325e16:/# apt-get update
      Ign:1 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 InRelease
      Get:2 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release [3457 B]                        
      Get:3 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release.gpg [801 B]                     
      Get:4 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 Packages [15.3 kB]
      Get:5 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]    
      Get:6 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse arm64 Packages [15.3 kB] 
      Ign:7 https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 InRelease             
      Hit:8 http://archive.ubuntu.com/ubuntu xenial InRelease  
      Get:9 https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 Release [2495 B]                               
      Get:10 https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 Release.gpg [801 B]                                
      Err:10 https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 Release.gpg                                          
        The following signatures were invalid: KEYEXPIRED 1544811256
      Get:11 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [136 kB]                                
      Get:12 https://deb.nodesource.com/node_9.x xenial InRelease [4622 B]
      Get:13 https://deb.nodesource.com/node_9.x xenial/main amd64 Packages [765 B]            
      Get:14 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]              
      Get:15 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [936 kB]                                          
      Get:16 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]                                                                         
      Get:17 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [581 kB]                                                                            
      Get:18 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6119 B]                                                                          
      Get:19 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]                                                                                          
      Ign:19 http://archive.ubuntu.com/ubuntu xenial-backports InRelease                                                                                                   
      Get:20 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [328 kB]                                                                                     
      Get:21 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1319 kB]
      Get:22 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]                                                                           
      Get:23 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [986 kB]                                                                              
      Get:24 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.1 kB]                                                                           
      Get:25 http://archive.ubuntu.com/ubuntu xenial-backports Release [106 kB]                                                                                            
      Get:26 http://archive.ubuntu.com/ubuntu xenial-backports Release.gpg [916 B]                                                                                         
      Get:27 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]                                                                                
      Get:28 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8807 B]
      Fetched 4723 kB in 5min 22s (14.6 kB/s)
      Reading package lists... Done
      W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://repo.mongodb.org/pt/ubuntu trusty/mongodb-org/3.6 Release: The following signatures were invalid: KEYEXPIRED 1544811256
      W: Failed to fetch https://repo.mongodb.org/apt/ubuntu/dists/trusty/mongodb-org/3.6/Release.gpg  The following signatures were invalid: KEYEXPIRED 1544811256
      W: Some index files failed to download. They have been ignored, or old ones used instead.
      

      在升级git源时我们可以看到如上log抛出了一个Error信息(如果没有出现请直接到下一步):

      Err:10 https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 Release.gpg                                          
        The following signatures were invalid: KEYEXPIRED 1544811256
      

      这是由于docker容器里面运行apt-get的该容器的镜像比较旧,所以导致签名过期,解决办法:
      1.首先找到过期的key

       sudo apt-key list | grep -A 1 expired
      
      root@efd41c325e16:/# apt-key list | grep -A 1 expired
      pub   4096R/91FA4AD5 2016-12-14 [expired: 2018-12-14]
      uid                  MongoDB 3.6 Release Signing Key 
      

      2.更新过期的key并再次校验

      apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 91FA4AD5
      
      root@efd41c325e16:/# apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 91FA4AD5
      Executing: /tmp/tmp.0p8fyY3L0w/gpg.1.sh --keyserver
      hkp://keyserver.ubuntu.com:80
      --recv-keys
      91FA4AD5
      gpg: requesting key 91FA4AD5 from hkp server keyserver.ubuntu.com
      gpg: key 91FA4AD5: "MongoDB 3.6 Release Signing Key " 1 new signature
      gpg: Total number processed: 1
      gpg:         new signatures: 1
      root@efd41c325e16:/# apt-key list | grep -A 1 expired
      
    • 安装git,中途需要输入y同意安装

      apt-get install git
      
      root@efd41c325e16:/# apt-get install git
      Reading package lists... Done
      Building dependency tree       
      Reading state information... Done
      The following packages were automatically installed and are no longer required:
        libboost-filesystem1.58.0 libboost-program-options1.58.0 libboost-system1.58.0 libboost-thread1.58.0 libgoogle-perftools4 libpcap0.8 libpcrecpp0v5 libsnappy1v5
        libtcmalloc-minimal4 libunwind8 libv8-3.14.5 libyaml-cpp0.5v5
      Use 'apt autoremove' to remove them.
      The following additional packages will be installed:
        git-man ifupdown iproute2 isc-dhcp-client isc-dhcp-common less libatm1 libbsd0 libdns-export162 libedit2 liberror-perl libgdbm3 libisc-export160 libmnl0
        libperl5.22 libpopt0 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 libxtables11 netbase openssh-client patch perl perl-base perl-modules-5.22
        rename rsync xauth
      Suggested packages:
      
      
  • 查看升级后的get版本

    root@efd41c325e16:/# git --version
    git version 2.7.4
    root@efd41c325e16:/# 
    
    
  • 启动Yapi服务进行部署配置

    root@efd41c325e16:/# yapi server
    在浏览器打开 http://0.0.0.0:9090 访问。非本地服务器,请将 0.0.0.0 替换成指定的域名或ip 
    /bin/sh: 1: xdg-open: not found
    

-访问http://你的公网IP:9090,并配置YAPI
Docker 之部署YAPI_第1张图片
安装时出现了一个问题:
Docker 之部署YAPI_第2张图片

开始下载平台文件压缩包...
http://yapi.demo.qunar.com/publicapi/archive/v1.4.1
部署文件完成,正在安装依赖库...
 
> [email protected] install /my-yapi/vendors/node_modules/dtrace-provider
> node-gyp rebuild || node suppress-error.js


 gyp
  ERR! build error 

 gyp
  ERR! stack
  Error: not found: make
gyp ERR! 
 stack     at getNotFoundError (/usr/lib/node_modules/npm/node_modules/which/which.js:13:12)
gyp 
 ERR! stack     at F (/usr/lib/node_modules/npm/node_modules/which/which.js:68:19)

 gyp ERR! stack
      at E (/usr/lib/node_modules/npm/node_modules/which/which.js:80:29)
gyp ERR!
  stack     at /usr/lib/node_modules/npm/node_modules/which/which.js:89:16
gyp 
 ERR! stack     at /usr/lib/node_modules/npm/node_modules/which/node_modules/isexe/index.js:42:5
gyp
  ERR! 
 stack     at /usr/lib/node_modules/npm/node_modules/which/node_modules/isexe/mode.js:8:5
gyp ERR!
  stack     at FSReqWrap.oncomplete (fs.js:170:21)

 gyp 
 ERR! System Linux 3.10.0-957.27.2.el7.x86_64

 gyp
  ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"

 gyp ERR!
  cwd /my-yapi/vendors/node_modules/dtrace-provider

 gyp ERR! 
 node -v v9.6.1

 gyp ERR! node-gyp -v
  v3.6.2
gyp 
 ERR! not ok 

 
> [email protected] postinstall /my-yapi/vendors/node_modules/jsonpath
> node lib/aesprim.js > generated/aesprim-browser.js


 npm 
 WARN [email protected] license should be a valid SPDX license expression
npm WARN The package moment is included as both a dev and production dependency.

 

 added 347 packages in 19.915s

依赖库安装完成,正在初始化数据库mongodb...
 
> [email protected] install-server /my-yapi/vendors
>  node server/install.js


Error:  (node:1438) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

初步预计时npm安装时出错了,带排查,不过不影响我们运行并部署yapi。

  • Ctrl+C退出,然后进入/my-yapi/,运行

    node vendors/server/app.js
    
    root@b0f2d55673f3:/# cd /my-yapi/
    root@b0f2d55673f3:/my-yapi# node vendors/server/app.js
    log: 服务已启动,请打开下面链接访问: 
    http://127.0.0.1:3000/
    (node:1449) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
    log: mongodb load success...
    
    
  • Ctrl+C 退出,使YAPI在后台运行

    root@b0f2d55673f3:/# cd /my-yapi/
    root@b0f2d55673f3:/my-yapi# node vendors/server/app.js
    log: 服务已启动,请打开下面链接访问: 
    http://127.0.0.1:3000/
    (node:1449) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
    log: mongodb load success...
    ^C
    root@b0f2d55673f3:/my-yapi# nohup node /my-yapi/vendors/server/app.js 2>&1 &
    [1] 1460
    root@b0f2d55673f3:/my-yapi# nohup: ignoring input and appending output to 'nohup.out'
    
    root@b0f2d55673f3:/my-yapi# exit 
    exit
    [root@iZwz92qakfc7zvdr9h0gipZ workspace]#
    
  • 访问http://你的公网IP:3000

Docker 之部署YAPI_第3张图片

使用[email protected]/ymfe.org 口令登录。

Docker 之部署YAPI_第4张图片
到此YAPI就安装成功了。

小结

  • 停止YAPI容器

    [root@iZwz92qakfc7zvdr9h0gipZ workspace]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                                                      NAMES
    b0f2d55673f3        silsuer/yapi        "bash"              23 minutes ago      Up 23 minutes       0.0.0.0:3000->3000/tcp, 0.0.0.0:9090->9090/tcp, 0.0.0.0:27017->27017/tcp   yapi
    [root@iZwz92qakfc7zvdr9h0gipZ workspace]# docker stop yapi
    yapi
    [root@iZwz92qakfc7zvdr9h0gipZ workspace]# 
    
    
  • 每次启动/重启YAPI必做的操作:

    [root@iZwz92qakfc7zvdr9h0gipZ workspace]# docker start yapi
    yapi
    [root@iZwz92qakfc7zvdr9h0gipZ workspace]# docker exec -it yapi /bin/bash
    root@b0f2d55673f3:/# service mongodb start
     * Starting database mongodb                                                                                                                                                 [ OK ] 
    root@b0f2d55673f3:/# cd /my-yapi/
    root@b0f2d55673f3:/my-yapi# node vendors/server/app.js
    log: 服务已启动,请打开下面链接访问: 
    http://127.0.0.1:3000/
    (node:86) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
    log: mongodb load success...
    ^C
    root@b0f2d55673f3:/my-yapi# nohup node /my-yapi/vendors/server/app.js 2>&1 &
    [1] 97
    root@b0f2d55673f3:/my-yapi# nohup: ignoring input and appending output to 'nohup.out'
    
    root@b0f2d55673f3:/my-yapi# 
    
    
    • docker start yapi :启动yapi Docker容器
    • docker exec -it yapi /bin/bash : 进入YAPI Docker 容器
    • service mongodb start : 启动monggodb服务
    • cd /my-yapi/ :进入部署路径
    • node vendors/server/app.js :启动服务
    • nohup node /my-yapi/vendors/server/app.js 2>&1 & :服务后台运行

你可能感兴趣的:(Docker)