本篇内容主要讲解“怎么用docker创建app”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用docker创建app”吧!
docker-app 将docker容器编排为一个bundle,命名为 application 应用程序。希望将一组docker容器打包为一个应用程序进行分发。
启用app插件
app 是一个体验特性,默认不会启用,最低版本为 19.03 ,因此首先要确保docker引擎不低于此版本。
编辑 ~/.docker/config.json
,增加:
{
"experimental": "enabled",
"debug": true
}
检查:
docker
...
app* Docker Application (Docker Inc., v0.8.0)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.3.1-tp-docker)
checkpoint Manage checkpoints
...
标记 *
的指令为体验特性指令。
# docker app version
Version: v0.8.0
Git commit: 7eea32b
Built: Wed Nov 13 07:28:05 2019
OS/Arch: linux/amd64
Experimental: off
Renderers: none
Invocation Base Image: docker/cnab-app-base:v0.8.0
创建app
在当前工作目录初始化一个docker应用程序。
# docker app init myapp
# tree .
.
└── myapp.dockerapp
├── docker-compose.yml
├── metadata.yml
└── parameters.yml
1 directory, 3 files
默认地,会在工作目录创建一个项目目录,其中包含了三个文件:
docker-compose.yml 服务的编排文件
metadata.yml 项目的基本配置文件
parameters.yml 服务compose引用的变量
创建单文件项目:众做周知,分发一个目录比分发单个文件要麻烦的多,因此docker-app 支持将应用程序的项目文件整合为一个独立的文件。
# docker app init demo --single-file
# ll
-rw-r--r--. 1 root root 508 Dec 2 07:13 demo.dockerapp
项目文件并不是创建后就不可更改了,你随时可以将目录格式的应用转换为单文件:
docker app merge myapp
反过来讲单文件项目转换为目录格式:
docker app split myapp
编辑服务
vi myapp.dockerapp/docker-compose.yml
version: "3.6"
services: {
"nginx": {
"image": "nginx"
}
}
安装app
# docker app install myapp
# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
i2ceu8f82bsh myapp_nginx replicated 1/1 nginx:latest
# docker app ls
INSTALLATION APPLICATION LAST ACTION RESULT CREATED MODIFIED REFERENCE
myapp myapp (0.1.0) install success 6 seconds 4 seconds
查看服务
使用render 子命令可以查看docker应用的服务编排:
# docker app render myapp
version: "3.6"
services:
nginx:
deploy:
replicas: 2
image: nginx
如果myapp 项目文件不在当前目录,和镜像一样,docker引擎会首先去中央仓库拉取。
使用参数
修改parameters.json 文件:
"DEPLOY_REPLICAS": 5
修改docker-compose引用该变量:
version: "3.6"
services: {
"nginx": {
"image": "nginx",
"deploy": {
"replicas": "${DEPLOY_REPLICAS}"
}
}
}
查看服务:
# docker app render myapp
version: "3.6"
services:
nginx:
deploy:
replicas: 5
image: nginx
多级变量:参数配置文件是yaml格式,很容易想到 —— 可以使用多级参数名,这样就可以对不同的变量进行分组。
deploy:
replicas: 2
对应的compose中:
"replicas": "${deploy.replicas}"
更新app
执行更新:
# docker app upgrade myapp -s DEPLOY_REPLICAS=2
Updating service myapp_nginx (id: t9n7hcic2ek42qhn35kyfh3lj)
Application "myapp" upgraded on context "default"
# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ud4sms7cx9yx myapp_nginx replicated 2/2 nginx:latest
删除应用
docker app uninstall myapp
发布应用
docker login 192.168.33.10:5000
docker app push myapp
但是失败了:
# docker app push myapp
192.168.33.10:5000/library/myapp:0.1.0-invoc
fixing up "192.168.33.10:5000/library/myapp:0.1.0" for push: failed to resolve "192.168.33.10:5000/library/myapp:0.1.0-invoc", push the image to the registry before pushing the bundle: failed to do request: Head https://192.168.33.10:5000/v2/library/myapp/manifests/0.1.0-invoc: http: server gave HTTP response to HTTPS client
尝试不修改metadata中的name,在命令行中使用tag参数:
# docker app push myapp -t 192.168.33.10:5000/library/myapp
192.168.33.10:5000/library/myapp:latest-invoc
fixing up "192.168.33.10:5000/library/myapp:latest" for push: failed to resolve "192.168.33.10:5000/library/myapp:latest-invoc", push the image to the registry before pushing the bundle: failed to do request: Head https://192.168.33.10:5000/v2/library/myapp/manifests/latest-invoc: http: server gave HTTP response to HTTPS client
- 可以在docker images中看到指定tag的镜像,但是push还是失败
使用https协议的仓库,仍然失败。
对于私服,暂时没办法解决,官网文档不多。
常用推送到中央仓库,通过:
docker app push myapp -t pollyduan/myapp
红蓝日志闪烁,看起来正常了。但结局还是残酷的:
fixing up "docker.io/pollyduan/myapp:latest" for push: failed commit on ref "manifest-sha256:f5dc44557f1d666381791c3d01300d64899ba7b74dc26f4d681bd1827caf61ca": no response
登录hub.docker.com查看,镜像存在了。
到此,相信大家对“怎么用docker创建app”有了更深的了解,不妨来实际操作一番吧!这里是天达云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!