Skip to content

Commit 78b2903

Browse files
authored
Merge pull request #1125 from nialdaly/upgrade-docker-compose-v2
`docker-compose` command changed to `docker compose` for upgrade to Docker Compose V2
2 parents 044fd84 + 87ab7bd commit 78b2903

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

.circleci/config.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ env: &env
1414
HELM_VERSION: v3.8.0
1515
KUBECONFIG: /home/circleci/.kube/config
1616
BIN_BUILD_PARALLELISM: 3
17+
DOCKER_COMPOSE_VERSION: v2.5.0
1718

1819
defaults: &defaults
1920
machine:
@@ -74,6 +75,16 @@ install_docker_buildx: &install_docker_buildx
7475
# Verify buildx is available
7576
docker buildx create --use
7677
78+
# Installation script for the docker compose plugin. See: https://docs.docker.com/compose/install/#install-compose-on-linux-systems
79+
install_docker_compose: &install_docker_compose
80+
name: install docker compose
81+
command: |
82+
curl -sLO https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-linux-x86_64
83+
mkdir -p ~/.docker/cli-plugins
84+
mv docker-compose-linux-x86_64 ~/.docker/cli-plugins/docker-compose
85+
chmod a+x ~/.docker/cli-plugins/docker-compose
86+
docker compose version
87+
7788
configure_environment_for_gcp: &configure_environment_for_gcp
7889
name: configure environment for gcp
7990
command: |
@@ -169,6 +180,8 @@ jobs:
169180
<<: *install_gruntwork_utils
170181
- run:
171182
<<: *install_docker_buildx
183+
- run:
184+
<<: *install_docker_compose
172185

173186
# The weird way you have to set PATH in Circle 2.0
174187
- run: |

docs/_docs/01_getting-started/packages-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Terratest's [modules folder](https://github.com/gruntwork-io/terratest/tree/mast
1919
| **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. |
2020
| **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. |
2121
| **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. |
22-
| **docker** | Functions that make it easier to work with Docker and Docker Compose. Examples: run `docker-compose` commands. |
22+
| **docker** | Functions that make it easier to work with Docker and Docker Compose. Examples: run `docker compose` commands. |
2323
| **environment** | Functions for interacting with os environment. Examples: check for first non empty environment variable in a list. |
2424
| **files** | Functions for manipulating files and folders. Examples: check if a file exists, copy a folder and all of its contents. |
2525
| **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. |

examples/packer-docker-example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The Docker-based tests in this folder are in some sense "unit tests" for the Pac
2929

3030

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

3535

examples/packer-docker-example/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# This file can be used with Docker and Docker Compose to run the web app in the Packer template 100% locally, without
22
# having to deploy anything to AWS.
3-
43
version: '3'
54
services:
65
web_app:

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ require (
4848
k8s.io/client-go v0.20.6
4949
)
5050

51-
require github.com/slack-go/slack v0.10.3
51+
require (
52+
github.com/slack-go/slack v0.10.3
53+
gotest.tools/v3 v3.0.3
54+
)
5255

5356
require (
5457
github.com/Azure/go-autorest v14.2.0+incompatible // indirect

modules/docker/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// Package docker allows to interact with Docker and docker-compose resources.
1+
// Package docker allows to interact with Docker and docker compose resources.
22
package docker

modules/docker/docker_compose.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package docker
22

33
import (
4+
"strings"
5+
46
"github.com/gruntwork-io/terratest/modules/logger"
57
"github.com/gruntwork-io/terratest/modules/shell"
68
"github.com/gruntwork-io/terratest/modules/testing"
79
"github.com/stretchr/testify/require"
10+
"gotest.tools/v3/icmd"
811
)
912

1013
// Options are Docker options.
@@ -15,7 +18,7 @@ type Options struct {
1518
Logger *logger.Logger
1619
}
1720

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

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

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

3942
func runDockerComposeE(t testing.TestingT, stdout bool, options *Options, args ...string) (string, error) {
40-
cmd := shell.Command{
41-
Command: "docker-compose",
42-
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
43-
// up in the same project and end up conflicting with each other.
44-
Args: append([]string{"--project-name", t.Name()}, args...),
45-
WorkingDir: options.WorkingDir,
46-
Env: options.EnvVars,
47-
Logger: options.Logger,
43+
var cmd shell.Command
44+
45+
dockerComposeVersionCmd := icmd.Command("docker", "compose", "version")
46+
47+
result := icmd.RunCmd(dockerComposeVersionCmd)
48+
49+
if result.ExitCode == 0 {
50+
cmd = shell.Command{
51+
Command: "docker",
52+
Args: append([]string{"compose", "--project-name", strings.ToLower(t.Name())}, args...),
53+
WorkingDir: options.WorkingDir,
54+
Env: options.EnvVars,
55+
Logger: options.Logger,
56+
}
57+
} else {
58+
cmd = shell.Command{
59+
Command: "docker-compose",
60+
// We append --project-name to ensure containers from multiple different tests using Docker Compose don't end
61+
// up in the same project and end up conflicting with each other.
62+
Args: append([]string{"--project-name", strings.ToLower(t.Name())}, args...),
63+
WorkingDir: options.WorkingDir,
64+
Env: options.EnvVars,
65+
Logger: options.Logger,
66+
}
4867
}
4968

5069
if stdout {

0 commit comments

Comments
 (0)