Skip to content

Commit

Permalink
use '-' as separator by default for image name
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
  • Loading branch information
glours committed Jul 29, 2022
1 parent b49bd7c commit 150fd4b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 41 deletions.
8 changes: 4 additions & 4 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ func (o *projectOptions) toProject(services []string, po ...cli.ProjectOptionsFn
return nil, compose.WrapComposeError(err)
}

if o.Compatibility || utils.StringToBool(options.Environment["COMPOSE_COMPATIBILITY"]) {
api.Separator = "_"
}

project, err := cli.ProjectFromOptions(options)
if err != nil {
return nil, compose.WrapComposeError(err)
}

if o.Compatibility || utils.StringToBool(project.Environment["COMPOSE_COMPATIBILITY"]) {
compose.Separator = "_"
}

ef := o.EnvFile
if ef != "" && !filepath.IsAbs(ef) {
ef, err = filepath.Abs(ef)
Expand Down
5 changes: 4 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,14 @@ const (
UserCancel
)

// Separator is used for naming components
var Separator = "-"

// GetImageNameOrDefault computes the default image name for a service, used to tag built images
func GetImageNameOrDefault(service types.ServiceConfig, projectName string) string {
imageName := service.Image
if imageName == "" {
imageName = projectName + "_" + service.Name
imageName = projectName + Separator + service.Name
}
return imageName
}
3 changes: 0 additions & 3 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ import (
"github.com/sanathkr/go-yaml"
)

// Separator is used for naming components
var Separator = "-"

// NewComposeService create a local implementation of the compose.Service API
func NewComposeService(dockerCli command.Cli) api.Service {
return &composeService{
Expand Down
8 changes: 4 additions & 4 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func mustRecreate(expected types.ServiceConfig, actual moby.Container, policy st
}

func getContainerName(projectName string, service types.ServiceConfig, number int) string {
name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, Separator)
name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, api.Separator)
if service.ContainerName != "" {
name = service.ContainerName
}
Expand Down Expand Up @@ -553,8 +553,8 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
containerName := getCanonicalContainerName(c)
links = append(links,
format(containerName, linkName),
format(containerName, linkServiceName+Separator+strconv.Itoa(number)),
format(containerName, strings.Join([]string{projectName, linkServiceName, strconv.Itoa(number)}, Separator)),
format(containerName, linkServiceName+api.Separator+strconv.Itoa(number)),
format(containerName, strings.Join([]string{projectName, linkServiceName, strconv.Itoa(number)}, api.Separator)),
)
}
}
Expand All @@ -568,7 +568,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
containerName := getCanonicalContainerName(c)
links = append(links,
format(containerName, service.Name),
format(containerName, strings.TrimPrefix(containerName, projectName+Separator)),
format(containerName, strings.TrimPrefix(containerName, projectName+api.Separator)),
format(containerName, containerName),
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestBuildVolumeMount(t *testing.T) {

func TestServiceImageName(t *testing.T) {
assert.Equal(t, api.GetImageNameOrDefault(composetypes.ServiceConfig{Image: "myImage"}, "myProject"), "myImage")
assert.Equal(t, api.GetImageNameOrDefault(composetypes.ServiceConfig{Name: "aService"}, "myProject"), "myProject_aService")
assert.Equal(t, api.GetImageNameOrDefault(composetypes.ServiceConfig{Name: "aService"}, "myProject"), "myProject-aService")
}

func TestPrepareNetworkLabels(t *testing.T) {
Expand Down
30 changes: 15 additions & 15 deletions pkg/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ func TestLocalComposeBuild(t *testing.T) {

t.Run("build named and unnamed images", func(t *testing.T) {
// ensure local test run does not reuse previously build image
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
c.RunDockerOrExitError(t, "rmi", "custom-nginx")

res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build")

res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
})

t.Run("build with build-arg", func(t *testing.T) {
// ensure local test run does not reuse previously build image
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
c.RunDockerOrExitError(t, "rmi", "custom-nginx")

c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR")

res := c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
})

t.Run("build with build-arg set by env", func(t *testing.T) {
// ensure local test run does not reuse previously build image
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
c.RunDockerOrExitError(t, "rmi", "custom-nginx")

icmd.RunCmd(c.NewDockerComposeCmd(t,
Expand All @@ -67,20 +67,20 @@ func TestLocalComposeBuild(t *testing.T) {
cmd.Env = append(cmd.Env, "FOO=BAR")
})

res := c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
})

t.Run("build with multiple build-args ", func(t *testing.T) {
// ensure local test run does not reuse previously build image
c.RunDockerOrExitError(t, "rmi", "-f", "multi-args_multiargs")
c.RunDockerOrExitError(t, "rmi", "-f", "multi-args-multiargs")
cmd := c.NewDockerComposeCmd(t, "--project-directory", "fixtures/build-test/multi-args", "build")

icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
})

res := c.RunDockerCmd(t, "image", "inspect", "multi-args_multiargs")
res := c.RunDockerCmd(t, "image", "inspect", "multi-args-multiargs")
res.Assert(t, icmd.Expected{Out: `"RESULT": "SUCCESS"`})
})

Expand Down Expand Up @@ -131,7 +131,7 @@ func TestLocalComposeBuild(t *testing.T) {
})

t.Run("build as part of up", func(t *testing.T) {
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
c.RunDockerOrExitError(t, "rmi", "custom-nginx")

res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
Expand All @@ -145,7 +145,7 @@ func TestLocalComposeBuild(t *testing.T) {
output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))

c.RunDockerCmd(t, "image", "inspect", "build-test_nginx")
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
})

Expand All @@ -164,7 +164,7 @@ func TestLocalComposeBuild(t *testing.T) {

t.Run("cleanup build project", func(t *testing.T) {
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
c.RunDockerCmd(t, "rmi", "build-test_nginx")
c.RunDockerCmd(t, "rmi", "build-test-nginx")
c.RunDockerCmd(t, "rmi", "custom-nginx")
})
}
Expand Down Expand Up @@ -216,19 +216,19 @@ func TestBuildImageDependencies(t *testing.T) {
t.Cleanup(resetState)

// the image should NOT exist now
res := cli.RunDockerOrExitError(t, "image", "inspect", "build-dependencies_service")
res := cli.RunDockerOrExitError(t, "image", "inspect", "build-dependencies-service")
res.Assert(t, icmd.Expected{
ExitCode: 1,
Err: "Error: No such image: build-dependencies_service",
Err: "Error: No such image: build-dependencies-service",
})

res = cli.RunDockerComposeCmd(t, "build")
t.Log(res.Combined())

res = cli.RunDockerCmd(t,
"image", "inspect", "--format={{ index .RepoTags 0 }}",
"build-dependencies_service")
res.Assert(t, icmd.Expected{Out: "build-dependencies_service:latest"})
"build-dependencies-service")
res.Assert(t, icmd.Expected{Out: "build-dependencies-service:latest"})
}

t.Run("ClassicBuilder", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/e2e/fixtures/project-volume-bind-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ services:
image: nginx
container_name: frontend
volumes:
- project_data:/data
- project-data:/data

volumes:
project_data:
project-data:
driver: local
driver_opts:
type: none
Expand Down
6 changes: 3 additions & 3 deletions pkg/e2e/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func TestNetworks(t *testing.T) {
// fixture is shared with TestNetworkModes and is not safe to run concurrently
c := NewCLI(t)

const projectName = "network_e2e"
const projectName = "network-e2e"

t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
c.RunDockerOrExitError(t, "network", "rm", projectName+"_dbnet")
c.RunDockerOrExitError(t, "network", "rm", projectName+"-dbnet")
c.RunDockerOrExitError(t, "network", "rm", "microservices")
})

Expand Down Expand Up @@ -125,7 +125,7 @@ func TestIPAMConfig(t *testing.T) {
const projectName = "ipam_e2e"

t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
c.RunDockerOrExitError(t, "network", "rm", projectName+"_default")
c.RunDockerOrExitError(t, "network", "rm", projectName+"-default")
})

t.Run("up", func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/e2e/scan_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ func TestDisplayScanMessageAfterBuild(t *testing.T) {
t.Run("display on compose build", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p",
"scan-msg-test-compose-build", "build")
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-compose-build_nginx")
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-compose-build-nginx")
res.Assert(t, icmd.Expected{Err: utils.ScanSuggestMsg})
})

t.Run("do not display on compose build with quiet flag", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-quiet",
"build", "--quiet")
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
res = c.RunDockerCmd(t, "rmi", "-f", "scan-msg-test-quiet_nginx")
res = c.RunDockerCmd(t, "rmi", "-f", "scan-msg-test-quiet-nginx")
assert.Assert(t, !strings.Contains(res.Combined(), "No such image"))

res = c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test-q",
"build", "-q")
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-q_nginx")
defer c.RunDockerOrExitError(t, "rmi", "-f", "scan-msg-test-q-nginx")
assert.Assert(t, !strings.Contains(res.Combined(), "docker scan"), res.Combined())
})

_ = c.RunDockerOrExitError(t, "rmi", "scan-msg-test_nginx")
_ = c.RunDockerOrExitError(t, "rmi", "scan-msg-test-nginx")

t.Run("display on compose up if image is built", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-build-test/compose.yaml", "-p", "scan-msg-test", "up",
Expand Down
8 changes: 4 additions & 4 deletions pkg/e2e/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestLocalComposeVolume(t *testing.T) {

t.Run("up with build and no image name, volume", func(t *testing.T) {
// ensure local test run does not reuse previously build image
c.RunDockerOrExitError(t, "rmi", "compose-e2e-volume_nginx")
c.RunDockerOrExitError(t, "volume", "rm", projectName+"_staticVol")
c.RunDockerOrExitError(t, "rmi", "compose-e2e-volume-nginx")
c.RunDockerOrExitError(t, "volume", "rm", projectName+"-staticVol")
c.RunDockerOrExitError(t, "volume", "rm", "myvolume")
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up",
"-d")
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestLocalComposeVolume(t *testing.T) {
t.Run("cleanup volume project", func(t *testing.T) {
c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "--volumes")
ls := c.RunDockerCmd(t, "volume", "ls").Stdout()
assert.Assert(t, !strings.Contains(ls, projectName+"_staticVol"))
assert.Assert(t, !strings.Contains(ls, projectName+"-staticVol"))
assert.Assert(t, !strings.Contains(ls, "myvolume"))
})
}
Expand All @@ -107,7 +107,7 @@ func TestProjectVolumeBind(t *testing.T) {

c.RunDockerComposeCmd(t, "--project-name", projectName, "down")

c.RunDockerOrExitError(t, "volume", "rm", "-f", projectName+"_project_data").Assert(t, icmd.Success)
c.RunDockerOrExitError(t, "volume", "rm", "-f", projectName+"-project-data").Assert(t, icmd.Success)
cmd := c.NewCmdWithEnv([]string{"TEST_DIR=" + tmpDir},
"docker", "compose", "--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "up", "-d")
icmd.RunCmd(cmd).Assert(t, icmd.Success)
Expand Down

0 comments on commit 150fd4b

Please sign in to comment.