centOS下 docker部署Yapi(解决yapi.demo.qunar.com问题)

2 篇文章 0 订阅
2 篇文章 0 订阅

一,使用Docker创建Yapi容器

1,先安装docker,参考另一篇文章,可用docker version查看docker版本

[root@localhost ~]# docker version
Client:
 Version:    17.12.1-ce
 API version:    1.35
 Go version:    go1.9.4
 Git commit:    7390fc6
 Built:    Tue Feb 27 22:15:20 2018
 OS/Arch:    linux/amd64

Server:
 Engine:
  Version:    17.12.1-ce
  API version:    1.35 (minimum version 1.12)
  Go version:    go1.9.4
  Git commit:    7390fc6
  Built:    Tue Feb 27 22:17:54 2018
  OS/Arch:    linux/amd64
  Experimental:    false    

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

[root@localhost ~]# docker pull silsuer/yapi
Using default tag: latest
latest: Pulling from silsuer/yapi
1be7f2b886e8: Pull complete 
6fbc4a21b806: Pull complete 
c71a6f8e1378: Pull complete 
... 

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

[root@localhost ~]# docker run --name yapi -dit -p 27017:27017 -p 9090:9090 -p 3000:3000 silsuer/yapi bash
3d3a94296f11c725ff091d52acb2e2a4bdb19d55354a0725f1e8b742c0a93ce7  

备注:如果想要更改默认端口可这样写,如下 

[root@localhost ~]# docker run --name yapi -dit -p XXXXX:27017 -p 9090:9090 -p XXXX:3000 silsuer/yapi bash

27017是mongo的默认端口,3000是yapi的默认端口 

4,(可选)查看当前正运行的容器,docker ps

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                                                      NAMES
3d3a94296f11        silsuer/yapi        "bash"              25 minutes ago      Up About a minute   0.0.0.0:3000->3000/tcp, 0.0.0.0:9090->9090/tcp, 0.0.0.0:27017->27017/tcp   yapi

5,进入容器,有两种方式:docker attach yapi (exit退出容器后,容器停止运行)或者docker exec -it yapi bash(exit退出容器后,容器依旧运行)

[root@localhost ~]# docker attach yapi(容器id/容器名字)
root@3d3a94296f11:/#                            ------(这就是伪终端,进入容器后键入才显示)
[root@localhost ~]# docker exec -it  yapi(容器id/容器名字) bash
root@3d3a94296f11:/#                            ------(这就是伪终端,进入容器后键入才显示)

二,容器中部署Yapi,以下命令是在容器中执行

 1,启动MongoDB,Yapi部署需要数据库的依赖。(上面pull的镜像中带有mongodb,版本是v3.6.3)

root@3d3a94296f11:/# service mongodb start
 * Starting database mongodb                                           [ OK ] 

2,Yapi需要Git支持,需要先升级Git源(如果不先安装git直接部署yapi的话湖出现错误的)

root@3d3a94296f11:/# apt-get update
Ign:1 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 InRelease
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Get:3 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release [3457 B]
...

3,安装git,中间会需要输入y同意安装

root@3d3a94296f11:/# 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:
...

安装过程中可能会出现几个error,不用管正常现象。

4,查看升级后的git版本 

root@3d3a94296f11:/# git --version
git version 2.7.4

5,启动Yapi服务进行部署配置,会有如下提示

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

启动后,提示你在浏览器打开 http://0.0.0.0:9090 访问,我用的阿里云服务器,访问http://0.0.0.0:9090是不可能成功的,我这里是访问http://ip:9090才可显示

PS:到这里正常来讲浏览器访问会出现配置Yapi的界面,但是由于之前的官网yapi.demo.qunar.com已经不再维护,导致访问页面就会出现:

(node:879) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND yapi.demo.qunar.com yapi.demo.qunar.com:80
    at errnoException (dns.js:55:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:26)
(node:879) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:879) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:879) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND yapi.demo.qunar.com yapi.demo.qunar.com:80

 所以这里我们早docker中采用其他方式来安装Yapi:

root@5ad780d91c4a:/# npm install -g yapi-cli --registry https://registry.npm.taobao.org
npm WARN deprecated axios@0.16.2: Critical security vulnerability fixed in v0.21.1. For more information, see https://github.com/axios/axios/pull/3410
npm WARN deprecated bson@1.0.9: Fixed a critical issue with BSON serialization documented in CVE-2019-2391, see https://bit.ly/2KcpXdo for more details
/usr/bin/yapi -> /usr/lib/node_modules/yapi-cli/bin/yapi-cli
/usr/bin/yapi-cli -> /usr/lib/node_modules/yapi-cli/bin/yapi-cli
+ yapi-cli@1.5.0
added 17 packages, removed 13 packages and updated 83 packages in 12.513s


   ╭─────────────────────────────────────╮
   │                                     │
   │   Update available 5.6.0 → 8.1.3    │
   │     Run npm i -g npm to update      │
   │                                     │
   ╰─────────────────────────────────────╯

现在重新启动服务:

root@5ad780d91c4a:/# yapi server
在浏览器打开 http://0.0.0.0:9090 访问。非本地服务器,请将 0.0.0.0 替换成指定的域名或ip

然后访问页面:

7,配置完成后,点击开始部署,如下图

  

 备注:如果有报错,根据部署日志上的信息查找原因。我在安装的时候有如下一个错误

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

 这个错误是因为mongo版本的问题,这个不用管,可以正常使用。 

8,部署成功后,查看命令窗口,显示如下 

 ...

 added 345 packages in 53.839s

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

 log: mongodb load success...

 初始化管理员账号成功,账号名:"admin@admin.com",密码:"ymfe.org"

部署成功,请切换到部署目录,输入: "node vendors/server/app.js" 指令启动服务器, 然后在浏览器打开 http://127.0.0.1:3000或者http://ip:3000(放开3000端口) 访问

9,根据上面信息提示,记住账户名和密码,按ctrl+c停止安装,切换到部署目录(/my-yapi/),输入:"node vendors/server/app.js" 指令。

root@3d3a94296f11:/# cd /my-yapi/
root@3d3a94296f11:/my-yapi# node vendors/server/app.js
log: the server is start at 127.0.0.1:3000
log: mongodb load success...

 这里的300端口是Yapi的默认端口,如果上面docker启动时部署更改了默认端口,需要改成启动时的端口才可。

10,在浏览器中输入:http://ip:3000或者http://127.0.0.1:3000,(安全组放开3000端口,如果你更改了默认端口就是用更改后的端口)使用上面记住的账号密码就可以登录了,如下图

备注1:现在的yapi进程不是后台启动的,如果按ctrl+c退出,那么yapi也会关掉,所以使用nohup将yapi放置后台运行: 

root@3d3a94296f11:/my-yapi# node vendors/server/app.js
log: the server is start at 127.0.0.1:3000
log: mongodb load success...
^C
root@3d3a94296f11:/my-yapi# nohup node /my-yapi/vendors/server/app.js 2>&1 &
[1] 91
root@3d3a94296f11:/my-yapi# nohup: ignoring input and appending output to 'nohup.out'

root@3d3a94296f11:/my-yapi# 

 备注2:如果开始是以docker exec -ti yapi bash方式进入的容器,输入exit就可以了,以docker attach方式进入的容器,按ctrl+p+q退出

登录后的管理界面如下图 :

OK!完成~ 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值