Skip to content

Commit

Permalink
fix: prefer docker-compose-v1 to work around race condition changes i…
Browse files Browse the repository at this point in the history
…n Compose V2 RC3 (#304)

* fix: prefer docker-compose-v1 to work around race condition introduced by Compose V2 RC3

* fix: cache the composeCommand() call

* style nit
  • Loading branch information
jbinto authored Sep 17, 2021
1 parent adb245f commit 9fa1de5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,12 @@ Examples:
log.Info("☐ performing preRun step for services")

for _, s := range selectedServices {
name := s.FullName()
if s.PreRun == "" {
log.Infof("\t☐ no preRun command for %s, continuing...\n", name)
continue
}

name := s.FullName()
log.Infof("\t☐ running preRun command %s for %s. this may take a long time.\n", s.PreRun, name)

err := docker.ComposeRun(s.DockerName(), s.PreRun)
Expand Down
27 changes: 24 additions & 3 deletions docker/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"fmt"
"os/exec"
"strconv"
"strings"

Expand All @@ -15,14 +16,34 @@ const (
stopTimeoutSecs = 2
)

var composeCmd string

func composeCommand() string {
if composeCmd != "" {
return composeCmd
}

_, err := exec.LookPath("docker-compose-v1")

if err == nil {
log.Debugf("docker-compose-v1 exists, using it to avoid race condition issue with Docker Compose version v2.0.0-rc.3...")
composeCmd = "docker-compose-v1"
} else {
log.Debugf("docker-compose-v1 does not exist, falling back to docker-compose (this might break if using Docker Compose version v2.0.0-rc.3)...")
composeCmd = "docker-compose"
}

return composeCmd
}

func composeFile() string {
return fmt.Sprintf("%s/docker-compose.yml", config.TBRootPath())
}

func ComposeExec(serviceName string, execArgs []string, cmd *command.Command) error {
args := []string{"-f", composeFile(), "exec", serviceName}
args = append(args, execArgs...)
err := cmd.Exec("docker-compose", args...)
err := cmd.Exec(composeCommand(), args...)
if err != nil {
return errors.Wrap(err, "failed to run docker-compose exec")
}
Expand All @@ -32,7 +53,7 @@ func ComposeExec(serviceName string, execArgs []string, cmd *command.Command) er
func ComposeLogs(services []string, cmd *command.Command) error {
args := []string{"-f", composeFile(), "logs", "-f"}
args = append(args, services...)
err := cmd.Exec("docker-compose", args...)
err := cmd.Exec(composeCommand(), args...)
if err != nil {
return errors.Wrap(err, "failed to run docker-compose logs")
}
Expand Down Expand Up @@ -69,7 +90,7 @@ func execDockerCompose(subcmd string, args ...string) error {
defer w.Close()
cmd := command.New(command.WithStdout(w), command.WithStderr(w))
args = append([]string{"-f", composeFile(), subcmd}, args...)
err := cmd.Exec("docker-compose", args...)
err := cmd.Exec(composeCommand(), args...)
if err != nil {
return errors.Wrapf(err, "failed to run docker-compose %s", subcmd)
}
Expand Down

0 comments on commit 9fa1de5

Please sign in to comment.