Skip to content

Commit

Permalink
Fix: use container name consistent with Docker Compose API (#633)
Browse files Browse the repository at this point in the history
* Set service naming per docker compoose version

* Docker compose v1 (python) and v2 (Go) use
  different service naming conventions. Set
  service name based on the version.

* refactor code, fix linter failure

* refactor

* Update internal/compose/compose.go

* remove extraneous newline

* Adjust Project{}

* Fix

* nit

* Fix: make format

Co-authored-by: Marcin Tojek <[email protected]>
Co-authored-by: mtojek <[email protected]>
  • Loading branch information
3 people authored Dec 24, 2021
1 parent c37ef6d commit ef32056
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions internal/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"github.com/Masterminds/semver"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"

Expand All @@ -26,6 +27,8 @@ import (
type Project struct {
name string
composeFilePaths []string

dockerComposeV1 bool
}

// Config represents a Docker Compose configuration file.
Expand Down Expand Up @@ -140,11 +143,20 @@ func NewProject(name string, paths ...string) (*Project, error) {
}
}

c := Project{
name,
paths,
}
var c Project
c.name = name
c.composeFilePaths = paths

ver, err := c.dockerComposeVersion()
if err != nil {
logger.Errorf("Unable to determine Docker Compose version: %v. Defaulting to 1.x", err)
c.dockerComposeV1 = true
return &c, nil
}
if ver.Major() == 1 {
logger.Debugf("Determined Docker Compose version: %v, the tool will use Compose V1", err)
c.dockerComposeV1 = true
}
return &c, nil
}

Expand Down Expand Up @@ -340,7 +352,28 @@ func (p *Project) runDockerComposeCmd(opts dockerComposeOptions) error {
return cmd.Run()
}

func (p *Project) dockerComposeVersion() (*semver.Version, error) {
var b bytes.Buffer

args := []string{
"version",
"--short",
}
if err := p.runDockerComposeCmd(dockerComposeOptions{args: args, stdout: &b}); err != nil {
return nil, errors.Wrap(err, "running Docker Compose version command failed")
}
dcVersion := b.String()
ver, err := semver.NewVersion(strings.Trim(dcVersion, "\n"))
if err != nil {
return nil, errors.Wrapf(err, "docker compose version is not a valid semver (value: %s)", dcVersion)
}
return ver, nil
}

// ContainerName method the container name for the service.
func (p *Project) ContainerName(serviceName string) string {
return fmt.Sprintf("%s_%s_1", p.name, serviceName)
if p.dockerComposeV1 {
return fmt.Sprintf("%s_%s_1", p.name, serviceName)
}
return fmt.Sprintf("%s-%s-1", p.name, serviceName)
}

0 comments on commit ef32056

Please sign in to comment.