Skip to content

Commit

Permalink
Add --target to bud command
Browse files Browse the repository at this point in the history
Signed-off-by: TomSweeneyRedHat <[email protected]>

Add the --target option to the bud command.  This allows the
user to specify the last stage to build in a multi stage
Dockerfile.

Addresses containers#632

Tests:
```
FROM fedora:latest
RUN touch /foo

FROM alpine:latest AS mytarget
RUN touch /1

STEP 1: FROM alpine:latest AS mytarget
Getting image source signatures
Copying blob 6c40cc604d8e: 2.63 MiB / 2.63 MiB [============================] 2s
Copying config caf27325b298: 1.48 KiB / 1.48 KiB [==========================] 0s
Writing manifest to image destination
Storing signatures
STEP 2: RUN touch /1
STEP 3: COMMIT containers-storage:[overlay@/var/lib/containers/storage+/var/run/containers/storage]localhost/tom:latest
Getting image source signatures
Skipping blob 503e53e365f3 (already present): 5.52 MiB / 5.52 MiB [=========] 0s
Copying blob 3266af627e70: 3.50 KiB / 3.50 KiB [============================] 0s
Copying config 52c07ff42762: 704 B / 704 B [================================] 0s
Writing manifest to image destination
Storing signatures
--> 52c07ff42762c07286ecb608c342df4af7105190ce30f70b678ff667a26c1842
STEP 4: COMMIT containers-storage:[overlay@/var/lib/containers/storage+/var/run/containers/storage]localhost/tom:latest
Getting image source signatures
Skipping blob 503e53e365f3 (already present): 5.52 MiB / 5.52 MiB [=========] 0s
Skipping blob 3266af627e70 (already present): 3.50 KiB / 3.50 KiB [=========] 0s
Copying config c75fd71d1f59: 704 B / 704 B [================================] 0s
Writing manifest to image destination
Storing signatures
--> c75fd71d1f59c500b2009151919b3ee183c0314db9e91f30e348daca6bbaa74b

IMAGE NAME                                               IMAGE TAG            IMAGE ID             CREATED AT             SIZE
docker.io/library/alpine                                 latest               caf27325b298         Jan 30, 2019 17:19     5.8 MB
localhost/tom

```

Signed-off-by: TomSweeneyRedHat <[email protected]>
  • Loading branch information
TomSweeneyRedHat committed Feb 4, 2019
1 parent bc38a74 commit 20d8a32
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/buildah/bud.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budResults) error {
RemoveIntermediateCtrs: iopts.Rm,
ForceRmIntermediateCtrs: iopts.ForceRm,
BlobDirectory: iopts.BlobCache,
Target: iopts.Target,
}

if iopts.Quiet {
Expand Down
1 change: 1 addition & 0 deletions contrib/completions/bash/buildah
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ return 1
--signature-policy
-t
--tag
--target
--ulimit
--userns
--userns-uid-map
Expand Down
6 changes: 6 additions & 0 deletions docs/buildah-bud.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ Specifies the name which will be assigned to the resulting image if the build
process completes successfully.
If _imageName_ does not include a registry name, the registry name *localhost* will be prepended to the image name.

**--target** *stageName*

Set the target build stage to build. When building a Dockerfile with multiple build stages, --target
can be used to specify an intermediate build stage by name as a final stage for the resulting image.
Commands after the target stage will be skipped.

**--tls-verify** *bool-value*

Require HTTPS and verify certificates when talking to container registries (defaults to true).
Expand Down
9 changes: 9 additions & 0 deletions imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ type BuildOptions struct {
ForceRmIntermediateCtrs bool
// BlobDirectory is a directory which we'll use for caching layer blobs.
BlobDirectory string
// Target the targeted FROM in the Dockerfile to build
Target string
}

// Executor is a buildah-based implementation of the imagebuilder.Executor
Expand Down Expand Up @@ -1441,6 +1443,13 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options BuildOpt
if err != nil {
return "", nil, errors.Wrap(err, "error reading multiple stages")
}
if options.Target != "" {
stagesTargeted, ok := stages.ByTarget(options.Target)
if !ok {
return "", nil, errors.Erroff("The target %q was not found in the provided Dockerfile", options.Target)
}
stages = stagesTargeted
}
return exec.Build(ctx, stages)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type BudResults struct {
SignaturePolicy string
Squash bool
Tag []string
Target string
TlsVerify bool
}

Expand Down Expand Up @@ -157,6 +158,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
fs.StringVar(&flags.SignaturePolicy, "signature-policy", "", "`pathname` of signature policy file (not usually used)")
fs.BoolVar(&flags.Squash, "squash", false, "Squash newly built layers into a single new layer. The build process does not currently support caching so this is a NOOP.")
fs.StringSliceVarP(&flags.Tag, "tag", "t", []string{}, "tagged `name` to apply to the built image")
fs.StringVar(&flags.Target, "target", "", "set the target build stage to build")
fs.BoolVar(&flags.TlsVerify, "tls-verify", true, "require HTTPS and verify certificates when accessing the registry")
return fs
}
Expand Down
13 changes: 13 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,16 @@ load helpers
run test -e $mnt/usr/local/bin/composer
[ "$status" -eq 0 ]
}

@test "bud-target" {
target=target
run buildah bud --signature-policy ${TESTSDIR}/policy.json -t ${target} --target mytarget ${TESTSDIR}/bud/target
[ "$status" -eq 0 ]
cid=$(buildah from ${target})
root=$(buildah mount ${cid})
run ls ${root}/1
echo "$output"
[ "$status" -eq 0 ]
buildah umount ${cid}
buildah rm ${cid}
}
5 changes: 5 additions & 0 deletions tests/bud/target/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM fedora:latest
RUN touch /foo

FROM alpine:latest AS mytarget
RUN touch /1

0 comments on commit 20d8a32

Please sign in to comment.