diff --git a/local/compose/build.go b/local/compose/build.go index f36af9bcc..9c88430f1 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -36,7 +36,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro opts := map[string]build.Options{} for _, service := range project.Services { if service.Build != nil { - imageName := getImageName(service, project) + imageName := getImageName(service, project.Name) opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName) } } @@ -44,14 +44,6 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro return s.build(ctx, project, opts) } -func getImageName(service types.ServiceConfig, project *types.Project) string { - imageName := service.Image - if imageName == "" { - imageName = project.Name + "_" + service.Name - } - return imageName -} - func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project) error { opts := map[string]build.Options{} for _, service := range project.Services { @@ -59,7 +51,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name) } - imageName := getImageName(service, project) + imageName := getImageName(service, project.Name) localImagePresent, err := s.localImagePresent(ctx, imageName) if err != nil { return err diff --git a/local/compose/create.go b/local/compose/create.go index ebea5d9a7..be6daea17 100644 --- a/local/compose/create.go +++ b/local/compose/create.go @@ -123,6 +123,14 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type return nil } +func getImageName(service types.ServiceConfig, projectName string) string { + imageName := service.Image + if imageName == "" { + imageName = projectName + "_" + service.Name + } + return imageName +} + func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) { hash, err := jsonHash(s) if err != nil { @@ -155,10 +163,6 @@ func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inher if len(s.Entrypoint) > 0 { entrypoint = strslice.StrSlice(s.Entrypoint) } - image := s.Image - if s.Image == "" { - image = fmt.Sprintf("%s_%s", p.Name, s.Name) - } var ( tty = s.Tty @@ -178,7 +182,7 @@ func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inher AttachStderr: true, AttachStdout: true, Cmd: runCmd, - Image: image, + Image: getImageName(s, p.Name), WorkingDir: s.WorkingDir, Entrypoint: entrypoint, NetworkDisabled: s.NetworkMode == "disabled", diff --git a/local/compose/create_test.go b/local/compose/create_test.go index fa1b3681e..58b717407 100644 --- a/local/compose/create_test.go +++ b/local/compose/create_test.go @@ -21,6 +21,7 @@ import ( "path/filepath" "testing" + "github.com/compose-spec/compose-go/types" composetypes "github.com/compose-spec/compose-go/types" mountTypes "github.com/docker/docker/api/types/mount" "gotest.tools/v3/assert" @@ -60,3 +61,8 @@ func TestBuildVolumeMount(t *testing.T) { assert.Equal(t, mount.Source, "myProject_myVolume") assert.Equal(t, mount.Type, mountTypes.TypeVolume) } + +func TestServiceImageName(t *testing.T) { + assert.Equal(t, getImageName(types.ServiceConfig{Image: "myImage"}, "myProject"), "myImage") + assert.Equal(t, getImageName(types.ServiceConfig{Name: "aService"}, "myProject"), "myProject_aService") +}