Skip to content

Instantly share code, notes, and snippets.

@thaJeztah
Last active February 8, 2023 19:38
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thaJeztah/b7950186212a49e91a806689e66b317d to your computer and use it in GitHub Desktop.
Save thaJeztah/b7950186212a49e91a806689e66b317d to your computer and use it in GitHub Desktop.
Docker Compose as a Docker CLI plugin

Run Docker Compose as a Docker CLI plugin

Just a quick fun experiment to try to make docker-compose work as a Docker CLI plugin

To use it (assuming you have docker compose installed):

  1. download a and install nightly build of the docker cli (linux macOS)

  2. create the plugins directory

    mkdir -p ~/.docker/cli-plugins
  3. download the "plugin", and save it as ~/.docker/cli-plugins/docker-compose (note: no .sh extension!)

    curl https://gist.githubusercontent.com/thaJeztah/b7950186212a49e91a806689e66b317d/raw/36d4c5854523502deed5b56842b0d34cd6ec0f70/docker-compose-plugin.sh > ~/.docker/cli-plugins/docker-compose 
  4. make it executable

    chmod +x ~/.docker/cli-plugins/docker-compose
  5. run the help command on the nightly docker cli to verify the plugin was installed

    ./docker help
    ...
    Commands:
      build                     Build an image from a Dockerfile
      compose     (Docker)      Define and run multi-container applications
      login                     Log in to a Docker registry
      logout                    Log out from a Docker registry
  6. enjoy!

    ./docker compose help
    Define and run multi-container applications with Docker.
    
    Usage:
      docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
      docker-compose -h|--help
    
    Options:
      -f, --file FILE             Specify an alternate compose file
                                  (default: docker-compose.yml)
      -p, --project-name NAME     Specify an alternate project name
#!/usr/bin/env bash
docker_cli_plugin_metadata() {
if [ -z "$DOCKER_COMPOSE_VERSION" ]; then
export DOCKER_COMPOSE_VERSION="$(docker-compose --version | cut -d " " -f 3 | cut -d "," -f 1)"
fi
local vendor="Docker"
local url="https://www.docker.com"
local description="Define and run multi-container applications"
cat <<-EOF
{"SchemaVersion":"0.1.0","Vendor":"${vendor}","Version":"${DOCKER_COMPOSE_VERSION}","ShortDescription":"${description}","URL":"${url}"}
EOF
}
case "$1" in
docker-cli-plugin-metadata)
docker_cli_plugin_metadata
;;
*)
exec "docker-$@"
;;
esac
@hyzyla
Copy link

hyzyla commented May 26, 2022

Nice concept!

@thaJeztah
Copy link
Author

@hyzyla heh, thanks! I completely forgot I write this up 😂 ; I should add a big disclaimer to it, because compose V2 (which was fully rewritten in Go), now defaults as a CLI plugin

@JonathonRichardson
Copy link

Yeah, it's not necessary for docker-compose anymore, but your little shell script was super useful to experiment with the CLI plugin concept, since it makes it really easy to answer the metadata request correctly.

Side note, your link to the nightly build for the Linux cli didn't have any packages newer than 2019. Did you mean to link to https://download.docker.com/linux/static/test/aarch64/? That directory includes much newer builds, including docker-22.06.0-beta.0.tgz dated June 6, 2022 (a week ago as of this writing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment