Skip to content

Commit

Permalink
as --no-build is set, set service image to default image name
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
ndeloof authored and glours committed Jul 27, 2022
1 parent 085d8e9 commit f52c5fb
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 @@ -127,6 +127,9 @@ func (opts createOptions) Apply(project *types.Project) {
for i, service := range project.Services {
service.Build = nil
project.Services[i] = service
if service.Image == "" {
service.Image = api.GetDefaultImageName(service, project.Name)
}
}
}
}
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
)

// GetDefaultImageName computes the default image name for a service, used to tag built images
func GetDefaultImageName(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.GetDefaultImageName(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.GetDefaultImageName(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.GetDefaultImageName(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.GetDefaultImageName(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.GetDefaultImageName(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 @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/docker/compose/v2/pkg/api"
"io"
"os"
"path/filepath"
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.GetDefaultImageName(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.GetDefaultImageName(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.GetDefaultImageName(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 @@ -65,8 +67,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.GetDefaultImageName(composetypes.ServiceConfig{Image: "myImage"}, "myProject"), "myImage")
assert.Equal(t, api.GetDefaultImageName(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.GetDefaultImageName(service, project.Name)
}
images[image] = struct{}{}
}
Expand Down

0 comments on commit f52c5fb

Please sign in to comment.