diff --git a/.gitignore b/.gitignore index 08e4c0323bf91..dd898d0a51aa3 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ jetpack_vendor/ .DS_Store *.code-workspace *.swp -# Custom environment for docker-compose (used by docker-compose.yml) +# Custom environment for Docker compose (used by docker-compose.yml) /.env /tools/docker/compose-extras.yml /tools/docker/compose-volumes.yml @@ -34,7 +34,7 @@ npm-debug.log phpcs.xml .phpunit.result.cache -## Things for docker-composer +## Things for Docker compose /tools/docker/data/* !/tools/docker/data/.gitkeep !/tools/docker/data/ssh.keys/ diff --git a/tools/check-development-environment.sh b/tools/check-development-environment.sh index 45f4e16adb5e0..188c4323b66a6 100755 --- a/tools/check-development-environment.sh +++ b/tools/check-development-environment.sh @@ -358,19 +358,23 @@ if [[ -z "$BIN" ]]; then else success "yes" - checking '[optional] Docker-compose is available' - BIN="$(command -v docker-compose)" - if [[ -z "$BIN" ]]; then + checking '[optional] Docker compose is available' + AS= + if docker compose version &>/dev/null; then + VER="$(docker compose version 2>/dev/null | sed -n -E 's/^(docker-compose|Docker Compose) version v?([0-9]+\.[0-9]+\.[0-9a-zA-Z.-]+)(, .*|\+.*)?$/\2/p')" + AS='docker compose' + elif BIN="$(command -v docker-compose)"; then + VER="$(docker-compose --version 2>/dev/null | sed -n -E 's/^(docker-compose|Docker Compose) version v?([0-9]+\.[0-9]+\.[0-9a-zA-Z.-]+)(, .*|\+.*)?$/\2/p')" + AS='docker-compose' + fi + if [[ -z "$AS" ]]; then warning "no" 'docker-supported-recommended' + elif [[ -z "$VER" ]]; then + warning "yes (as '$AS', version unknown)" + elif version_compare "$VER" "1.28"; then + success "yes (as '$AS', version $VER)" else - VER="$(docker-compose --version 2>/dev/null | sed -n -E 's/^(docker-compose|Docker Compose) version v?([0-9]+\.[0-9]+\.[0-9a-zA-Z.-]+)(, .*)?$/\2/p')" - if [[ -z "$VER" ]]; then - warning "yes (version unknown)" - elif version_compare "$VER" "1.28"; then - success "yes (version $VER)" - else - warning "yes (version $VER)" '' "Docker-compose at $BIN is version $VER. Version 1.28 or later is recommended." - fi + warning "yes (as '$AS', version $VER)" '' "Docker compose at $BIN is version $VER. Version 1.28 or later is recommended." fi checking '[optional] Docker is running' diff --git a/tools/cli/commands/docker.js b/tools/cli/commands/docker.js index 78fcdac8daf9d..b3257b4123766 100644 --- a/tools/cli/commands/docker.js +++ b/tools/cli/commands/docker.js @@ -4,6 +4,11 @@ import chalk from 'chalk'; import * as envfile from 'envfile'; import { dockerFolder, setConfig } from '../helpers/docker-config.js'; +/** + * How to run Docker compose. + */ +let dockerComposeCmd = null; + /** * Sets default options that are common for most of the commands * @@ -154,16 +159,28 @@ const checkProcessResult = res => { }; /** - * Executor for `docker-compose` commands + * Executor for `docker compose` commands * * @param {object} argv - Yargs * @param {Array} opts - Array of arguments * @param {object} envOpts - key-value pairs of the ENV variables to set */ const composeExecutor = ( argv, opts, envOpts ) => { - const res = executor( argv, () => - shellExecutor( argv, 'docker-compose', opts, { env: envOpts } ) - ); + if ( dockerComposeCmd === null ) { + if ( argv.v ) { + console.log( chalk.green( 'Checking how to run Docker compose' ) ); + } + if ( spawnSync( 'docker', [ 'compose', 'version' ], { stdio: 'ignore' } ).status === 0 ) { + dockerComposeCmd = [ 'docker', 'compose' ]; + } else if ( spawnSync( 'docker-compose', [ '--version' ], { stdio: 'ignore' } ).status === 0 ) { + dockerComposeCmd = [ 'docker-compose' ]; + } else { + console.error( chalk.red( `Neither 'docker compose' nor 'docker-compose' is available.` ) ); + process.exit( 1 ); + } + } + const [ cmd, ...args ] = dockerComposeCmd.concat( opts ); + const res = executor( argv, () => shellExecutor( argv, cmd, args, { env: envOpts } ) ); checkProcessResult( res ); }; @@ -501,7 +518,7 @@ const execJtCmdHandler = argv => { const dockerPs = spawnSync( 'docker', [ - "ps --filter 'name=jetpack_dev_wordpress' --filter 'status=running' --format='{{.ID}} {{.Names}}'", + "ps --filter 'name=jetpack_dev[_-]wordpress' --filter 'status=running' --format='{{.ID}} {{.Names}}'", ], { encoding: 'utf8', diff --git a/tools/docker/README.md b/tools/docker/README.md index 0d53ad61a1825..ff3ed2d622385 100644 --- a/tools/docker/README.md +++ b/tools/docker/README.md @@ -29,7 +29,7 @@ cp tools/docker/default.env tools/docker/.env Anything you put in `.env` overrides values in `default.env`. You should modify all the password fields for security, for example. -**Note**: in older versions of docker-compose (earlier than 1.28), you'll need to place that file at the root of the monorepo. +**Note**: in older versions of Docker compose (earlier than 1.28), you'll need to place that file at the root of the monorepo. ## Quick start @@ -59,7 +59,7 @@ WordPress’ `WP_SITEURL` and `WP_HOME` constants are configured to be dynamic i ## Custom mounts, environment Variables, `.env` Files, and Ports You can control some of the behavior of Jetpack's Docker configuration with environment variables. Note, though, that there are two types of environments: -1. The host environment in which the `jetpack docker *` (`docker-compose`) commands run when creating/managing the containers. +1. The host environment in which the `jetpack docker *` (Docker compose) commands run when creating/managing the containers. 2. The containers' environments. ### Host Environment @@ -90,7 +90,7 @@ Users can extended these configurations further via override config file `tools/ The default config file `tools/docker/jetpack-docker-config-default.yml` includes inline comments explaining the structure of config, but here's quick overview. The configuration is grouped per environment type: `default`, `dev`, `e2e`. Each type may define `volumeMappings` and `extras`: * `volumeMappings` - list of key value pairs which defines local directory mappings with following structure: local_path: wordpress_container_path -* `extras` - basically any other configuration that is supported by `docker-compose` +* `extras` - basically any other configuration that is supported by Docker compose. ## Working with containers @@ -144,7 +144,7 @@ Stops all containers. jetpack docker down ``` -Will stop all of the containers created by this docker-compose configuration and remove them, too. It won’t remove the images. Just the containers that have just been stopped. +Will stop all of the containers created by this Docker compose configuration and remove them, too. It won’t remove the images. Just the containers that have just been stopped. ### Running unit tests diff --git a/tools/docker/compose-volumes.yml.sample b/tools/docker/compose-volumes.yml.sample index 0bcc04316784f..89cd00b49d858 100644 --- a/tools/docker/compose-volumes.yml.sample +++ b/tools/docker/compose-volumes.yml.sample @@ -5,7 +5,7 @@ ######## ## List volumes for plugins, themes, and so on here. Format is a sequence as appropriate for -## docker-compose's service.volumes. In brief, +## Docker compose's service.volumes. In brief, ## - Begin each line with a "-" followed by a space. ## - Line consists of two fields, local path and docker path, separated by a colon. ## - Local path may be absolute, or relative to this file by beginning with "./" or "../". diff --git a/tools/docker/docker-compose.yml b/tools/docker/docker-compose.yml index dc8782599b379..9a1910556a680 100644 --- a/tools/docker/docker-compose.yml +++ b/tools/docker/docker-compose.yml @@ -23,7 +23,7 @@ services: ## - The container wordpress is a very basic but custom container with WordPress and all of the tools we need ## for development. - ## - The container will be named jetpack_wordpress for easy reference when running docker/docker-compose commands + ## - The container will be named jetpack_dev_wordpress_1 or jetpack_dev-wordpress-1 (depending on your Docker compose version) for easy reference when running Docker commands ## ## Here we map the following: ## - The docker/wordpress-develop directory where we'll get WordPress source code with unit tests diff --git a/tools/docker/jetpack-docker-config-default.yml b/tools/docker/jetpack-docker-config-default.yml index b8592a6b13b72..8498d44384cbd 100644 --- a/tools/docker/jetpack-docker-config-default.yml +++ b/tools/docker/jetpack-docker-config-default.yml @@ -9,7 +9,7 @@ default: tools/docker/mu-plugins: /var/www/html/wp-content/mu-plugins # Extra configuration (none by default). Anything set under this key will be written out as - # an extra docker-compose configuration file. + # an extra Docker compose configuration file. extras: # Dev environment overrides. None by default.