Skip to content

Commit f024f1b

Browse files
committed
feat: add docker build custom options
1 parent 8695a25 commit f024f1b

File tree

11 files changed

+94
-59
lines changed

11 files changed

+94
-59
lines changed

internal/pkg/cli/deploy/workload.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,7 @@ func buildArgsPerContainer(name, workspacePath string, img ContainerImageIdentif
590590
Dockerfile: aws.StringValue(buildArgs.Dockerfile),
591591
Context: aws.StringValue(buildArgs.Context),
592592
Args: buildArgs.Args,
593-
CacheFrom: buildArgs.CacheFrom,
594-
Target: aws.StringValue(buildArgs.Target),
593+
Options: buildArgs.Options,
595594
Platform: mf.ContainerPlatform(),
596595
Tags: tags,
597596
Labels: labels,

internal/pkg/docker/dockerengine/dockerengine.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ const (
6060
containerStatusExited = "exited"
6161
)
6262

63+
// Deprecated predefined options to pass to `docker build`. The `options: [...]` config should be used instead.
64+
const (
65+
BuildOptionCacheFrom = "--cache-from"
66+
BuildOptionTarget = "--target"
67+
)
68+
6369
// DockerCmdClient represents the docker client to interact with the server via external commands.
6470
type DockerCmdClient struct {
6571
runner Cmd
@@ -85,8 +91,7 @@ type BuildArguments struct {
8591
Dockerfile string // Optional. One of Dockerfile or DockerfileContent is required. Dockerfile to pass to `docker build` via --file flag.
8692
DockerfileContent string // Optional. One of Dockerfile or DockerfileContent is required. Dockerfile content to pass to `docker build` via stdin.
8793
Context string // Optional. Build context directory to pass to `docker build`.
88-
Target string // Optional. The target build stage to pass to `docker build`.
89-
CacheFrom []string // Optional. Images to consider as cache sources to pass to `docker build`
94+
Options []string // Optional. Additional build options and flags to pass to `docker build`. See https://docs.docker.com/reference/cli/docker/image/build/#options
9095
Platform string // Optional. OS/Arch to pass to `docker build`.
9196
Args map[string]string // Optional. Build args to pass via `--build-arg` flags. Equivalent to ARG directives in dockerfile.
9297
Labels map[string]string // Required. Set metadata for an image.
@@ -130,21 +135,16 @@ func (in *BuildArguments) GenerateDockerBuildArgs(c DockerCmdClient) ([]string,
130135

131136
args := []string{"build"}
132137

138+
// Add all custom options first.
139+
for _, option := range in.Options {
140+
args = append(args, option)
141+
}
142+
133143
// Add additional image tags to the docker build call.
134144
for _, tag := range in.Tags {
135145
args = append(args, "-t", imageName(in.URI, tag))
136146
}
137147

138-
// Add cache from options.
139-
for _, imageFrom := range in.CacheFrom {
140-
args = append(args, "--cache-from", imageFrom)
141-
}
142-
143-
// Add target option.
144-
if in.Target != "" {
145-
args = append(args, "--target", in.Target)
146-
}
147-
148148
// Add platform option.
149149
if in.Platform != "" {
150150
args = append(args, "--platform", in.Platform)

internal/pkg/docker/dockerengine/dockerengine_test.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ func TestDockerCommand_Build(t *testing.T) {
4343
context string
4444
tags []string
4545
args map[string]string
46-
target string
47-
cacheFrom []string
46+
options []string
4847
envVars map[string]string
4948
labels map[string]string
5049
setupMocks func(controller *gomock.Controller)
@@ -177,18 +176,17 @@ func TestDockerCommand_Build(t *testing.T) {
177176
"-f", "mockPath/to/mockDockerfile"}, gomock.Any(), gomock.Any()).Return(nil)
178177
},
179178
},
180-
"runs with cache_from and target fields": {
181-
path: mockPath,
182-
tags: []string{"latest"},
183-
target: "foobar",
184-
cacheFrom: []string{"foo/bar:latest", "foo/bar/baz:1.2.3"},
179+
"success with options field": {
180+
path: mockPath,
181+
tags: []string{"latest"},
182+
options: []string{"--foo", "bar", "--baz"},
185183
setupMocks: func(c *gomock.Controller) {
186184
mockCmd = NewMockCmd(c)
187185
mockCmd.EXPECT().RunWithContext(ctx, "docker", []string{"build",
186+
"--foo",
187+
"bar",
188+
"--baz",
188189
"-t", fmt.Sprintf("%s:%s", mockURI, "latest"),
189-
"--cache-from", "foo/bar:latest",
190-
"--cache-from", "foo/bar/baz:1.2.3",
191-
"--target", "foobar",
192190
filepath.FromSlash("mockPath/to"),
193191
"-f", "mockPath/to/mockDockerfile"}, gomock.Any(), gomock.Any()).Return(nil)
194192
},
@@ -224,8 +222,7 @@ func TestDockerCommand_Build(t *testing.T) {
224222
DockerfileContent: tc.dockerfileContent,
225223
URI: mockURI,
226224
Args: tc.args,
227-
Target: tc.target,
228-
CacheFrom: tc.cacheFrom,
225+
Options: tc.options,
229226
Tags: tc.tags,
230227
Labels: tc.labels,
231228
}

internal/pkg/manifest/rd_web_svc_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func TestRequestDrivenWebService_UnmarshalYaml(t *testing.T) {
141141
" build:\n" +
142142
" dockerfile: ./Dockerfile\n" +
143143
" context: context/dir\n" +
144+
" options: [\"--pull\", \"--cache-from\", \"foo/bar:cache\"]\n" +
144145
" target: build-stage\n" +
145146
" cache_from:\n" +
146147
" - image:tag\n" +
@@ -158,6 +159,7 @@ func TestRequestDrivenWebService_UnmarshalYaml(t *testing.T) {
158159
BuildArgs: DockerBuildArgs{
159160
Context: aws.String("context/dir"),
160161
Dockerfile: aws.String("./Dockerfile"),
162+
Options: []string{"--pull", "--cache-from", "foo/bar:cache"},
161163
Target: aws.String("build-stage"),
162164
CacheFrom: []string{"image:tag"},
163165
Args: map[string]string{"a": "1", "b": "2"},

internal/pkg/manifest/svc_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ sidecars:
6767
build:
6868
dockerfile: "web/Dockerfile"
6969
context: "pathto/Dockerfile"
70+
options: ["--pull", "--cache-from", "foo/bar:cache"]
7071
target: "build-stage"
7172
cache_from:
7273
- foo/bar:latest
@@ -173,6 +174,7 @@ environments:
173174
BuildArgs: DockerBuildArgs{
174175
Dockerfile: aws.String("web/Dockerfile"),
175176
Context: aws.String("pathto/Dockerfile"),
177+
Options: []string{"--pull", "--cache-from", "foo/bar:cache"},
176178
Target: aws.String("build-stage"),
177179
CacheFrom: []string{"foo/bar:latest"},
178180
Args: map[string]string{

internal/pkg/manifest/workload.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ func (i *ImageLocationOrBuild) BuildConfig(rootDirectory string) *DockerBuildArg
176176
Dockerfile: aws.String(filepath.Join(rootDirectory, i.dockerfilePath())),
177177
Context: aws.String(filepath.Join(rootDirectory, i.contextPath())),
178178
Args: i.args(),
179-
Target: i.target(),
180-
CacheFrom: i.cacheFrom(),
179+
Options: i.options(),
181180
}
182181
}
183182

@@ -227,15 +226,23 @@ func (i *ImageLocationOrBuild) args() map[string]string {
227226
return i.Build.BuildArgs.Args
228227
}
229228

230-
// target returns the build target stage if it exists, otherwise nil.
231-
func (i *ImageLocationOrBuild) target() *string {
232-
return i.Build.BuildArgs.Target
233-
}
229+
// options returns the additional options from build section, if some are manually defined. Otherwise, it returns an empty array.
230+
func (i *ImageLocationOrBuild) options() []string {
231+
options := i.Build.BuildArgs.Options
232+
233+
// Add the deprecated target value
234+
if i.Build.BuildArgs.Target != nil {
235+
options = append(options, dockerengine.BuildOptionTarget, *i.Build.BuildArgs.Target)
236+
}
237+
238+
// Add the deprecated cacheFrom values
239+
if i.Build.BuildArgs.CacheFrom != nil {
240+
for _, cacheFrom := range i.Build.BuildArgs.CacheFrom {
241+
options = append(options, dockerengine.BuildOptionCacheFrom, cacheFrom)
242+
}
243+
}
234244

235-
// cacheFrom returns the cache from build section, if it exists.
236-
// Otherwise it returns nil.
237-
func (i *ImageLocationOrBuild) cacheFrom() []string {
238-
return i.Build.BuildArgs.CacheFrom
245+
return options
239246
}
240247

241248
// ImageOverride holds fields that override Dockerfile image defaults.
@@ -396,12 +403,13 @@ type DockerBuildArgs struct {
396403
Context *string `yaml:"context,omitempty"`
397404
Dockerfile *string `yaml:"dockerfile,omitempty"`
398405
Args map[string]string `yaml:"args,omitempty"`
399-
Target *string `yaml:"target,omitempty"`
400-
CacheFrom []string `yaml:"cache_from,omitempty"`
406+
Options []string `yaml:"options,omitempty"`
407+
Target *string `yaml:"target,omitempty"` // Deprecated. Use options with [--target, VALUE] instead.
408+
CacheFrom []string `yaml:"cache_from,omitempty"` // Deprecated. Use options with [--cache-from, VALUE] instead.
401409
}
402410

403411
func (b *DockerBuildArgs) isEmpty() bool {
404-
if b.Context == nil && b.Dockerfile == nil && b.Args == nil && b.Target == nil && b.CacheFrom == nil {
412+
if b.Context == nil && b.Dockerfile == nil && b.Args == nil && b.Options == nil && b.Target == nil && b.CacheFrom == nil {
405413
return true
406414
}
407415
return false

internal/pkg/manifest/workload_test.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,32 @@ func TestBuildArgs_UnmarshalYAML(t *testing.T) {
270270
BuildString: nil,
271271
},
272272
},
273-
"Dockerfile with cache from and target build opts": {
273+
"Dockerfile with build opts": {
274+
inContent: []byte(`build:
275+
options:
276+
- --pull
277+
- --target
278+
- "foobar"
279+
- --cache-from
280+
- foo/bar:latest
281+
- --cache-from
282+
- foo/bar/baz:1.2.3`),
283+
wantedStruct: BuildArgsOrString{
284+
BuildArgs: DockerBuildArgs{
285+
Options: []string{
286+
"--pull",
287+
"--target",
288+
"foobar",
289+
"--cache-from",
290+
"foo/bar:latest",
291+
"--cache-from",
292+
"foo/bar/baz:1.2.3",
293+
},
294+
},
295+
BuildString: nil,
296+
},
297+
},
298+
"Dockerfile with deprecated cache from and target build opts": {
274299
inContent: []byte(`build:
275300
cache_from:
276301
- foo/bar:latest
@@ -313,6 +338,7 @@ func TestBuildArgs_UnmarshalYAML(t *testing.T) {
313338
require.Equal(t, tc.wantedStruct.BuildArgs.Context, b.Build.BuildArgs.Context)
314339
require.Equal(t, tc.wantedStruct.BuildArgs.Dockerfile, b.Build.BuildArgs.Dockerfile)
315340
require.Equal(t, tc.wantedStruct.BuildArgs.Args, b.Build.BuildArgs.Args)
341+
require.Equal(t, tc.wantedStruct.BuildArgs.Options, b.Build.BuildArgs.Options)
316342
require.Equal(t, tc.wantedStruct.BuildArgs.Target, b.Build.BuildArgs.Target)
317343
require.Equal(t, tc.wantedStruct.BuildArgs.CacheFrom, b.Build.BuildArgs.CacheFrom)
318344
}
@@ -817,10 +843,23 @@ func TestBuildConfig(t *testing.T) {
817843
},
818844
},
819845
},
820-
"including build options": {
846+
"custom build options": {
821847
inBuild: BuildArgsOrString{
822848
BuildArgs: DockerBuildArgs{
823-
Target: aws.String("foobar"),
849+
Options: []string{"--foo"},
850+
},
851+
},
852+
wantedBuild: DockerBuildArgs{
853+
Dockerfile: aws.String(filepath.Join(mockWsRoot, "Dockerfile")),
854+
Context: aws.String(mockWsRoot),
855+
Options: []string{"--foo"},
856+
},
857+
},
858+
"deprecated build options": {
859+
inBuild: BuildArgsOrString{
860+
BuildArgs: DockerBuildArgs{
861+
Options: []string{"--foo"},
862+
Target: aws.String("foobar"),
824863
CacheFrom: []string{
825864
"foo/bar:latest",
826865
"foo/bar/baz:1.2.3",
@@ -830,11 +869,7 @@ func TestBuildConfig(t *testing.T) {
830869
wantedBuild: DockerBuildArgs{
831870
Dockerfile: aws.String(filepath.Join(mockWsRoot, "Dockerfile")),
832871
Context: aws.String(mockWsRoot),
833-
Target: aws.String("foobar"),
834-
CacheFrom: []string{
835-
"foo/bar:latest",
836-
"foo/bar/baz:1.2.3",
837-
},
872+
Options: []string{"--foo", "--target", "foobar", "--cache-from", "foo/bar:latest", "--cache-from", "foo/bar/baz:1.2.3"},
838873
},
839874
},
840875
}

site/content/docs/include/image-config.en.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ image:
1414
build:
1515
dockerfile: path/to/dockerfile
1616
context: context/dir
17-
target: build-stage
18-
cache_from:
19-
- image:tag
17+
option: ["--target", "build-stage", "--cache-from", "image:tag"]
2018
args:
2119
key: value
2220
```

site/content/docs/include/image-config.ja.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ image:
1414
build:
1515
dockerfile: path/to/dockerfile
1616
context: context/dir
17-
target: build-stage
18-
cache_from:
19-
- image:tag
17+
option: ["--target", "build-stage", "--cache-from", "image:tag"]
2018
args:
2119
key: value
2220
```

site/content/docs/manifest/rd-web-service.en.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ image:
155155
build:
156156
dockerfile: path/to/dockerfile
157157
context: context/dir
158-
target: build-stage
159-
cache_from:
160-
- image:tag
158+
option: ["--target", "build-stage", "--cache-from", "image:tag"]
161159
args:
162160
key: value
163161
```

site/content/docs/manifest/rd-web-service.ja.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ image:
157157
build:
158158
dockerfile: path/to/dockerfile
159159
context: context/dir
160-
target: build-stage
161-
cache_from:
162-
- image:tag
160+
option: ["--target", "build-stage", "--cache-from", "image:tag"]
163161
args:
164162
key: value
165163
```

0 commit comments

Comments
 (0)