directory search
Compose About versions and upgrading (Compose) ASP.NET Core + SQL Server on Linux (Compose) CLI environment variables (Compose) Command-line completion (Compose) Compose(组成) Compose command-line reference(组合命令行参考) Control startup order (Compose) Django and PostgreSQL (Compose) Docker stacks and distributed application bundles (Compose) docker-compose build(docker-compose构建) docker-compose bundle docker-compose config docker-compose create docker-compose down docker-compose events docker-compose exec docker-compose help docker-compose images docker-compose kill docker-compose logs docker-compose pause docker-compose port docker-compose ps docker-compose pull docker-compose push docker-compose restart docker-compose rm docker-compose run docker-compose scale docker-compose start docker-compose stop docker-compose top docker-compose unpause docker-compose up Environment file (Compose) Environment variables in Compose Extend services in Compose Frequently asked questions (Compose) Getting started (Compose) Install Compose Link environment variables (deprecated) (Compose) Networking in Compose Overview of Docker Compose Overview of docker-compose CLI Quickstart: Compose and WordPress Rails and PostgreSQL (Compose) Sample apps with Compose Using Compose in production Using Compose with Swarm Engine .NET Core application (Engine) About images, containers, and storage drivers (Engine) Add nodes to the swarm (Engine) Apply custom metadata (Engine) Apply rolling updates (Engine) apt-cacher-ng Best practices for writing Dockerfiles (Engine) Binaries (Engine) Bind container ports to the host (Engine) Breaking changes (Engine) Build your own bridge (Engine) Configure container DNS (Engine) Configure container DNS in user-defined networks (Engine) CouchDB (Engine) Create a base image (Engine) Create a swarm (Engine) Customize the docker0 bridge (Engine) Debian (Engine) Default bridge network Delete the service (Engine) Deploy a service (Engine) Deploy services to a swarm (Engine) Deprecated Engine features Docker container networking (Engine) Docker overview (Engine) Docker run reference (Engine) Dockerfile reference (Engine) Dockerize an application Drain a node (Engine) Engine FAQ (Engine) Fedora (Engine) Get started (Engine) Get started with macvlan network driver (Engine) Get started with multi-host networking (Engine) How nodes work (Engine) How services work (Engine) Image management (Engine) Inspect the service (Engine) Install Docker (Engine) IPv6 with Docker (Engine) Join nodes to a swarm (Engine) Legacy container links (Engine) Lock your swarm (Engine) Manage nodes in a swarm (Engine) Manage sensitive data with Docker secrets (Engine) Manage swarm security with PKI (Engine) Manage swarm service networks (Engine) Migrate to Engine 1.10 Optional Linux post-installation steps (Engine) Overview (Engine) PostgreSQL (Engine) Raft consensus in swarm mode (Engine) Riak (Engine) Run Docker Engine in swarm mode Scale the service (Engine) SDKs (Engine) Select a storage driver (Engine) Set up for the tutorial (Engine) SSHd (Engine) Storage driver overview (Engine) Store service configuration data (Engine) Swarm administration guide (Engine) Swarm mode key concepts (Engine) Swarm mode overlay network security model (Engine) Swarm mode overview (Engine) Understand container communication (Engine) Use multi-stage builds (Engine) Use swarm mode routing mesh (Engine) Use the AUFS storage driver (Engine) Use the Btrfs storage driver (Engine) Use the Device mapper storage driver (Engine) Use the OverlayFS storage driver (Engine) Use the VFS storage driver (Engine) Use the ZFS storage driver (Engine) Engine: Admin Guide Amazon CloudWatch logs logging driver (Engine) Bind mounts (Engine) Collect Docker metrics with Prometheus (Engine) Configuring and running Docker (Engine) Configuring logging drivers (Engine) Control and configure Docker with systemd (Engine) ETW logging driver (Engine) Fluentd logging driver (Engine) Format command and log output (Engine) Google Cloud logging driver (Engine) Graylog Extended Format (GELF) logging driver (Engine) Journald logging driver (Engine) JSON File logging driver (Engine) Keep containers alive during daemon downtime (Engine) Limit a container's resources (Engine) Link via an ambassador container (Engine) Log tags for logging driver (Engine) Logentries logging driver (Engine) PowerShell DSC usage (Engine) Prune unused Docker objects (Engine) Run multiple services in a container (Engine) Runtime metrics (Engine) Splunk logging driver (Engine) Start containers automatically (Engine) Storage overview (Engine) Syslog logging driver (Engine) tmpfs mounts Troubleshoot volume problems (Engine) Use a logging driver plugin (Engine) Using Ansible (Engine) Using Chef (Engine) Using Puppet (Engine) View a container's logs (Engine) Volumes (Engine) Engine: CLI Daemon CLI reference (dockerd) (Engine) docker docker attach docker build docker checkpoint docker checkpoint create docker checkpoint ls docker checkpoint rm docker commit docker config docker config create docker config inspect docker config ls docker config rm docker container docker container attach docker container commit docker container cp docker container create docker container diff docker container exec docker container export docker container inspect docker container kill docker container logs docker container ls docker container pause docker container port docker container prune docker container rename docker container restart docker container rm docker container run docker container start docker container stats docker container stop docker container top docker container unpause docker container update docker container wait docker cp docker create docker deploy docker diff docker events docker exec docker export docker history docker image docker image build docker image history docker image import docker image inspect docker image load docker image ls docker image prune docker image pull docker image push docker image rm docker image save docker image tag docker images docker import docker info docker inspect docker kill docker load docker login docker logout docker logs docker network docker network connect docker network create docker network disconnect docker network inspect docker network ls docker network prune docker network rm docker node docker node demote docker node inspect docker node ls docker node promote docker node ps docker node rm docker node update docker pause docker plugin docker plugin create docker plugin disable docker plugin enable docker plugin inspect docker plugin install docker plugin ls docker plugin push docker plugin rm docker plugin set docker plugin upgrade docker port docker ps docker pull docker push docker rename docker restart docker rm docker rmi docker run docker save docker search docker secret docker secret create docker secret inspect docker secret ls docker secret rm docker service docker service create docker service inspect docker service logs docker service ls docker service ps docker service rm docker service scale docker service update docker stack docker stack deploy docker stack ls docker stack ps docker stack rm docker stack services docker start docker stats docker stop docker swarm docker swarm ca docker swarm init docker swarm join docker swarm join-token docker swarm leave docker swarm unlock docker swarm unlock-key docker swarm update docker system docker system df docker system events docker system info docker system prune docker tag docker top docker unpause docker update docker version docker volume docker volume create docker volume inspect docker volume ls docker volume prune docker volume rm docker wait Use the Docker command line (Engine) Engine: Extend Access authorization plugin (Engine) Docker log driver plugins Docker network driver plugins (Engine) Extending Engine with plugins Managed plugin system (Engine) Plugin configuration (Engine) Plugins API (Engine) Volume plugins (Engine) Engine: Security AppArmor security profiles for Docker (Engine) Automation with content trust (Engine) Content trust in Docker (Engine) Delegations for content trust (Engine) Deploying Notary (Engine) Docker security (Engine) Docker security non-events (Engine) Isolate containers with a user namespace (Engine) Manage keys for content trust (Engine) Play in a content trust sandbox (Engine) Protect the Docker daemon socket (Engine) Seccomp security profiles for Docker (Engine) Secure Engine Use trusted images Using certificates for repository client verification (Engine) Engine: Tutorials Engine tutorials Network containers (Engine) Get Started Part 1: Orientation Part 2: Containers Part 3: Services Part 4: Swarms Part 5: Stacks Part 6: Deploy your app Machine Amazon Web Services (Machine) Digital Ocean (Machine) docker-machine active docker-machine config docker-machine create docker-machine env docker-machine help docker-machine inspect docker-machine ip docker-machine kill docker-machine ls docker-machine provision docker-machine regenerate-certs docker-machine restart docker-machine rm docker-machine scp docker-machine ssh docker-machine start docker-machine status docker-machine stop docker-machine upgrade docker-machine url Driver options and operating system defaults (Machine) Drivers overview (Machine) Exoscale (Machine) Generic (Machine) Get started with a local VM (Machine) Google Compute Engine (Machine) IBM Softlayer (Machine) Install Machine Machine Machine CLI overview Machine command-line completion Machine concepts and help Machine overview Microsoft Azure (Machine) Microsoft Hyper-V (Machine) Migrate from Boot2Docker to Machine OpenStack (Machine) Oracle VirtualBox (Machine) Provision AWS EC2 instances (Machine) Provision Digital Ocean Droplets (Machine) Provision hosts in the cloud (Machine) Rackspace (Machine) VMware Fusion (Machine) VMware vCloud Air (Machine) VMware vSphere (Machine) Notary Client configuration (Notary) Common Server and signer configurations (Notary) Getting started with Notary Notary changelog Notary configuration files Running a Notary service Server configuration (Notary) Signer configuration (Notary) Understand the service architecture (Notary) Use the Notary client
characters

Compose文件是一个定义Docker应用程序的服务,网络和卷的YAML文件。

现在在这些参考文献中描述了撰写文件格式,这些文件格式针对每个版本。

参考文件

在这个版本中改变了什么

版本3(最新,推荐)

版本3更新

版本2

版本2更新

版本1

版本1更新

下面的主题解释了版本之间的差异,Docker Engine兼容性以及如何升级。

兼容性矩阵

有几种版本的Compose文件格式 -  1,2,2.x和3.x

此表显示哪些Compose文件版本支持特定的Docker版本。

纠错

Compose file format

Docker Engine release

3.3

17.06.0+

3.2

17.04.0+

3.1

1.13.1+

3.0

1.13.0+

2.3

17.06.0+

2.2

1.13.0+

2.1

1.12.0+

2.0

1.10.0+

1.0

1.9.1.+

除了表格中显示的Compose文件格式版本外,Compose本身的发布时间表也如发布版本中所示,但文件格式版本并不一定随每个发行版而增加。例如,Compose文件格式3.0首先在Compose版本1.10.0中引入,并在随后的版本中逐步版本化。

在Docker和Compose兼容性上寻找更多细节?我们建议尽可能保持最新的版本。但是,如果您使用的是较旧版本的Docker,并且想要确定哪个Compose版本兼容,请参阅撰写发行说明。每套发行说明都详细介绍了哪些版本的Docker Engine受支持,以及兼容的Compose文件格式版本。(另见#3404号讨论。)

有关版本和如何升级的详细信息,请参阅版本控制和升级。

版本

目前有三种版本的Compose文件格式:

  • 版本1,传统格式。这是通过省略versionYAML根部的一个键来指定的。

  • 版本2.x. 这是通过YAML根部的一个version: '2'或者version: '2.1'等等条目指定的。

  • 版本3.x是最新和推荐的版本,旨在在Compose和Docker Engine的swarm模式之间交叉兼容。这是通过YAML根部的一个version: '3'或者version: '3.1'等等条目指定的。

兼容性矩阵显示映射到Docker Engine发行版的Compose文件版本。

要将项目移到更高版本,请参阅升级部分。

注意:如果您使用多个Compose文件或扩展服务,则每个文件的版本必须相同 - 例如,不能在单个项目中混合使用版本1和版本2。

根据您使用的版本,有几件事情会有所不同:

  • 结构和允许的配置键

  • 您必须运行的最低Docker引擎版本

  • 撰写关于网络的行为

下面将解释这些差异。

版本1

撰写不声明版本的文件被认为是“版本1”。在这些文件中,所有服务都是在文档的根部声明的。

版本1是支持撰写高达1.6.x版。它将在未来的Compose版本中被弃用。

版本1文件不能声明命名卷,网络或构建参数。

使用版本1时,Compose不利用网络:每个容器都放置在默认bridge网络上,并且可以通过其IP地址上的每个其他容器进行访问。您将需要使用链接来启用容器之间的发现。

例:

web:
  build: .
  ports:   - "5000:5000"
  volumes:   - .:/code
  links:   - redis
redis:
  image: redis

版本2

使用版本2语法撰写文件必须在文档的根目录中指明版本号。所有服务必须在services密钥下声明。

版本2文件由Compose 1.6.0+支持,并且需要版本为1.10.0+的Docker引擎。

命名卷可以在volumes密钥下声明,网络可以在networks密钥下声明。

默认情况下,每个容器都加入一个应用程序范围的默认网络,并且可以在与服务名称相同的主机名上发现。这意味着链接很不必要。有关更多详细信息,请参阅撰写中的联网。

简单的例子:

version: '2'services:
  web:
    build: .
    ports:     - "5000:5000"
    volumes:     - .:/code
  redis:
    image: redis

一个更广泛的例子,定义卷和网络:

version: '2'services:
  web:
    build: .
    ports:     - "5000:5000"
    volumes:     - .:/code
    networks:      - front-tier      - back-tier
  redis:
    image: redis
    volumes:      - redis-data:/var/lib/redis
    networks:      - back-tier
volumes:
  redis-data:
    driver: local
networks:
  front-tier:
    driver: bridge
  back-tier:
    driver: bridge

添加了其他几个选项来支持联网,例如:

  • aliases

  • depends_on选项可用于代替链接以指示服务和启动顺序之间的依赖关系。版本:'2'服务:web:build:。depends_on: -  db  -  redis redis:image:redis db:image:postgres

  • ipv4_address, ipv6_address

版本2中也添加了变量替换。

版本2.1

引入仅在Docker引擎版本1.12.0以上提供的新参数的版本2的升级。Compose 1.9.0+支持版本2.1文件。

引入以下附加参数:

  • link_local_ips

  • isolation

  • labels 对于卷和网络

  • name 为卷

  • userns_mode

  • healthcheck

  • sysctls

  • pids_limit

版本2.2

2.1版本的升级引入了仅在Docker Engine版本1.13.0+中可用的新参数。Compose 1.13.0+支持版本2.2文件。该版本还允许您指定服务配置中的默认缩放编号。

引入以下附加参数:

  • init

  • scale

版本2.3

2.2版的升级引入了仅在Docker Engine版本17.06.0以上版本中提供的新参数。Compose 1.16.0+支持版本2.3文件。

引入以下附加参数:

  • target 用于构建配置

  • start_period 对于 healthchecks

版本3

为了在Compose和Docker Engine的swarm模式之间交叉兼容,版本3删除了几个选项并添加了更多选项。

  • 删除:volume_drivervolumes_fromcpu_sharescpu_quotacpusetmem_limitmemswap_limitextendsgroup_add。请参阅升级指南以了解如何迁移这些指南。(有关更多信息extends,请参阅扩展服务。)

  • 补充:部署

版本3.3

引入仅适用于Docker Engine 17.06.0及更高版本的新参数的版本3的升级。

引入以下附加参数:

  • 建立 labels

  • credential_spec

  • configs

  • 部署 endpoint_mode

升级

版本2.x到3.x

在版本2.x和3.x之间,撰写文件的结构是相同的,但是几个选项已被删除:

  • volume_driver:不是在服务上设置卷驱动,而是使用顶层volumes选项定义卷并在那里指定驱动。版本:“3”服务:db:image:postgres卷: -  data:/ var / lib / postgresql / data卷:data:驱动程序:mydriver

  • volumes_from:要在服务之间共享卷,请使用顶级volumes选项对其进行定义,并从使用服务级别volumes选项共享服务的每个服务中引用它。

  • cpu_sharescpu_quotacpusetmem_limitmemswap_limit:这些已被替换下的资源键deploy。请注意,deploy配置仅在使用时生效docker stack deploy,并被忽略docker-compose

  • extends:对于version: "3.x"撰写文件,此选项已被删除。(有关更多信息,请参阅扩展服务。)

  • group_add:对于version: "3.x"撰写文件,此选项已被删除。

  • pids_limit:此选项尚未在version: "3.x"Compose文件中引入。

  • link_local_ipsin networks:该选项尚未在version: "3.x"Compose文件中引入。

版本1到2.x

在大多数情况下,从版本1移动到2是一个非常简单的过程:

  1. 将整个文件缩进一层,并services:在顶部放置一个键。

  2. version: '2'在文件顶部添加一行。

如果您使用特定的配置功能,则会更加复杂:

  • dockerfile:现在生活在build关键之下:build:context:。dockerfile:Dockerfile-alternate

  • log_driverlog_opt:这些现在生活在logging密钥下:logging:driver:syslog options:syslog-address:“tcp://192.168.0.42:123”

  • links使用环境变量:如环境变量引用中所述,由链接创建的环境变量已被弃用一段时间。在新的Docker网络系统中,它们已被删除。您应该直接连接到适当的主机名或使用链接主机名设置相关的环境变量:web:links: -  db environment: -  DB_PORT = tcp:// db:5432

  • external_links:Compose在运行版本2项目时使用Docker网络,因此链接的行为稍有不同。特别是,两个容器必须连接到至少一个共同的网络才能通信,即使明确地链接在一起。将外部容器连接到应用程序的默认网络,或者将外部容器和服务的容器连接到外部网络。

  • net:这现在被network_mode:net:host  - > network_mode:host net:bridge  - > network_mode:bridge net:none  - > network_mode:none 替代如果您正在使用net: "container:[service name]",您现在必须network_mode: "service:[service name]"改用它。net:“container:web” - > network_mode:“service:web”如果你正在使用net: "container:[container name/id]",这个值不需要改变。net:“container:cont-name” - > network_mode:“container:cont-name”net:“container:abc12345” - > network_mode:“container:abc12345”

  • volumes使用命名卷:现在必须在volumesCompose文件的顶级部分显式声明这些命令。如果某个服务装载了一个已命名的卷data,则必须data在顶级volumes部分中声明一个卷。整个文件可能如下所示:版本:'2'服务:db:image:postgres卷: -  data:/ var / lib / postgresql / data卷:data:默认情况下,Compose创建一个卷,其名称带有前缀与您的项目名称。如果您希望仅调用data它,请将其声明为external:volumes:data:external:true

Previous article: Next article: