Skip to content

Commit

Permalink
Merge pull request #1125 from nialdaly/upgrade-docker-compose-v2
Browse files Browse the repository at this point in the history
`docker-compose` command changed to `docker compose` for upgrade to Docker Compose V2
  • Loading branch information
denis256 committed Jul 11, 2022
2 parents 044fd84 + 87ab7bd commit 78b2903
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ env: &env
HELM_VERSION: v3.8.0
KUBECONFIG: /home/circleci/.kube/config
BIN_BUILD_PARALLELISM: 3
DOCKER_COMPOSE_VERSION: v2.5.0

defaults: &defaults
machine:
Expand Down Expand Up @@ -74,6 +75,16 @@ install_docker_buildx: &install_docker_buildx
# Verify buildx is available
docker buildx create --use
# Installation script for the docker compose plugin. See: https://docs.docker.com/compose/install/#install-compose-on-linux-systems
install_docker_compose: &install_docker_compose
name: install docker compose
command: |
curl -sLO https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-linux-x86_64
mkdir -p ~/.docker/cli-plugins
mv docker-compose-linux-x86_64 ~/.docker/cli-plugins/docker-compose
chmod a+x ~/.docker/cli-plugins/docker-compose
docker compose version
configure_environment_for_gcp: &configure_environment_for_gcp
name: configure environment for gcp
command: |
Expand Down Expand Up @@ -169,6 +180,8 @@ jobs:
<<: *install_gruntwork_utils
- run:
<<: *install_docker_buildx
- run:
<<: *install_docker_compose

# The weird way you have to set PATH in Circle 2.0
- run: |
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/01_getting-started/packages-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Terratest's [modules folder](https://github.com/gruntwork-io/terratest/tree/mast
| **aws** | Functions that make it easier to work with the AWS APIs. Examples: find an EC2 Instance by tag, get the IPs of EC2 Instances in an ASG, create an EC2 KeyPair, look up a VPC ID. |
| **azure** | Functions that make it easier to work with the Azure APIs. Examples: get the size of a virtual machine, get the tags of a virtual machine. |
| **collections** | Go doesn't have much of a collections library built-in, so this package has a few helper methods for working with lists and maps. Examples: subtract two lists from each other. |
| **docker** | Functions that make it easier to work with Docker and Docker Compose. Examples: run `docker-compose` commands. |
| **docker** | Functions that make it easier to work with Docker and Docker Compose. Examples: run `docker compose` commands. |
| **environment** | Functions for interacting with os environment. Examples: check for first non empty environment variable in a list. |
| **files** | Functions for manipulating files and folders. Examples: check if a file exists, copy a folder and all of its contents. |
| **gcp** | Functions that make it easier to work with the GCP APIs. Examples: Add labels to a Compute Instance, get the Public IPs of an Instance, Get a list of Instances in a Managed Instance Group, Work with Storage Buckets and Objects. |
Expand Down
2 changes: 1 addition & 1 deletion examples/packer-docker-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The Docker-based tests in this folder are in some sense "unit tests" for the Pac


## Run the container
1. Run `docker-compose up`.
1. Run `docker compose up`.
1. You should now be able to access the sample web app at http://localhost:8080


Expand Down
1 change: 0 additions & 1 deletion examples/packer-docker-example/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# This file can be used with Docker and Docker Compose to run the web app in the Packer template 100% locally, without
# having to deploy anything to AWS.

version: '3'
services:
web_app:
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ require (
k8s.io/client-go v0.20.6
)

require github.com/slack-go/slack v0.10.3
require (
github.com/slack-go/slack v0.10.3
gotest.tools/v3 v3.0.3
)

require (
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand Down
2 changes: 1 addition & 1 deletion modules/docker/docker.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package docker allows to interact with Docker and docker-compose resources.
// Package docker allows to interact with Docker and docker compose resources.
package docker
41 changes: 30 additions & 11 deletions modules/docker/docker_compose.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package docker

import (
"strings"

"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/shell"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
"gotest.tools/v3/icmd"
)

// Options are Docker options.
Expand All @@ -15,7 +18,7 @@ type Options struct {
Logger *logger.Logger
}

// RunDockerCompose runs docker-compose with the given arguments and options and return stdout/stderr.
// RunDockerCompose runs docker compose with the given arguments and options and return stdout/stderr.
func RunDockerCompose(t testing.TestingT, options *Options, args ...string) string {
out, err := runDockerComposeE(t, false, options, args...)
if err != nil {
Expand All @@ -24,27 +27,43 @@ func RunDockerCompose(t testing.TestingT, options *Options, args ...string) stri
return out
}

// RunDockerComposeAndGetStdout runs docker-compose with the given arguments and options and returns only stdout.
// RunDockerComposeAndGetStdout runs docker compose with the given arguments and options and returns only stdout.
func RunDockerComposeAndGetStdOut(t testing.TestingT, options *Options, args ...string) string {
out, err := runDockerComposeE(t, true, options, args...)
require.NoError(t, err)
return out
}

// RunDockerComposeE runs docker-compose with the given arguments and options and return stdout/stderr.
// RunDockerComposeE runs docker compose with the given arguments and options and return stdout/stderr.
func RunDockerComposeE(t testing.TestingT, options *Options, args ...string) (string, error) {
return runDockerComposeE(t, false, options, args...)
}

func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args ...string) (string, error) {
cmd := shell.Command{
Command: "docker-compose",
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
// up in the same project and end up conflicting with each other.
Args: append([]string{"--project-name", t.Name()}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
var cmd shell.Command

dockerComposeVersionCmd := icmd.Command("docker", "compose", "version")

result := icmd.RunCmd(dockerComposeVersionCmd)

if result.ExitCode == 0 {
cmd = shell.Command{
Command: "docker",
Args: append([]string{"compose", "--project-name", strings.ToLower(t.Name())}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
}
} else {
cmd = shell.Command{
Command: "docker-compose",
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
// up in the same project and end up conflicting with each other.
Args: append([]string{"--project-name", strings.ToLower(t.Name())}, args...),
WorkingDir: options.WorkingDir,
Env: options.EnvVars,
Logger: options.Logger,
}
}

if stdout {
Expand Down

0 comments on commit 78b2903

Please sign in to comment.