1

The Compose V2 project started in June 2021 until April 26, 2022, when the GA version was released. After the release of the GA version, the community also announced that there will be no further functional updates for Compose V1 , and will end of life cycle (EOL) after 6 months, during which key security and bug fixes will be made.

Compatibility comparison between V1 and V2

Ensuring compatibility between V1 and V2 is essential for daily workflow, here are two key changes in V2

Change Potential impact migrate
V2 natively supports BuildKit and is enabled by default Developers will use BuildKit for image building by default in V2 Can not be used by setting the environment variable DOCKER_BUILDKIT=0
The container name uses - instead of _ as the delimiter This may cause errors if the container name is used in the script This change can be turned off with the "--compatibility" flag

For more compatibility changes, see the compatibility documentation

How to install Compose V2

Using Docker Desktop on Windows, MacOS and Linux comes with Compose V2, which can be executed with the command docker compose . It can also be set by configuring "Use Docker Compose V2" to alias ---2bb60992bbd171ef1aea4b2976c99e91 docker-compose to docker compose .

desktop-compose-v2

If you do not use Docker Desktop for Linux , but use Docker Engine directly, you need to install additionally docker-compose-plugin or a separate binary package.

For example, for Ubuntu, it can be installed directly from Docker's official APT source

 ❯ sudo apt update
❯ sudo apt install docker-compose-plugin

Other Linux, for example on my openSUSE, install by manually downloading the binaries from Github (note the choice of version and platform architecture)

 ❯ DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
❯ mkdir -p $DOCKER_CONFIG/cli-plugins
❯ wget https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64
❯ mv docker-compose-linux-x86_64 $DOCKER_CONFIG/cli-plugins/docker-compose
❯ chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

Execute the command test

 ❯ docker compose version
Docker Compose version 2.6.0

For more installation methods, please check the official documentation .

Advantages of Compose V2

Quickly deliver new features in the Docker CLI

  • Support for GPU hosts - If the Docker host has a GPU device (graphics card) and the Docker engine is configured to do so, the Compose service can define a reservation for the GPU device.
  • Support configuration service enablement (Profiles) - Enables Compose application model for multiple purposes and environments through selective server enablement, as follows Compose file
 version: "3.9"
services:
  frontend:
    image: frontend
    profiles: ["frontend"]

  phpmyadmin:
    image: phpmyadmin
    depends_on:
      - db
    profiles:
      - debug

  backend:
    image: backend

  db:
    image: mysql

The default execution docker compose up will only start the backend and db services. To start the corresponding configured services, you need to use the --profile mark or set the environment variable COMPOSE_PROFILES , for example

 ❯ docker compose --profile debug --profile frontend up
❯ COMPOSE_PROFILES=frontend,debug docker compose up
  • Added cp command - to copy files and directories directly in service container and local file system
  • Added ls command - List Compose projects (application stack) in the current environment

Seamless development to production transition

Through cloud integration projects, you can easily deploy multi-container applications to AWS ECS or Azure ACI environments using Compose V2.

For specific examples, please refer to Deploying WordPress to the Cloud .

Create a homogeneous Docker ecosystem in Golang

Before Compose V2, V1 was written in the Python language, which was not part of Docker's language ecosystem. V2 is written in Golang language and can provide code from Moby, CLI or any Golang-based project, reducing a lot of development of rewriting new functions or defects through Python, and easily adding new functions from other Docker tools (such as BuildKit) to Compose .

With Golang, it is now possible to publish a static binary executable, which greatly simplifies update and dependency management compared to Python.

Execute command without Compose file

Compose V2 can manage running Compose project container services via the --project-name|-p option in the following cases

  • The current directory does not contain the project Compose file (not in the Compose project file directory)
  • Do not specify Compose file by --file tag
  • Do not specify the Compose project directory through the --project-directory tag

可执行的命令: psexec , startstoprestartdown

You can first list the Compose projects in the current environment through docker compose ls

 ❯ docker compose ls
NAME                STATUS              CONFIG FILES
dbweb               running(1)          docker-compose.yml
monitor             running(1)          /home/mengz/dockerapp/monitor/docker-compose.yml
traefik             running(1)          /home/mengz/dockerapp/traefik/docker-compose.yml
truenas             running(1)          /home/mengz/dockerapp/truenas/docker-compose.yml

Then manage the project service through -p <项目名> 命令

 ❯ docker compose -p dbweb ps
NAME                COMMAND             SERVICE             STATUS              PORTS
dbweb_pgadmin       "/entrypoint.sh"    pgadmin             running             443/tcp

❯ docker compose -p dbweb exec pgadmin sh
/pgadmin4 $ exit

Summarize

Here is a brief introduction to some of the features and functions of Docker Compose V2. As V1 is gradually phased out, we want to embrace V2 and try out the new features it provides.
For detailed Docker Docker, please refer to the official documentation .

refer to:

Also published on 【 Mengz's Blog

梦哲
74 声望58 粉丝

寻找人生的意义!