Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

Docker Compose now shipped with Docker CLI by default

3 min read

Last year, Dockercon attracted 78,000 registrants, 21,000 conversations across 193 countries. This year, it was an even much bigger event attracting over 90,000+ attendees. With tons of speakers, tracks, and community rooms, it was a 1-day busy event with exciting announcements, talks & community room sessions. This year, Docker Compose 2.0 was announced for the first time as a first-class citizen for Docker CLI. It was a long-awaited feature and finally, the Docker community was super happy to see this being shipped with Docker Desktop for Mac & Windows by default.

In this blog post, I will share the overall experience around the newer docker compose CLI.

#1 You don’t need to install Docker Compose as a separate package

Yes, you heard it right. Docker Desktop for Mac and for Windows version 3.2.1 and above includes the new Compose command along with the Docker CLI. Therefore, Windows and Mac users do not need to install the Compose CLI separately.

docker compose version
Docker Compose version 2.0.0-beta.1

#2 Full Compatibility with older Docker Compose CLI

The Docker CLI now supports the compose command, including most of the docker-compose features and flags, without the need for a separate tool. You can replace the dash (-) with a space when you use docker-compose to switch over to docker compose. You can also use them interchangeably, so that you are not locked-in with the new compose command and, if needed, you can still use docker-compose.

$ docker-compose up -d

#3 You can now specify the name of the project flawlessly

The newer Compose v2 comes with CLI option to specify the project name as shown below:

 % docker compose --project-name --help

Usage:  docker compose [OPTIONS] COMMAND

Docker Compose

Options:
      --ansi string                Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto")
      --env-file string            Specify an alternate environment file.
  -f, --file stringArray           Compose configuration files
      --profile stringArray        Specify a profile to enable
      --project-directory string   Specify an alternate working directory
                                   (default: the path of the Compose file)
  -p, --project-name string        Project name

Let us try out a simple wordpress example. Below is a sample WordPress Docker compose file.

version: "3.6"

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "80:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: wordpress

volumes:
    db_data:

Let us use the newer Docker compose CLI and try to run WordPress app using a new project name.

 % docker compose --project-name demo up -d
[+] Running 2/2
 ⠿ Container demo_db_1         Running                                                                                                  0.0s
 ⠿ Container demo_wordpress_1  Running

Verify the services:

% docker compose ls
NAME                STATUS
demo                running(2)

Please note: In the earlier release of Docker Compose, one has to run the Compose CLI from a specific directory where the compose file used to reside. But with the newer release, you don’t need to worry about it.

#4 The docker compose "ls VS ps"

With the newer Docker Compose CLI, there are two distinct CLIs – the “ls” and “ps”. The “ls” list running compose projects whereas “ps” lists containers.

ajeetraina@Ajeets-MacBook-Pro wordpress % docker compose up -d
[+] Running 3/3
 ⠿ Network wordpress_default        Created                                                                                             3.9s
 ⠿ Container wordpress_db_1         Started                                                                                             2.2s
 ⠿ Container wordpress_wordpress_1  Started                                                                                             5.8s
ajeetraina@Ajeets-MacBook-Pro wordpress % docker compose ps
NAME                    SERVICE             STATUS              PORTS
wordpress_db_1          db                  running             3306/tcp, 33060/tcp
wordpress_wordpress_1   wordpress           running             0.0.0.0:80->80/tcp, :::80->80/tcp
ajeetraina@Ajeets-MacBook-Pro wordpress % docker compose ls
NAME                STATUS
wordpress           running(2)
ajeetraina@Ajeets-MacBook-Pro wordpress % docker compose ps
NAME                    SERVICE             STATUS              PORTS
wordpress_db_1          db                  running             3306/tcp, 33060/tcp
wordpress_wordpress_1   wordpress           running             0.0.0.0:80->80/tcp, :::80->80/tcp

#5 Checking the logs

docker compose logs -f wordpress
wordpress_1  | WordPress not found in /var/www/html - copying now...
wordpress_1  | Complete! WordPress has been successfully copied to /var/www/html
wordpress_1  | No 'wp-config.php' found in /var/www/html, but 'WORDPRESS_...' variables supplied; copying 'wp-config-docker.php' (WORDPRESS_DB_HOST WORDPRESS_DB_PASSWORD)
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.21.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.21.0.3. Set the 'ServerName' directive globally to suppress this message
wordpress_1  | [Fri May 28 23:49:11.020711 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.19 configured -- resuming normal operations
wordpress_1  | [Fri May 28 23:49:11.020767 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

#6 A new Compose convert CLI

The new “Compose convert” command converts the compose file to platform’s canonical format. It’s the alias of the same config CLI that was introduced long back in Docker Compose binaries.

docker compose convert --help --services

Usage:  docker compose convert SERVICES

Converts the compose file to platform's canonical format

Aliases:
  convert, config

Options:
      --format string           Format the output. Values: [yaml | json] (default "yaml")
      --hash string             Print the service config hash, one per line.
      --no-interpolate          Don't interpolate environment variables.
      --profiles                Print the profile names, one per line.
  -q, --quiet                   Only validate the configuration, don't print anything.
      --resolve-image-digests   Pin image tags to digests.
      --services                Print the service names, one per line.
      --volumes                 Print the volume names, one per line.
ajeetraina@Ajeets-MacBook-Pro wordpress % docker compose convert --services
wordpress
db
ajeetraina@Ajeets-MacBook-Pro wordpress % docker compose config --services
db
wordpress
ajeetraina@Ajeets-MacBook-Pro wordpr

References

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index