|
| 1 | +There are three legacy versions of the Compose file format: |
| 2 | + |
| 3 | +- Version 1. This is specified by omitting a `version` key at the root of the YAML. |
| 4 | + |
| 5 | +- Version 2.x. This is specified with a `version: '2'` or `version: '2.1'`, etc., entry at the root of the YAML. |
| 6 | + |
| 7 | +- Version 3.x, designed to be cross-compatible between Compose and the Docker Engine's |
| 8 | +[swarm mode](https://docs.docker.com/engine/swarm/). This is specified with a `version: '3'` or `version: '3.1'`, etc., entry at the root of the YAML. |
| 9 | + |
| 10 | +The latest and recommended version of the Compose file format is defined by the [Compose Specification](https://docs.docker.com/compose/compose-file/). This format merges the 2.x and 3.x versions and is implemented by **Compose 1.27.0+**. |
| 11 | + |
| 12 | + |
| 13 | +> **Note** |
| 14 | +> |
| 15 | +> If you're using [multiple Compose files](https://docs.docker.com/compose/multiple-compose-files/) or |
| 16 | +> [extending services](https://docs.docker.com/compose/multiple-compose-files/extends/), |
| 17 | +> each file must be of the same version - you cannot, for example, |
| 18 | +> mix version 1 and 2 in a single project. |
| 19 | +
|
| 20 | +Several things differ depending on which version you use: |
| 21 | + |
| 22 | +- The structure and permitted configuration keys |
| 23 | +- The minimum Docker Engine version you must be running |
| 24 | +- Compose's behaviour with regards to networking |
| 25 | + |
| 26 | +These differences are explained below. |
| 27 | + |
| 28 | +### Version 2 |
| 29 | + |
| 30 | +Compose files using the version 2 syntax must indicate the version number at |
| 31 | +the root of the document. All [services](compose-file-v2.md#service-configuration-reference) |
| 32 | +must be declared under the `services` key. |
| 33 | + |
| 34 | +Version 2 files are supported by **Compose 1.6.0+** and require a Docker Engine |
| 35 | +of version **1.10.0+**. |
| 36 | + |
| 37 | +Named [volumes](compose-file-v2.md#volume-configuration-reference) can be declared under the |
| 38 | +`volumes` key, and [networks](compose-file-v2.md#network-configuration-reference) can be declared |
| 39 | +under the `networks` key. |
| 40 | + |
| 41 | +By default, every container joins an application-wide default network, and is |
| 42 | +discoverable at a hostname that's the same as the service name. This means |
| 43 | +[links](compose-file-v2.md#links) are largely unnecessary. For more details, see |
| 44 | +[Networking in Compose](https://docs.docker.com/compose/networking/). |
| 45 | + |
| 46 | +> **Note** |
| 47 | +> |
| 48 | +> With Compose version 2, when specifying the Compose file version to use, make sure to |
| 49 | +> specify both the _major_ and _minor_ numbers. If no minor version is given, |
| 50 | +> `0` is used by default and not the latest minor version. As a result, features added in later versions will not be supported. For example: |
| 51 | +> |
| 52 | +> ```yaml |
| 53 | +> version: "2" |
| 54 | +> ``` |
| 55 | +> |
| 56 | +> is equivalent to: |
| 57 | +> |
| 58 | +> ```yaml |
| 59 | +> version: "2.0" |
| 60 | +> ``` |
| 61 | +
|
| 62 | +Simple example: |
| 63 | +
|
| 64 | + version: "{{% param "compose_file_v2" %}}" |
| 65 | + services: |
| 66 | + web: |
| 67 | + build: . |
| 68 | + ports: |
| 69 | + - "8000:5000" |
| 70 | + volumes: |
| 71 | + - .:/code |
| 72 | + redis: |
| 73 | + image: redis |
| 74 | +
|
| 75 | +A more extended example, defining volumes and networks: |
| 76 | +
|
| 77 | + version: "{{% param "compose_file_v2" %}}" |
| 78 | + services: |
| 79 | + web: |
| 80 | + build: . |
| 81 | + ports: |
| 82 | + - "8000:5000" |
| 83 | + volumes: |
| 84 | + - .:/code |
| 85 | + networks: |
| 86 | + - front-tier |
| 87 | + - back-tier |
| 88 | + redis: |
| 89 | + image: redis |
| 90 | + volumes: |
| 91 | + - redis-data:/var/lib/redis |
| 92 | + networks: |
| 93 | + - back-tier |
| 94 | + volumes: |
| 95 | + redis-data: |
| 96 | + driver: local |
| 97 | + networks: |
| 98 | + front-tier: |
| 99 | + driver: bridge |
| 100 | + back-tier: |
| 101 | + driver: bridge |
| 102 | +
|
| 103 | +Several other options were added to support networking, such as: |
| 104 | +
|
| 105 | +* [`aliases`](compose-file-v2.md#aliases) |
| 106 | +
|
| 107 | +* The [`depends_on`](compose-file-v2.md#depends_on) option can be used in place of links to indicate dependencies |
| 108 | +between services and startup order. |
| 109 | +
|
| 110 | + version: "{{% param "compose_file_v2" %}}" |
| 111 | + services: |
| 112 | + web: |
| 113 | + build: . |
| 114 | + depends_on: |
| 115 | + - db |
| 116 | + - redis |
| 117 | + redis: |
| 118 | + image: redis |
| 119 | + db: |
| 120 | + image: postgres |
| 121 | +
|
| 122 | +* [`ipv4_address`, `ipv6_address`](compose-file-v2.md#ipv4_address-ipv6_address) |
| 123 | +
|
| 124 | +[Variable substitution](compose-file-v2.md#variable-substitution) also was added in Version 2. |
| 125 | +
|
| 126 | +### Version 2.1 |
| 127 | +
|
| 128 | +An upgrade of [version 2](#version-2) that introduces new parameters only |
| 129 | +available with Docker Engine version **1.12.0+**. Version 2.1 files are |
| 130 | +supported by **Compose 1.9.0+**. |
| 131 | +
|
| 132 | +Introduces the following additional parameters: |
| 133 | +
|
| 134 | +- [`link_local_ips`](compose-file-v2.md#link_local_ips) |
| 135 | +- [`isolation`](compose-file-v2.md#isolation-1) in build configurations and |
| 136 | + service definitions |
| 137 | +- `labels` for [volumes](compose-file-v2.md#volume-configuration-reference), |
| 138 | + [networks](compose-file-v2.md#network-configuration-reference), and |
| 139 | + [build](compose-file-v3.md#build) |
| 140 | +- `name` for [volumes](compose-file-v2.md#volume-configuration-reference) |
| 141 | +- [`userns_mode`](compose-file-v2.md#userns_mode) |
| 142 | +- [`healthcheck`](compose-file-v2.md#healthcheck) |
| 143 | +- [`sysctls`](compose-file-v2.md#sysctls) |
| 144 | +- [`pids_limit`](compose-file-v2.md#pids_limit) |
| 145 | +- [`oom_kill_disable`](compose-file-v2.md#cpu-and-other-resources) |
| 146 | +- [`cpu_period`](compose-file-v2.md#cpu-and-other-resources) |
| 147 | +
|
| 148 | +### Version 2.2 |
| 149 | +
|
| 150 | +An upgrade of [version 2.1](#version-21) that introduces new parameters only |
| 151 | +available with Docker Engine version **1.13.0+**. Version 2.2 files are |
| 152 | +supported by **Compose 1.13.0+**. This version also allows you to specify |
| 153 | +default scale numbers inside the service's configuration. |
| 154 | +
|
| 155 | +Introduces the following additional parameters: |
| 156 | +
|
| 157 | +- [`init`](compose-file-v2.md#init) |
| 158 | +- [`scale`](compose-file-v2.md#scale) |
| 159 | +- [`cpu_rt_runtime` and `cpu_rt_period`](compose-file-v2.md#cpu_rt_runtime-cpu_rt_period) |
| 160 | +- [`network`](compose-file-v2.md#network) for [build configurations](compose-file-v2.md#build) |
| 161 | +
|
| 162 | +### Version 2.3 |
| 163 | +
|
| 164 | +An upgrade of [version 2.2](#version-22) that introduces new parameters only |
| 165 | +available with Docker Engine version **17.06.0+**. Version 2.3 files are |
| 166 | +supported by **Compose 1.16.0+**. |
| 167 | +
|
| 168 | +Introduces the following additional parameters: |
| 169 | +
|
| 170 | +- [`target`](compose-file-v2.md#target), [`extra_hosts`](compose-file-v2.md#extra_hosts-1) and |
| 171 | + [`shm_size`](compose-file-v2.md#shm_size) for [build configurations](compose-file-v2.md#build) |
| 172 | +- `start_period` for [`healthchecks`](compose-file-v2.md#healthcheck) |
| 173 | +- ["Long syntax" for volumes](compose-file-v2.md#long-syntax) |
| 174 | +- [`runtime`](compose-file-v2.md#runtime) for service definitions |
| 175 | +- [`device_cgroup_rules`](compose-file-v2.md#device_cgroup_rules) |
| 176 | +
|
| 177 | +### Version 2.4 |
| 178 | +
|
| 179 | +An upgrade of [version 2.3](#version-23) that introduces new parameters only |
| 180 | +available with Docker Engine version **17.12.0+**. Version 2.4 files are |
| 181 | +supported by **Compose 1.21.0+**. |
| 182 | +
|
| 183 | +Introduces the following additional parameters: |
| 184 | +
|
| 185 | +- [`platform`](compose-file-v2.md#platform) for service definitions |
| 186 | +- Support for extension fields at the root of service, network, and volume |
| 187 | + definitions |
| 188 | +
|
| 189 | +### Version 3 |
| 190 | +
|
| 191 | +Designed to be cross-compatible between Compose and the Docker Engine's |
| 192 | +[swarm mode](/engine/swarm/), version 3 removes several options and adds |
| 193 | +several more. |
| 194 | +
|
| 195 | +- Removed: `volume_driver`, `volumes_from`, `cpu_shares`, `cpu_quota`, |
| 196 | + `cpuset`, `mem_limit`, `memswap_limit`, `extends`, `group_add`. See |
| 197 | + the [upgrading](#upgrading) guide for how to migrate away from these. |
| 198 | +
|
| 199 | +- Added: [deploy](compose-file-v3.md#deploy) |
| 200 | +
|
| 201 | +If only the major version is given (`version: '3'`), |
| 202 | +the latest minor version is used by default. |
| 203 | +
|
| 204 | +### Version 3.1 |
| 205 | +
|
| 206 | +An upgrade of [version 3](#version-3) that introduces new parameters only |
| 207 | +available with Docker Engine version **1.13.1+**, and higher. |
| 208 | +
|
| 209 | +Introduces the following additional parameters: |
| 210 | +
|
| 211 | +- [`secrets`](compose-file-v3.md#secrets) |
| 212 | +
|
| 213 | +### Version 3.2 |
| 214 | +
|
| 215 | +An upgrade of [version 3](#version-3) that introduces new parameters only |
| 216 | +available with Docker Engine version **17.04.0+**, and higher. |
| 217 | +
|
| 218 | +Introduces the following additional parameters: |
| 219 | +
|
| 220 | +- [`cache_from`](compose-file-v3.md#cache_from) in [build configurations](compose-file-v3.md#build) |
| 221 | +- Long syntax for [ports](compose-file-v3.md#ports) and [volume mounts](compose-file-v3.md#volumes) |
| 222 | +- [`attachable`](compose-file-v3.md#attachable) network driver option |
| 223 | +- [deploy `endpoint_mode`](compose-file-v3.md#endpoint_mode) |
| 224 | +- [deploy placement `preference`](compose-file-v3.md#placement) |
| 225 | +
|
| 226 | +### Version 3.3 |
| 227 | +
|
| 228 | +An upgrade of [version 3](#version-3) that introduces new parameters only |
| 229 | +available with Docker Engine version **17.06.0+**, and higher. |
| 230 | +
|
| 231 | +Introduces the following additional parameters: |
| 232 | +
|
| 233 | +- [build `labels`](compose-file-v3.md#build) |
| 234 | +- [`credential_spec`](compose-file-v3.md#credential_spec) |
| 235 | +- [`configs`](compose-file-v3.md#configs) |
| 236 | +
|
| 237 | +### Version 3.4 |
| 238 | +
|
| 239 | +An upgrade of [version 3](#version-3) that introduces new parameters. It is |
| 240 | +only available with Docker Engine version **17.09.0** and higher. |
| 241 | +
|
| 242 | +Introduces the following additional parameters: |
| 243 | +
|
| 244 | +- [`target`](compose-file-v3.md#target) and [`network`](compose-file-v3.md#network) in |
| 245 | + [build configurations](compose-file-v3.md#build) |
| 246 | +- `start_period` for [`healthchecks`](compose-file-v3.md#healthcheck) |
| 247 | +- `order` for [update configurations](compose-file-v3.md#update_config) |
| 248 | +- `name` for [volumes](compose-file-v3.md#volume-configuration-reference) |
| 249 | +
|
| 250 | +### Version 3.5 |
| 251 | +
|
| 252 | +An upgrade of [version 3](#version-3) that introduces new parameters. It is |
| 253 | +only available with Docker Engine version **17.12.0** and higher. |
| 254 | +
|
| 255 | +Introduces the following additional parameters: |
| 256 | +
|
| 257 | +- [`isolation`](compose-file-v3.md#isolation) in service definitions |
| 258 | +- `name` for networks, secrets and configs |
| 259 | +- `shm_size` in [build configurations](compose-file-v3.md#build) |
| 260 | +
|
| 261 | +### Version 3.6 |
| 262 | +
|
| 263 | +An upgrade of [version 3](#version-3) that introduces new parameters. It is |
| 264 | +only available with Docker Engine version **18.02.0** and higher. |
| 265 | +
|
| 266 | +Introduces the following additional parameters: |
| 267 | +
|
| 268 | +- [`tmpfs` size](compose-file-v3.md#long-syntax-3) for `tmpfs`-type mounts |
| 269 | +
|
| 270 | +### Version 3.7 |
| 271 | +
|
| 272 | +An upgrade of [version 3](#version-3) that introduces new parameters. It is |
| 273 | +only available with Docker Engine version **18.06.0** and higher. |
| 274 | +
|
| 275 | +Introduces the following additional parameters: |
| 276 | +
|
| 277 | +- [`init`](compose-file-v3.md#init) in service definitions |
| 278 | +- [`rollback_config`](compose-file-v3.md#rollback_config) in deploy configurations |
| 279 | +- Support for extension fields at the root of service, network, volume, secret |
| 280 | + and config definitions |
| 281 | +
|
| 282 | +### Version 3.8 |
| 283 | +
|
| 284 | +An upgrade of [version 3](#version-3) that introduces new parameters. It is |
| 285 | +only available with Docker Engine version **19.03.0** and higher. |
| 286 | +
|
| 287 | +Introduces the following additional parameters: |
| 288 | +
|
| 289 | +- [`max_replicas_per_node`](compose-file-v3.md#max_replicas_per_node) in placement |
| 290 | + configurations |
| 291 | +- `template_driver` option for [config](compose-file-v3.md#configs-configuration-reference) |
| 292 | + and [secret](compose-file-v3.md#secrets-configuration-reference) configurations. This |
| 293 | + option is only supported when deploying swarm services using |
| 294 | + `docker stack deploy`. |
| 295 | +- `driver` and `driver_opts` option for [secret](compose-file-v3.md#secrets-configuration-reference) |
| 296 | + configurations. This option is only supported when deploying swarm services |
| 297 | + using `docker stack deploy`. |
| 298 | +
|
| 299 | +### Version 1 (Deprecated) |
| 300 | +
|
| 301 | +Compose versions below 1.6.x are |
| 302 | +
|
| 303 | +Compose files that do not declare a version are considered "version 1". In those |
| 304 | +files, all the [services](compose-file-v3.md#service-configuration-reference) are |
| 305 | +declared at the root of the document. |
| 306 | +
|
| 307 | +Version 1 is supported by Compose up to 1.6.x** and has been deprecated. |
| 308 | +
|
| 309 | +Version 1 files cannot declare named |
| 310 | +[volumes](compose-file-v3.md#volume-configuration-reference), [networks](compose-file-v3.md#network-configuration-reference) or |
| 311 | +[build arguments](compose-file-v3.md#args). |
| 312 | +
|
| 313 | +Compose does not take advantage of [networking](https://docs.docker.com/compose/networking/) when you |
| 314 | +use version 1: every container is placed on the default `bridge` network and is |
| 315 | +reachable from every other container at its IP address. You need to use |
| 316 | +`links` to enable discovery between containers. |
| 317 | +
|
| 318 | +Example: |
| 319 | +
|
| 320 | + web: |
| 321 | + build: . |
| 322 | + ports: |
| 323 | + - "8000:5000" |
| 324 | + volumes: |
| 325 | + - .:/code |
| 326 | + links: |
| 327 | + - redis |
| 328 | + redis: |
| 329 | + image: redis |
| 330 | +
|
| 331 | +## Upgrading |
| 332 | +
|
| 333 | +### Version 2.x to 3.x |
| 334 | +
|
| 335 | +Between versions 2.x and 3.x, the structure of the Compose file is the same, but |
| 336 | +several options have been removed: |
| 337 | +
|
| 338 | +- `volume_driver`: Instead of setting the volume driver on the service, define |
| 339 | + a volume using the |
| 340 | + [top-level `volumes` option](compose-file-v3.md#volume-configuration-reference) |
| 341 | + and specify the driver there. |
| 342 | +
|
| 343 | + version: "3.8" |
| 344 | + services: |
| 345 | + db: |
| 346 | + image: postgres |
| 347 | + volumes: |
| 348 | + - data:/var/lib/postgresql/data |
| 349 | + volumes: |
| 350 | + data: |
| 351 | + driver: mydriver |
| 352 | +
|
| 353 | +- `volumes_from`: To share a volume between services, define it using the |
| 354 | + [top-level `volumes` option](compose-file-v3.md#volume-configuration-reference) |
| 355 | + and reference it from each service that shares it using the |
| 356 | + [service-level `volumes` option](compose-file-v3.md#driver). |
| 357 | +
|
| 358 | +- `cpu_shares`, `cpu_quota`, `cpuset`, `mem_limit`, `memswap_limit`: These |
| 359 | + have been replaced by the [resources](compose-file-v3.md#resources) key under |
| 360 | + `deploy`. `deploy` configuration only takes effect when using |
| 361 | + `docker stack deploy`, and is ignored by `docker-compose`. |
| 362 | +
|
| 363 | +- `extends`: This option has been removed for `version: "3.x"` Compose files. |
| 364 | + For more information on `extends`, see |
| 365 | + [Extending services](https://docs.docker.com/compose/multiple-compose-files/extends/). |
| 366 | +- `group_add`: This option has been removed for `version: "3.x"` Compose files. |
| 367 | +- `pids_limit`: This option has not been introduced in `version: "3.x"` Compose files. |
| 368 | +- `link_local_ips` in `networks`: This option has not been introduced in |
| 369 | + `version: "3.x"` Compose files. |
| 370 | +
|
| 371 | +#### Compatibility mode |
| 372 | +
|
| 373 | +`docker-compose` 1.20.0 introduces a new `--compatibility` flag designed to |
| 374 | +help developers transition to version 3 more easily. When enabled, |
| 375 | +`docker-compose` reads the `deploy` section of each service's definition and |
| 376 | +attempts to translate it into the equivalent version 2 parameter. Currently, |
| 377 | +the following deploy keys are translated: |
| 378 | +
|
| 379 | +- [resources](compose-file-v3.md#resources) limits and memory reservations |
| 380 | +- [replicas](compose-file-v3.md#replicas) |
| 381 | +- [restart_policy](compose-file-v3.md#restart_policy) `condition` and `max_attempts` |
| 382 | +
|
| 383 | +All other keys are ignored and produce a warning if present. You can review |
| 384 | +the configuration that will be used to deploy by using the `--compatibility` |
| 385 | +flag with the `config` command. |
| 386 | +
|
| 387 | +> Do not use this in production |
| 388 | +> |
| 389 | +> We recommend against using `--compatibility` mode in production. The |
| 390 | +> resulting configuration is only an approximate using non-Swarm mode |
| 391 | +> properties, it may produce unexpected results. |
| 392 | +
|
| 393 | +### Version 1 to 2.x |
| 394 | +
|
| 395 | +In the majority of cases, moving from version 1 to 2 is a very simple process: |
| 396 | +
|
| 397 | +1. Indent the whole file by one level and put a `services:` key at the top. |
| 398 | +2. Add a `version: '2'` line at the top of the file. |
| 399 | +
|
| 400 | +It's more complicated if you're using particular configuration features: |
| 401 | +
|
| 402 | +- `dockerfile`: This now lives under the `build` key: |
| 403 | +
|
| 404 | + build: |
| 405 | + context: . |
| 406 | + dockerfile: Dockerfile-alternate |
| 407 | +
|
| 408 | +- `log_driver`, `log_opt`: These now live under the `logging` key: |
| 409 | +
|
| 410 | + logging: |
| 411 | + driver: syslog |
| 412 | + options: |
| 413 | + syslog-address: "tcp://192.168.0.42:123" |
| 414 | +
|
| 415 | +- `links` with environment variables: environment variables created by |
| 416 | + links, such as `CONTAINERNAME_PORT`, ` have been deprecated for some time. In the new Docker network system, |
| 417 | + they have been removed. You should either connect directly to the |
| 418 | + appropriate hostname or set the relevant environment variable yourself, |
| 419 | + using the link hostname: |
| 420 | +
|
| 421 | + web: |
| 422 | + links: |
| 423 | + - db |
| 424 | + environment: |
| 425 | + - DB_PORT=tcp://db:5432 |
| 426 | +
|
| 427 | +- `external_links`: Compose uses Docker networks when running version 2 |
| 428 | + projects, so links behave slightly differently. In particular, two |
| 429 | + containers must be connected to at least one network in common in order to |
| 430 | + communicate, even if explicitly linked together. |
| 431 | +
|
| 432 | + Either connect the external container to your app's |
| 433 | + [default network](https://docs.docker.com/compose/networking/), or connect both the external container and |
| 434 | + your service's containers to an |
| 435 | + [external network](https://docs.docker.com/compose/networking/). |
| 436 | +
|
| 437 | +- `net`: This is now replaced by [network_mode](compose-file-v3.md#network_mode): |
| 438 | +
|
| 439 | + net: host -> network_mode: host |
| 440 | + net: bridge -> network_mode: bridge |
| 441 | + net: none -> network_mode: none |
| 442 | +
|
| 443 | + If you're using `net: "container:[service name]"`, you must now use |
| 444 | + `network_mode: "service:[service name]"` instead. |
| 445 | +
|
| 446 | + net: "container:web" -> network_mode: "service:web" |
| 447 | +
|
| 448 | + If you're using `net: "container:[container name/id]"`, the value does not |
| 449 | + need to change. |
| 450 | +
|
| 451 | + net: "container:cont-name" -> network_mode: "container:cont-name" |
| 452 | + net: "container:abc12345" -> network_mode: "container:abc12345" |
| 453 | +
|
| 454 | +- `volumes` with named volumes: these must now be explicitly declared in a |
| 455 | + top-level `volumes` section of your Compose file. If a service mounts a |
| 456 | + named volume called `data`, you must declare a `data` volume in your |
| 457 | + top-level `volumes` section. The whole file might look like this: |
| 458 | +
|
| 459 | + version: "{{% param "compose_file_v2" %}}" |
| 460 | + services: |
| 461 | + db: |
| 462 | + image: postgres |
| 463 | + volumes: |
| 464 | + - data:/var/lib/postgresql/data |
| 465 | + volumes: |
| 466 | + data: {} |
| 467 | +
|
| 468 | + By default, Compose creates a volume whose name is prefixed with your |
| 469 | + project name. If you want it to just be called `data`, declare it as |
| 470 | + external: |
| 471 | +
|
| 472 | + volumes: |
| 473 | + data: |
| 474 | + external: true |
0 commit comments