diff --git a/buildah.go b/buildah.go index 558cd9f41c1..0cd8718040c 100644 --- a/buildah.go +++ b/buildah.go @@ -257,6 +257,8 @@ type BuilderOptions struct { // or "scratch" to indicate that the container should not be based on // an image. FromImage string + // ContainerSuffix is the suffix to add for generated container names + ContainerSuffix string // Container is a desired name for the build container. Container string // PullPolicy decides whether or not we should pull the image that diff --git a/cmd/buildah/from.go b/cmd/buildah/from.go index 92da8cc56fc..101f92dbea0 100644 --- a/cmd/buildah/from.go +++ b/cmd/buildah/from.go @@ -36,6 +36,8 @@ type fromReply struct { *buildahcli.NameSpaceResults } +var suffix string + func init() { var ( fromDescription = "\n Creates a new working container, either from scratch or using a specified\n image as a starting point." @@ -74,6 +76,11 @@ func init() { flags.BoolVar(&opts.pullNever, "pull-never", false, "do not pull the image, use the image present in store if available") flags.BoolVarP(&opts.quiet, "quiet", "q", false, "don't output progress information when pulling images") flags.StringVar(&opts.signaturePolicy, "signature-policy", "", "`pathname` of signature policy file (not usually used)") + flags.StringVar(&suffix, "suffix", "", "suffix to add to intermediate containers") + if err := flags.MarkHidden("suffix"); err != nil { + panic(fmt.Sprintf("error marking the suffix flag as hidden: %v", err)) + } + if err := flags.MarkHidden("signature-policy"); err != nil { panic(fmt.Sprintf("error marking signature-policy as hidden: %v", err)) } @@ -284,6 +291,7 @@ func fromCmd(c *cobra.Command, args []string, iopts fromReply) error { options := buildah.BuilderOptions{ FromImage: args[0], Container: iopts.name, + ContainerSuffix: suffix, PullPolicy: pullPolicy, SignaturePolicyPath: signaturePolicy, SystemContext: systemContext, diff --git a/define/build.go b/define/build.go index 41f92ad34d8..c30eb7afa1e 100644 --- a/define/build.go +++ b/define/build.go @@ -80,6 +80,8 @@ type CommonBuildOptions struct { // BuildOptions can be used to alter how an image is built. type BuildOptions struct { + // ContainerSuffix it the name to suffix containers with + ContainerSuffix string // ContextDirectory is the default source location for COPY and ADD // commands. ContextDirectory string diff --git a/imagebuildah/executor.go b/imagebuildah/executor.go index 229e4a4b7f9..7f3d878bcf7 100644 --- a/imagebuildah/executor.go +++ b/imagebuildah/executor.go @@ -57,6 +57,7 @@ var builtinAllowedBuildArgs = map[string]bool{ // interface. It coordinates the entire build by using one or more // StageExecutors to handle each stage of the build. type Executor struct { + containerSuffix string logger *logrus.Logger stages map[string]*StageExecutor store storage.Store @@ -205,6 +206,7 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o } exec := Executor{ + containerSuffix: options.ContainerSuffix, logger: logger, stages: make(map[string]*StageExecutor), store: store, diff --git a/imagebuildah/stage_executor.go b/imagebuildah/stage_executor.go index 5be5085192c..493a17e26a0 100644 --- a/imagebuildah/stage_executor.go +++ b/imagebuildah/stage_executor.go @@ -539,6 +539,7 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo Args: ib.Args, FromImage: from, PullPolicy: pullPolicy, + ContainerSuffix: s.executor.containerSuffix, Registry: s.executor.registry, BlobDirectory: s.executor.blobDirectory, SignaturePolicyPath: s.executor.signaturePolicyPath, diff --git a/new.go b/new.go index d25eb4959e0..edcbb7bdca0 100644 --- a/new.go +++ b/new.go @@ -197,6 +197,9 @@ func newBuilder(ctx context.Context, store storage.Store, options BuilderOptions } name := "working-container" + if options.ContainerSuffix != "" { + name = options.ContainerSuffix + } if options.Container != "" { name = options.Container } else { diff --git a/tests/from.bats b/tests/from.bats index d6b2fd8ff07..e00e875ddde 100644 --- a/tests/from.bats +++ b/tests/from.bats @@ -92,6 +92,10 @@ load helpers run_buildah from --quiet --signature-policy ${TESTSDIR}/policy.json localhost/alpine2 expect_output "alpine2-working-container" run_buildah rm $output + tmp=$RANDOM + run_buildah from --suffix $tmp --quiet --signature-policy ${TESTSDIR}/policy.json localhost/alpine2 + expect_output "alpine2-$tmp" + run_buildah rm $output run_buildah rmi alpine alpine2 run_buildah from --quiet --pull=true --signature-policy ${TESTSDIR}/policy.json docker.io/alpine