Skip to content

Commit

Permalink
Merge pull request #9179 from ndeloof/no_build_image
Browse files Browse the repository at this point in the history
as --no-build is set, set service image to default image name
  • Loading branch information
glours authored Jul 29, 2022
2 parents 236cd0c + abf5f73 commit b49bd7c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
3 changes: 3 additions & 0 deletions cmd/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func (opts createOptions) Apply(project *types.Project) {
if opts.noBuild {
for i, service := range project.Services {
service.Build = nil
if service.Image == "" {
service.Image = api.GetImageNameOrDefault(service, project.Name)
}
project.Services[i] = service
}
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,12 @@ const (
// UserCancel user cancelled compose up, we are stopping containers
UserCancel
)

// 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
}
return imageName
}
10 changes: 5 additions & 5 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
if service.Build == nil {
continue
}
imageName := getImageName(service, project.Name)
imageName := api.GetImageNameOrDefault(service, project.Name)
imagesToBuild = append(imagesToBuild, imageName)
buildOptions, err := s.toBuildOptions(project, service, imageName, options.SSHs)
if err != nil {
Expand Down Expand Up @@ -135,7 +135,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
}
// set digest as com.docker.compose.image label so we can detect outdated containers
for i, service := range project.Services {
image := getImageName(service, project.Name)
image := api.GetImageNameOrDefault(service, project.Name)
digest, ok := images[image]
if ok {
if project.Services[i].Labels == nil {
Expand All @@ -154,7 +154,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
if service.Image == "" && service.Build == nil {
return nil, fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
}
imageName := getImageName(service, project.Name)
imageName := api.GetImageNameOrDefault(service, project.Name)
_, localImagePresent := images[imageName]

if service.Build != nil {
Expand All @@ -176,7 +176,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
func (s *composeService) getLocalImagesDigests(ctx context.Context, project *types.Project) (map[string]string, error) {
var imageNames []string
for _, s := range project.Services {
imgName := getImageName(s, project.Name)
imgName := api.GetImageNameOrDefault(s, project.Name)
if !utils.StringContains(imageNames, imgName) {
imageNames = append(imageNames, imgName)
}
Expand All @@ -191,7 +191,7 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
}

for i := range project.Services {
imgName := getImageName(project.Services[i], project.Name)
imgName := api.GetImageNameOrDefault(project.Services[i], project.Name)
digest, ok := images[imgName]
if ok {
project.Services[i].CustomLabels.Add(api.ImageDigestLabel, digest)
Expand Down
3 changes: 2 additions & 1 deletion pkg/compose/build_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/compose-spec/compose-go/types"
buildx "github.com/docker/buildx/build"
"github.com/docker/cli/cli/command/image/build"
"github.com/docker/compose/v2/pkg/api"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/cli"
"github.com/docker/docker/pkg/archive"
Expand All @@ -45,7 +46,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, project *types.Proj
var nameDigests = make(map[string]string)
var errs error
err := project.WithServices(nil, func(service types.ServiceConfig) error {
imageName := getImageName(service, project.Name)
imageName := api.GetImageNameOrDefault(service, project.Name)
o, ok := opts[imageName]
if !ok {
return nil
Expand Down
12 changes: 2 additions & 10 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,6 @@ 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 (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig,
number int, inherit *moby.Container, autoRemove bool, attachStdin bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {

Expand Down Expand Up @@ -279,7 +271,7 @@ func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project,
AttachStderr: true,
AttachStdout: true,
Cmd: runCmd,
Image: getImageName(service, p.Name),
Image: api.GetImageNameOrDefault(service, p.Name),
WorkingDir: service.WorkingDir,
Entrypoint: entrypoint,
NetworkDisabled: service.NetworkMode == "disabled",
Expand Down Expand Up @@ -712,7 +704,7 @@ func (s *composeService) buildContainerVolumes(ctx context.Context, p types.Proj
inherit *moby.Container) (map[string]struct{}, []string, []mount.Mount, error) {
var mounts = []mount.Mount{}

image := getImageName(service, p.Name)
image := api.GetImageNameOrDefault(service, p.Name)
imgInspect, _, err := s.apiClient().ImageInspectWithRaw(ctx, image)
if err != nil {
return nil, nil, nil, err
Expand Down
8 changes: 5 additions & 3 deletions pkg/compose/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"sort"
"testing"

composetypes "github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/api"

composetypes "github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types"
mountTypes "github.com/docker/docker/api/types/mount"

"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -77,8 +79,8 @@ func TestBuildVolumeMount(t *testing.T) {
}

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

func TestPrepareNetworkLabels(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (s *composeService) getServiceImages(options api.DownOptions, project *type
continue
}
if image == "" {
image = getImageName(service, project.Name)
image = api.GetImageNameOrDefault(service, project.Name)
}
images[image] = struct{}{}
}
Expand Down

0 comments on commit b49bd7c

Please sign in to comment.