云原生核心技术之:容器 | Docker|k8s 容器平台架构
958
2022-10-30
使您的Docker Compose应用程序可重用,并在Docker Hub上共享它们
Docker Application Packages
An experimental utility to help make Compose files more reusable and sharable.
The problem application packages solve
Compose files do a great job of describing a set of related services. Not only are Compose files easy to write, they are generally easy to read as well. However, a couple of problems often emerge:
You have several environments where you want to deploy the application, with small configuration differencesYou have lots of similar applications
Fundamentally, Compose files are not easy to share between concerns. Docker Application Packages aim to solve these problems and make Compose more useful for development and production.
Looking at an example
Let's take the following Compose file. It launches an HTTP server which prints the specified text when hit on the configured port.
version: '3.2'services: hello: image: hashicorp/http-echo command: ["-text", "hello world"] ports: - 5678:5678
With docker-app installed let's create an Application Package based on this Compose file:
$ docker-app init --single-file hello$ lsdocker-compose.ymlhello.dockerapp
We created a new file hello.dockerapp that contains three YAML documents:
metadatasthe Compose filesettings for your application
It should look like this:
version: 0.1.0name: hellodescription: ""namespace: ""maintainers:- name: yourusername email: ""targets: swarm: true kubernetes: true--version: '3.2'services: hello: image: hashicorp/http-echo command: ["-text", "hello world"] ports: - 5678:5678--{}
Let's edit the settings section and add the following default values for our application:
port: 5678text: hello developmentversion: latest
Then modify the Compose file section in hello.dockerapp, adding in the variables.
version: '3.2'services: hello: image: hashicorp/http-echo:${version} command: ["-text", "${text}"] ports: - ${port}:5678
Finally you can test everything is working, by rendering the Compose file with the provided default values.
$ docker-app renderversion: "3.2"services: hello: command: - -text - hello development image: hashicorp/http-echo:latest ports: - mode: ingress target: 5678 published: 5678 protocol: tcp
You can then use that Compose file like any other. You could save it to disk or pipe it straight to docker stack or docker-compose to launch the application.
$ docker-app render | docker-compose -f - up
This is where it gets interesting. We can override those settings at runtime, using the --set option. Let's specify different option and run render again:
$ docker-app render --set version=0.2.3 --set port=4567 --set text="hello production"version: "3.2"services: hello: command: - -text - hello production image: hashicorp/http-echo:0.2.3 ports: - mode: ingress target: 5678 published: 4567 protocol: tcp
If you prefer you can create a standalone configuration file to store those settings. Let's create prod.yml with the following contents:
version: 0.2.3text: hello productionport: 4567
You can then run using that configuration file like so:
$ docker-app render -f prod.yml
More examples are available in the examples directory.
Installation
Pre-built binaries are available on GitHub releases for Windows, Linux and macOS.
wget https://github.com/docker/app/releases/download/v0.2.0/docker-app-linux.tar.gztar xf docker-app-linux.tar.gzcp docker-app-linux /usr/local/bin/docker-app
Integrating with Helm
docker-app comes with a few other helpful commands as well, in particular the ability to create Helm Charts from your Docker Applications. This can be useful if you're adopting Kubernetes, and standardising on Helm to manage the lifecycle of your application components, but want to maintain the simplicity of Compose when writing you applications. This also makes it easy to run the same applications locally just using Docker, if you don't want to be running a full Kubernetes cluster.
$ docker-app helm
This will create a folder,
Note that this requires the Compose Kubernetes controller available in Docker for Windows and Docker for Mac, and in Docker Enterprise Edition.
Single file or directory representation
If you prefer having the three documents in separate YAML files, omit the -s option to the docker-app init command. This will create a directory instead of a singe file, containing metadata.yml, docker-compose.yml and settings.yml.
Sharing your application on the Hub
You can push any application to the Hub using docker-app push:
$ docker-app push --namespace myHubUser --tag latest
This command will create an image named myHubUser/hello.dockerapp:latest on your local Docker daemon, and push it to the Hub.
By default, this command uses the application version defined in metadata.yml as the tag, and the value of the metadata field namespace as the image namespace.
All docker-app commands accept a local image name as input, which means you can run on a different host:
$ docker pull myHubUser/hello.dockerapp:latest$ docker-app inspect myHubUser/hello
Next steps
We have lots of ideas for making Compose-based applications easier to share and reuse, and making applications a first-class part of the Docker toolchain. Please let us know what you think about this initial release and about any of the ideas below:
Introducing environments to the settings fileDocker images which launch the application when runBuilt-in commands for running applicationsSaving required images into the application artifact to support offline installationAutomatically validating Compose files against the schema for the specified versionSigning applications with notary
Usage
$ docker-appDocker App PackagesUsage: docker-app [command]Available Commands: helm Render the Compose file for this app as an Helm package help Help about any command init Initialize an app package in the current working directory inspect Retrieve metadata for a given app package render Render the Compose file for this app version Print version informationFlags: -h, --help help for docker-appUse "docker-app [command] --help" for more information about a command.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。