Skip to content

Commit

Permalink
Add support for Docker Compose V2 generated container names and compo…
Browse files Browse the repository at this point in the history
…se-file specified container names
  • Loading branch information
artamonovkirill committed Feb 15, 2022
1 parent 95e84a0 commit e1be58a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 5 additions & 4 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ func (dc *LocalDockerCompose) applyStrategyToRunningContainer() error {
}

for k := range dc.WaitStrategyMap {
// TODO: if the compose file includes a container_name, this won't work,
// as we cannot delegate to the names generated by Docker Compose
containerName := dc.Identifier + "_" + k.service
// Docker compose appends "_1" to every started service by default. Trimming to match running docker container
f := filters.NewArgs(filters.Arg("name", containerName))
composeV2ContainerName := strings.ReplaceAll(containerName, "_", "-")
f := filters.NewArgs(
filters.Arg("name", containerName),
filters.Arg("name", composeV2ContainerName),
filters.Arg("name", k.service))
containerListOptions := types.ContainerListOptions{Filters: f}
containers, err := cli.ContainerList(context.Background(), containerListOptions)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,31 @@ func TestDockerComposeWithWaitHTTPStrategy(t *testing.T) {
assert.Contains(t, compose.Services, "nginx")
}

func TestDockerComposeWithContainerName(t *testing.T) {
path := "./testresources/docker-compose-container-name.yml"

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
}
defer destroyFn()

err := compose.
WithCommand([]string{"up", "-d"}).
WithEnv(map[string]string{
"bar": "BAR",
}).
WithExposedService("nginxy", 9080, wait.NewHTTPStrategy("/").WithPort("80/tcp").WithStartupTimeout(10*time.Second)).
Invoke()
checkIfError(t, err)

assert.Equal(t, 1, len(compose.Services))
assert.Contains(t, compose.Services, "nginx")
}

func TestDockerComposeWithWaitStrategy_NoExposedPorts(t *testing.T) {
path := "./testresources/docker-compose-no-exposed-ports.yml"

Expand Down
9 changes: 9 additions & 0 deletions testresources/docker-compose-container-name.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'
services:
nginx:
container_name: nginxy
image: nginx:stable-alpine
environment:
bar: ${bar}
ports:
- "9080:80"

0 comments on commit e1be58a

Please sign in to comment.