Docker App应用

这是一个实验特性。

实验性功能提供了对未来产品功能的早期访问。这些特性仅用于测试和反馈,因为它们可能在没有警告的情况下在不同版本之间更改,或者可以从将来的版本中完全删除。在生产环境中不得使用实验性功能。

Docker不支持实验特性。

要在Docker CLI中启用实验性功能,请编辑config.json文件并将“实验”设置为“已启用”。

要从Docker桌面菜单启用实验功能,请单击设置(macOS上的首选项)>命令行,然后启用启用启用实验功能切换。单击应用并重新启动。

有关Docker CLI中当前实验功能的列表,请参阅Docker CLI实验功能。

Overview

Docker App是一个CLI插件,它引入了一个顶级的Docker App命令,为应用程序带来容器体验。下表比较了Docker容器和Docker应用程序。

有了Docker应用程序,整个应用程序现在可以像管理图像和容器一样轻松。例如,Docker App允许使用Docker App命令构建、验证和部署应用程序。甚至可以利用安全的供应链功能,例如签名的推拉操作。

注:docker应用程序可与docker 19.03或更高版本配合使用。

本指南将引导了解两种情况:

从头开始初始化并部署新的Docker应用程序项目。

将现有的Compose应用程序转换为Docker应用程序项目(稍后在beta过程中添加)。

第一个场景描述Docker应用程序的基本组件以及工具和工作流。

从头开始初始化并部署新的Docker应用程序项目

本节介绍创建新Docker应用程序项目的步骤,熟悉工作流和最重要的命令。

l  Prerequisites

l  Initialize an empty new project

l  Populate the project

l  Validate the app

l  Deploy the app

l  Push the app to Docker Hub

l  Install the app directly from Docker Hub

先决条件

你需要至少一个Docker节点在Swarm模式下运行。还需要包含appcli插件的Docker CLI的最新版本。

根据Linux发行版和安全上下文,可能需要在命令前面添加sudo。

初始化新的空项目

docker app init命令用于初始化新的docker应用程序项目。如果运行它,它会初始化一个新的空项目。如果把它指向一个现有的docker-compose.yml文件,它将基于合成文件初始化新项目。

使用以下命令初始化名为“hello world”的新空项目。

$ docker app init --single-file hello-world

Created "hello-world.dockerapp"

该命令在当前目录中生成一个名为hello的hello-world.dockerapp。文件名的格式附加了“.dockerapp”。

$ ls

hello-world.dockerapp

If you run docker app init without the --single-file flag, you get a new directory containing three YAML files. The name of the directory is the name of the project with .dockerapp appended, and the three YAML files are:

  • docker-compose.yml
  • metadata.yml
  • parameters.yml

However, the --single-file option merges the three YAML files into a single YAML file with three sections. Each of these sections relates to one of the three YAML files mentioned previously: docker-compose.yml, metadata.yml, and parameters.yml. Using the --single-file option enables you to share your application using a single configuration file.

Inspect the YAML with the following command.

$ cat hello-world.dockerapp

# Application metadata - equivalent to metadata.yml.

version: 0.1.0

name: hello-world

description:

---

# Application services - equivalent to docker-compose.yml.

version: "3.6"

services: {}

---

# Default application parameters - equivalent to parameters.yml.

你的文件可能更详细。

请注意,这三个部分中的每一部分都由一组三个破折号(“--”)分隔开。快速描述每个部分。

文件的第一部分指定标识元数据,例如名称、版本、描述和维护者。它接受key-value pairs。文件的这一部分可以是一个单独的文件,名为metadata.yml

文件的第二部分描述了应用程序。

它可以是一个名为docker的docker-compose.yml.

最后一节指定应用程序参数的默认值。它可以是一个名为parameters.yml

填充项目

本节介绍如何编辑project YAML文件,以便它运行一个简单的web应用程序。

使用首选的编辑器编辑hello-world.dockerapp YAML文件并使用以下信息更新应用程序部分:

version: "3.6"

services:

hello:

image: hashicorp/http-echo

command: ["-text", "${hello.text}"]

ports:

- ${hello.port}:5678

Update the Parameters section to the following:

hello:

port: 8080

text: Hello world!

The sections of the YAML file are currently order-based. This means it’s important they remain in the order we’ve explained, with the metadata section being first, the app section being second, and the parameters section being last. This may change to name-based sections in future releases.

Save the changes.

The application is updated to run a single-container application based on the hashicorp/http-echo web server image. This image has it execute a single command that displays some text and exposes itself on a network port.

Following best practices, the configuration of the application is decoupled from the application itself using variables. In this case, the text displayed by the app and the port on which it will be published are controlled by two variables defined in the Parameters section of the file.

Docker App provides the inspect subcommand to provide a prettified summary of the application configuration. It is a quick way to check how to configure the application before deployment, without having to read the Compose file. It’s important to note that the application is not running at this point, and that the inspect operation inspects the configuration file(s).

$ docker app inspect hello-world.dockerapp

hello-world 0.1.0

Service (1) Replicas Ports Image

----------- -------- ----- -----

hello       1        8080  hashicorp/http-echo

Parameters (2) Value

-------------- -----

hello.port     8080

hello.text     Hello world!

docker app inspect operations fail if the Parameters section doesn’t specify a default value for every parameter expressed in the app section.

The application is ready to be validated and rendered.

Validate the app

Docker App provides the validate subcommand to check syntax and other aspects of the configuration. If the app passes validation, the command returns no arguments.

$ docker app validate hello-world.dockerapp

Validated "hello-world.dockerapp"

docker app validate operations fail if the Parameters section doesn’t specify a default value for every parameter expressed in the app section.

As the validate operation has returned no problems, the app is ready to be deployed.

Deploy the app

There are several options for deploying a Docker App project.

  • Deploy as a native Docker App application
  • Deploy as a Compose app application
  • Deploy as a Docker Stack application

All three options are discussed, starting with deploying as a native Docker App application.

Deploy as a native Docker App

The process for deploying as a native Docker app is as follows:

Use docker app install to deploy the application.

Use the following command to deploy (install) the application.

$ docker app install hello-world.dockerapp --name my-app
Creating network my-app_default
Creating service my-app_hello
Application "my-app" installed on context "default"

By default, docker app uses the current context to run the installation container and as a target context to deploy the application. You can override the second context using the flag --target-context or by using the environment variable DOCKER_TARGET_CONTEXT. This flag is also available for the commands status, upgrade, and uninstall.

$ docker app install hello-world.dockerapp --name my-app --target-context=my-big-production-cluster
Creating network my-app_default
Creating service my-app_hello
Application "my-app" installed on context "my-big-production-cluster"

Note: Two applications deployed on the same target context cannot share the same name, but this is valid if they are deployed on different target contexts.

You can check the status of the app with the docker app status <app-name> command.

$ docker app status my-app
INSTALLATION
------------
Name:         my-app
Created:      35 seconds
Modified:     31 seconds
Revision:     01DCMY7MWW67AY03B029QATXFF
Last Action:  install
Result:       SUCCESS
Orchestrator: swarm
 
APPLICATION
-----------
Name:      hello-world
Version:   0.1.0
Reference:
 
PARAMETERS
----------
hello.port: 8080
hello.text: Hello, World!
 
STATUS
------
ID              NAME            MODE          REPLICAS    IMAGE             PORTS
miqdk1v7j3zk    my-app_hello    replicated    1/1         hashicorp/http-echo:latest   *:8080->5678/tcp

The app is deployed using the stack orchestrator. This means you can also inspect it using the regular docker stack commands.

$ docker stack ls
NAME                SERVICES            ORCHESTRATOR
my-app              1                   Swarm

Now that the app is running, you can point a web browser at the DNS name or public IP of the Docker node on port 8080 and see the app. You must ensure traffic to port 8080 is allowed on the connection from your browser to your Docker host.

Now change the port of the application using docker app upgrade <app-name> command.

$ docker app upgrade my-app --set hello.port=8181
Upgrading service my-app_hello
Application "my-app" upgraded on context "default"

You can uninstall the app with docker app uninstall my-app.

Deploy as a Docker Compose app

The process for deploying as a Compose app comprises two major steps:

  1. Render the Docker app project as a docker-compose.yml file.
  2. Deploy the app using docker-compose up.

You need a recent version of Docker Compose to complete these steps.

Rendering is the process of reading the entire application configuration and outputting it as a single docker-compose.yml file. This creates a Compose file with hard-coded values wherever a parameter was specified as a variable.

Use the following command to render the app to a Compose file called docker-compose.yml in the current directory.

$ docker app render --output docker-compose.yml hello-world.dockerapp

Check the contents of the resulting docker-compose.yml file.

$ cat docker-compose.yml
version: "3.6"
services:
  hello:
    command:
    - -text
    - Hello world!
    image: hashicorp/http-echo
    ports:
    - mode: ingress
      target: 5678
      published: 8080
      protocol: tcp

Notice that the file contains hard-coded values that were expanded based on the contents of the Parameters section of the project’s YAML file. For example, ${hello.text} has been expanded to “Hello world!”.

Note: Almost all the docker app commands propose the --set key=value flag to override a default parameter.

Try to render the application with a different text:

$ docker app render hello-world.dockerapp --set hello.text="Hello whales!" 
version: "3.6"
services:
  hello:
    command:
    - -text
    - Hello whales!
    image: hashicorp/http-echo
    ports:
    - mode: ingress
      target: 5678
      published: 8080
      protocol: tcp

Use docker-compose up to deploy the app.

$ docker-compose up --detach
WARNING: The Docker Engine you're using is running in swarm mode.
<Snip>

The application is now running as a Docker Compose app and should be reachable on port 8080 on your Docker host. You must ensure traffic to port 8080 is allowed on the connection form your browser to your Docker host.

You can use docker-compose down to stop and remove the application.

Deploy as a Docker Stack

Deploying the app as a Docker stack is a two-step process very similar to deploying it as a Docker Compose app.

  1. Render the Docker app project as a docker-compose.yml file.
  2. Deploy the app using docker stack deploy.

Complete the steps in the previous section to render the Docker app project as a Compose file and make sure you’re ready to deploy it as a Docker Stack. Your Docker host must be in Swarm mode.

$ docker stack deploy hello-world-app -c docker-compose.yml
Creating network hello-world-app_default
Creating service hello-world-app_hello

The app is now deployed as a Docker stack and can be reached on port 8080 on your Docker host.

Use the docker stack rm hello-world-app command to stop and remove the stack. You must ensure traffic to port 8080 is allowed on the connection form your browser to your Docker host.

Push the app to Docker Hub

As mentioned in the introduction, docker app lets you manage entire applications the same way that you currently manage container images. For example, you can push and pull entire applications from registries like Docker Hub with docker app push and docker app pull. Other docker app commands, such as install, upgrade, inspect, and render can be performed directly on applications while they are stored in a registry.

Push the application to Docker Hub. To complete this step, you need a valid Docker ID and you must be logged in to the registry to which you are pushing the app.

By default, all platform architectures are pushed to the registry. If you are pushing an official Docker image as part of your app, you may find your app bundle becomes large with all image architectures embedded. To just push the architecture required, you can add the --platform flag.

$ docker login 
 
$ docker app push my-app --platform="linux/amd64" --tag <hub-id>/<repo>:0.1.0

Install the app directly from Docker Hub

Now that the app is pushed to the registry, try an inspect and install command against it. The location of your app is different from the one provided in the examples.

$ docker app inspect myuser/hello-world:0.1.0
hello-world 0.1.0
 
Service (1) Replicas Ports Image
----------- -------- ----- -----
hello       1        8080  myuser/hello-world@sha256:ba27d460cd1f22a1a4331bdf74f4fccbc025552357e8a3249c40ae216275de96
 
Parameters (2) Value
-------------- -----
hello.port     8080
hello.text     Hello world!

This action was performed directly against the app in the registry.

Now install it as a native Docker App by referencing the app in the registry, with a different port.

$ docker app install myuser/hello-world:0.1.0 --set hello.port=8181
Creating network hello-world_default
Creating service hello-world_hello
Application "hello-world" installed on context "default"

Test that the app is working.

The app used in these examples is a simple web server that displays the text “Hello world!” on port 8181, your app might be different.

$ curl http://localhost:8181
Hello world!

Uninstall the app.

$ docker app uninstall hello-world
Removing service hello-world_hello
Removing network hello-world_default
Application "hello-world" uninstalled on context "default"

You can see the name of your Docker App with the docker stack ls command.

Docker App应用的更多相关文章

  1. 一步步创建第一个Docker App —— 4. 部署应用

    原文:https://docs.docker.com/engine/getstarted-voting-app/deploy-app/ 在这一步中,将会使用第一步提到的 docker-stack.ym ...

  2. 一步步创建第一个Docker App —— 3. 创建一个集群Swarm

    原文:https://docs.docker.com/engine/getstarted-voting-app/create-swarm/ 初始化集群 Swarm 1. 使用 ssh 命令登录 man ...

  3. 一步步创建第一个Docker App —— 1. 背景介绍

    原文:https://docs.docker.com/engine/getstarted-voting-app/#/docker-stacks-and-services 你将会学习什么    本文创建 ...

  4. 一步步创建第一个Docker App —— 2. 创建 Docker化 主机

    原文:https://docs.docker.com/engine/getstarted-voting-app/node-setup/ 部署voting app的第一步,是为集群节点(swarm no ...

  5. Docker Resources

    Menu Main Resources Books Websites Documents Archives Community Blogs Personal Blogs Videos Related ...

  6. Windows 10 安装 Docker for Windows

    Docker for Windows是Docker社区版(CE)应用程序. Docker for Windows安装包包括在Windows系统上运行Docker所需的一切. 本主题介绍了预安装注意事项 ...

  7. dcoker实战,使用docker部署NodeJs应用

    docker简介   docker是一个开源的应用容器引擎,可以为我们提供安全.可移植.可重复的自动化部署的方式.docker采用虚拟化的技术来虚拟化出应用程序的运行环境.如上图一样.docker就像 ...

  8. 开发环境中Docker的使用

    一. Ubuntu16.04+Django+Redis+Nginx的Web项目Docker化 1.创建Django项目的image # 创建项目image 执行 docker build -t ccn ...

  9. mac上Docker安装&初体验

    Docker是什么? Docker是一个虚拟环境容器,可以将你的开发环境.代码.配置文件等一并打包到这个容器中,并发布和应用到任意平台中. 官方文档:https://docs.docker.com H ...

随机推荐

  1. 逆向工程第003篇:跨越CM4验证机制的鸿沟(上)

    一.前言 <冠军足球经理>系列作为一款拟真度极高的足球经营类游戏,赢得过无数赞誉,而CM4可以说是这个传奇的起点.但是在游戏安装过程中,当用户输入完序列号之后,程序并不会对用户的输入进行真 ...

  2. 路由选择协议(RIP/OSPF)

    目录 IGP RIP协议 OSPF协议 IS-IS协议 EIGRP协议 EGP BGP 我们可能会想,在偌大的网络中,我们是如何跟其他人通信的呢?我们是如何跟远在太平洋对面的美国小伙伴对话的呢? 这就 ...

  3. 利用 Windows 线程池定制的 4 种方式完成任务(Windows 核心编程)

    Windows 线程池 说起底层的线程操作一般都不会陌生,Windows 提供了 CreateThread 函数来创建线程,为了同步线程的操作,Windows 提供了事件内核对象.互斥量内核对象.关键 ...

  4. SQL Server 数据库基本使用技巧

    use master; #显示数据库 select top 3 * from spt_values; #显示去前3行 select * from test where id2 like '%1010% ...

  5. 【Mybatis源码解析】- 整体架构及原理

    整体架构 version-3.5.5 在深入了解Mybatis的源码之前,我们先了解一下Mybatis的整体架构和工作原理,这样有助于我们在阅读源码过程中了解思路和流程. 核心流程 在上一遍的入门程序 ...

  6. 从effective C++中窥探C++11特性

    这几天在看effective C++3rd,这本书算是比较经典的一本入门C++的书了.虽然年代比较久远书中讲的好多模式已经被的新特性取代了,但是从这些旧的模式中可以了解到一些C++新特性设计的初衷,也 ...

  7. font

    font属性简写 front: font-style font-variant font-weight font-size/line-height font-family 说明: 值之间空格隔开 注意 ...

  8. 封装axios在util中

    创建util工具类,封装通用的get和post请求 封装axios成工具类,方便大家请求调用 1.创建util文件夹 2.创建request.js 3.封装 //封装请求相关方法 //初始化一个axi ...

  9. 本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法。

    本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法. 一.前言 我希望用windows远程访问centos图形界面.xmanager连接centos远程桌 ...

  10. 056.Python前端Django模型ORM多表基本操作

    一 准备工作 1.1 新建一个项目 root@darren-virtual-machine:~# cd /root/PycharmProjects/ root@darren-virtual-machi ...