Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 52b815d

Browse files
committed
tag services images according to compose file image attribute
use docker-compose `Image` attribute if set use {tag}-{service} if user has specified a tag with --tag never tag the invocation image Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 64ae00f commit 52b815d

4 files changed

Lines changed: 14 additions & 37 deletions

File tree

internal/commands/build/build.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,12 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) (refe
8888
}
8989
defer app.Cleanup()
9090

91-
serviceTag := ref
92-
if serviceTag == nil {
93-
named, err := reference.WithName(app.Metadata().Name)
94-
if err != nil {
95-
return nil, err
96-
}
97-
serviceTag, err = reference.WithTag(named, app.Metadata().Version)
98-
if err != nil {
99-
return nil, err
100-
}
101-
}
102-
103-
buildopts, err := parseCompose(app, contextPath, opt, serviceTag)
91+
buildopts, err := parseCompose(app, contextPath, opt)
10492
if err != nil {
10593
return nil, err
10694
}
10795

108-
buildopts["com.docker.app.invocation-image"], err = createInvocationImageBuildOptions(dockerCli, app, serviceTag)
96+
buildopts["com.docker.app.invocation-image"], err = createInvocationImageBuildOptions(dockerCli, app)
10997
if err != nil {
11098
return nil, err
11199
}
@@ -160,6 +148,9 @@ func getAppFolder(opt buildOptions, contextPath string) (string, error) {
160148
return "", fmt.Errorf("%s contains multiple *.dockerapp folders, use -f option to select the one to build", contextPath)
161149
}
162150
application = filepath.Join(contextPath, f.Name())
151+
if !f.IsDir() {
152+
return "", fmt.Errorf("%s isn't a directory", f.Name())
153+
}
163154
}
164155
}
165156
}
@@ -205,7 +196,7 @@ func updateBundle(dockerCli command.Cli, bundle *bundle.Bundle, resp map[string]
205196
return nil
206197
}
207198

208-
func createInvocationImageBuildOptions(dockerCli command.Cli, app *types.App, serviceTag reference.Reference) (build.Options, error) {
199+
func createInvocationImageBuildOptions(dockerCli command.Cli, app *types.App) (build.Options, error) {
209200
buildContext := bytes.NewBuffer(nil)
210201
if err := packager.PackInvocationImageContext(dockerCli, app, buildContext); err != nil {
211202
return build.Options{}, err
@@ -216,7 +207,6 @@ func createInvocationImageBuildOptions(dockerCli command.Cli, app *types.App, se
216207
ContextPath: "-",
217208
},
218209
Session: []session.Attachable{authprovider.NewDockerAuthProvider(os.Stderr)},
219-
Tags: []string{fmt.Sprintf("%s-installer", serviceTag.String())},
220210
}, nil
221211
}
222212

internal/commands/build/compose.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package build
22

33
import (
4-
"errors"
54
"fmt"
65
"path"
76

8-
"github.com/docker/distribution/reference"
9-
107
"github.com/docker/app/types"
118
"github.com/docker/buildx/build"
129
"github.com/docker/cli/cli/compose/loader"
@@ -15,7 +12,7 @@ import (
1512

1613
// parseCompose do parse app compose file and extract buildx Options
1714
// We don't rely on bake's ReadTargets + TargetsToBuildOpt here as we have to skip environment variable interpolation
18-
func parseCompose(app *types.App, contextPath string, options buildOptions, reference reference.Reference) (map[string]build.Options, error) {
15+
func parseCompose(app *types.App, contextPath string, options buildOptions) (map[string]build.Options, error) {
1916
parsed, err := loader.ParseYAML(app.Composes()[0])
2017
if err != nil {
2118
return nil, err
@@ -31,13 +28,11 @@ func parseCompose(app *types.App, contextPath string, options buildOptions, refe
3128
if service.Build == nil {
3229
continue
3330
}
34-
35-
if service.Name == "installer" {
36-
return nil, errors.New("'installer' is a reserved service name, please fix your docker-compose.yml file")
31+
var tags []string
32+
if service.Image != nil {
33+
tags = append(tags, *service.Image)
3734
}
3835

39-
tags := []string{fmt.Sprintf("%s-%s", reference.String(), service.Name)}
40-
4136
if service.Build.Dockerfile == "" {
4237
service.Build.Dockerfile = "Dockerfile"
4338
}

internal/commands/build/compose_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ package build
33
import (
44
"testing"
55

6-
"github.com/docker/distribution/reference"
7-
86
"github.com/docker/app/internal/packager"
97
"github.com/docker/buildx/build"
108
"gotest.tools/assert"
119
)
1210

1311
func Test_parseCompose(t *testing.T) {
14-
15-
tag, err := reference.Parse("test:1.0")
16-
assert.NilError(t, err)
17-
1812
tests := []struct {
1913
name string
2014
service string
@@ -28,7 +22,7 @@ func Test_parseCompose(t *testing.T) {
2822
ContextPath: "testdata/web",
2923
DockerfilePath: "testdata/web/Dockerfile",
3024
},
31-
Tags: []string{"test:1.0-web"},
25+
Tags: []string{"frontend"},
3226
},
3327
},
3428
{
@@ -39,7 +33,6 @@ func Test_parseCompose(t *testing.T) {
3933
ContextPath: "testdata/web",
4034
DockerfilePath: "testdata/web/Dockerfile.custom",
4135
},
42-
Tags: []string{"test:1.0-web"},
4336
},
4437
},
4538
{
@@ -51,15 +44,14 @@ func Test_parseCompose(t *testing.T) {
5144
DockerfilePath: "testdata/web/Dockerfile",
5245
},
5346
BuildArgs: map[string]string{"foo": "bar"},
54-
Tags: []string{"test:1.0-web"},
5547
},
5648
},
5749
}
5850
for _, tt := range tests {
5951
t.Run(tt.name, func(t *testing.T) {
6052
app, err := packager.Extract("testdata/" + tt.name)
6153
assert.NilError(t, err)
62-
got, err := parseCompose(app, "testdata", buildOptions{}, tag)
54+
got, err := parseCompose(app, "testdata", buildOptions{})
6355
assert.NilError(t, err)
6456
_, ok := got["dontwant"]
6557
assert.Assert(t, !ok, "parseCompose() should have excluded 'dontwant' service")

internal/packager/extract.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// findApp looks for an app in CWD or subdirs
17-
func FindApp(cwd string) (string, error) {
17+
func findApp(cwd string) (string, error) {
1818
if strings.HasSuffix(cwd, internal.AppExtension) {
1919
return cwd, nil
2020
}
@@ -47,7 +47,7 @@ func Extract(name string, ops ...func(*types.App) error) (*types.App, error) {
4747
if err != nil {
4848
return nil, errors.Wrap(err, "cannot resolve current working directory")
4949
}
50-
if name, err = FindApp(cwd); err != nil {
50+
if name, err = findApp(cwd); err != nil {
5151
return nil, err
5252
}
5353
}

0 commit comments

Comments
 (0)