Merge pull request #2823 from TomSweeneyRedHat/dev/tsweeney/override_from

Allow FROM to be overriden with from option
This commit is contained in:
OpenShift Merge Robot 2020-12-21 15:38:09 -05:00 committed by GitHub
commit 7734b68d6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 1 deletions

View File

@ -318,6 +318,7 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error {
DropCapabilities: iopts.CapDrop, DropCapabilities: iopts.CapDrop,
Err: stderr, Err: stderr,
ForceRmIntermediateCtrs: iopts.ForceRm, ForceRmIntermediateCtrs: iopts.ForceRm,
From: iopts.From,
IDMappingOptions: idmappingOptions, IDMappingOptions: idmappingOptions,
IIDFile: iopts.Iidfile, IIDFile: iopts.Iidfile,
Isolation: isolation, Isolation: isolation,

View File

@ -61,6 +61,9 @@ instructions read from the Containerfiles in the same way that environment
variables are, but which will not be added to environment variable list in the variables are, but which will not be added to environment variable list in the
resulting image's configuration. resulting image's configuration.
Please refer to the [BUILD TIME VARIABLES](#build-time-variables) section for the
list of variables that can be overridden within the Containerfile at run time.
**--cache-from** **--cache-from**
Images to utilise as potential cache sources. Buildah does not currently support caching so this is a NOOP. Images to utilise as potential cache sources. Buildah does not currently support caching so this is a NOOP.
@ -264,6 +267,11 @@ Recognized formats include *oci* (OCI image-spec v1.0, the default) and
Note: You can also override the default format by setting the BUILDAH\_FORMAT Note: You can also override the default format by setting the BUILDAH\_FORMAT
environment variable. `export BUILDAH_FORMAT=docker` environment variable. `export BUILDAH_FORMAT=docker`
**--from**
Overrides the first `FROM` instruction within the Containerfile. If there are multiple
FROM instructions in a Containerfile, only the first is changed.
**-h**, **--help** **-h**, **--help**
Print usage statement Print usage statement
@ -666,6 +674,23 @@ will convert /foo into a `shared` mount point. The propagation properties of th
mount can be changed directly. For instance if `/` is the source mount for mount can be changed directly. For instance if `/` is the source mount for
`/foo`, then use `mount --make-shared /` to convert `/` into a `shared` mount. `/foo`, then use `mount --make-shared /` to convert `/` into a `shared` mount.
## BUILD TIME VARIABLES
The ENV instruction in a Containerfile can be used to define variable values. When the image
is built, the values will persist in the container image. At times it is more convenient to
change the values in the Containerfile via a command-line option rather than changing the
values within the Containerfile itself.
The following variables can be used in conjunction with the `--build-arg` option to override the
corresponding values set in the Containerfile using the `ENV` instruction.
* HTTP_PROXY
* HTTPS_PROXY
* FTP_PROXY
* NO_PROXY
Please refer to the [Using Build Time Variables](#using-build-time-variables) section of the Examples.
## EXAMPLE ## EXAMPLE
### Build an image using local Containerfiles ### Build an image using local Containerfiles
@ -727,6 +752,11 @@ buildah bud --dns-search=example.com --dns=223.5.5.5 --dns-option=use-vc .
Note: supported compression formats are 'xz', 'bzip2', 'gzip' and 'identity' (no compression). Note: supported compression formats are 'xz', 'bzip2', 'gzip' and 'identity' (no compression).
### Using Build Time Variables
#### Replace the value set for the HTTP_PROXY environment variable within the Containerfile.
buildah bud --build-arg=HTTP_PROXY="http://127.0.0.1:8321"
## ENVIRONMENT ## ENVIRONMENT
**BUILD\_REGISTRY\_SOURCES** **BUILD\_REGISTRY\_SOURCES**

View File

@ -187,6 +187,9 @@ type BuildOptions struct {
LogRusage bool LogRusage bool
// Excludes is a list of excludes to be used instead of the .dockerignore file. // Excludes is a list of excludes to be used instead of the .dockerignore file.
Excludes []string Excludes []string
// From is the image name to use to replace the value specified in the first
// FROM instruction in the Containerfile
From string
} }
// BuildDockerfiles parses a set of one or more Dockerfiles (which may be // BuildDockerfiles parses a set of one or more Dockerfiles (which may be

View File

@ -114,6 +114,7 @@ type Executor struct {
logRusage bool logRusage bool
imageInfoLock sync.Mutex imageInfoLock sync.Mutex
imageInfoCache map[string]imageTypeAndHistoryAndDiffIDs imageInfoCache map[string]imageTypeAndHistoryAndDiffIDs
fromOverride string
} }
type imageTypeAndHistoryAndDiffIDs struct { type imageTypeAndHistoryAndDiffIDs struct {
@ -229,6 +230,7 @@ func NewExecutor(store storage.Store, options BuildOptions, mainNode *parser.Nod
jobs: jobs, jobs: jobs,
logRusage: options.LogRusage, logRusage: options.LogRusage,
imageInfoCache: make(map[string]imageTypeAndHistoryAndDiffIDs), imageInfoCache: make(map[string]imageTypeAndHistoryAndDiffIDs),
fromOverride: options.From,
} }
if exec.err == nil { if exec.err == nil {
exec.err = os.Stderr exec.err = os.Stderr
@ -245,6 +247,7 @@ func NewExecutor(store storage.Store, options BuildOptions, mainNode *parser.Nod
fmt.Fprintf(exec.out, prefix+format+suffix, args...) fmt.Fprintf(exec.out, prefix+format+suffix, args...)
} }
} }
for arg := range options.Args { for arg := range options.Args {
if _, isBuiltIn := builtinAllowedBuildArgs[arg]; !isBuiltIn { if _, isBuiltIn := builtinAllowedBuildArgs[arg]; !isBuiltIn {
exec.unusedArgs[arg] = struct{}{} exec.unusedArgs[arg] = struct{}{}
@ -522,6 +525,12 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image
switch strings.ToUpper(child.Value) { // first token - instruction switch strings.ToUpper(child.Value) { // first token - instruction
case "FROM": case "FROM":
if child.Next != nil { // second token on this line if child.Next != nil { // second token on this line
// If we have a fromOverride, replace the value of
// image name for the first FROM in the Containerfile.
if b.fromOverride != "" {
child.Next.Value = b.fromOverride
b.fromOverride = ""
}
base := child.Next.Value base := child.Next.Value
if base != "scratch" { if base != "scratch" {
// TODO: this didn't undergo variable and arg // TODO: this didn't undergo variable and arg

View File

@ -63,6 +63,7 @@ type BudResults struct {
IgnoreFile string IgnoreFile string
File []string File []string
Format string Format string
From string
Iidfile string Iidfile string
Label []string Label []string
Logfile string Logfile string
@ -187,6 +188,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
fs.StringVar(&flags.Creds, "creds", "", "use `[username[:password]]` for accessing the registry") fs.StringVar(&flags.Creds, "creds", "", "use `[username[:password]]` for accessing the registry")
fs.BoolVarP(&flags.DisableCompression, "disable-compression", "D", true, "don't compress layers by default") fs.BoolVarP(&flags.DisableCompression, "disable-compression", "D", true, "don't compress layers by default")
fs.BoolVar(&flags.DisableContentTrust, "disable-content-trust", false, "This is a Docker specific option and is a NOOP") fs.BoolVar(&flags.DisableContentTrust, "disable-content-trust", false, "This is a Docker specific option and is a NOOP")
fs.StringVar(&flags.From, "from", "", "image name used to replace the value in the first FROM instruction in the Containerfile")
fs.StringVar(&flags.IgnoreFile, "ignorefile", "", "path to an alternate .dockerignore file") fs.StringVar(&flags.IgnoreFile, "ignorefile", "", "path to an alternate .dockerignore file")
fs.StringSliceVarP(&flags.File, "file", "f", []string{}, "`pathname or URL` of a Dockerfile") fs.StringSliceVarP(&flags.File, "file", "f", []string{}, "`pathname or URL` of a Dockerfile")
fs.StringVar(&flags.Format, "format", DefaultFormat(), "`format` of the built image's manifest and metadata. Use BUILDAH_FORMAT environment variable to override.") fs.StringVar(&flags.Format, "format", DefaultFormat(), "`format` of the built image's manifest and metadata. Use BUILDAH_FORMAT environment variable to override.")
@ -233,6 +235,7 @@ func GetBudFlagsCompletions() commonComp.FlagCompletions {
flagCompletion["cert-dir"] = commonComp.AutocompleteDefault flagCompletion["cert-dir"] = commonComp.AutocompleteDefault
flagCompletion["creds"] = commonComp.AutocompleteNone flagCompletion["creds"] = commonComp.AutocompleteNone
flagCompletion["file"] = commonComp.AutocompleteDefault flagCompletion["file"] = commonComp.AutocompleteDefault
flagCompletion["from"] = commonComp.AutocompleteDefault
flagCompletion["format"] = commonComp.AutocompleteNone flagCompletion["format"] = commonComp.AutocompleteNone
flagCompletion["ignorefile"] = commonComp.AutocompleteDefault flagCompletion["ignorefile"] = commonComp.AutocompleteDefault
flagCompletion["iidfile"] = commonComp.AutocompleteDefault flagCompletion["iidfile"] = commonComp.AutocompleteDefault

View File

@ -2430,5 +2430,14 @@ EOF
expect_output --substring "FROM alpine" expect_output --substring "FROM alpine"
run_buildah 125 bud --network=bogus --signature-policy ${TESTSDIR}/policy.json -t ${target} ${TESTSDIR}/bud/containerfile run_buildah 125 bud --network=bogus --signature-policy ${TESTSDIR}/policy.json -t ${target} ${TESTSDIR}/bud/containerfile
expect_output "error checking for network namespace: stat bogus: no such file or directory"
}
@test "bud-replace-from-in-containerfile" {
_prefetch alpine
# override the first FROM (fedora) image in the Containerfile
# with alpine, leave the second (busybox) alone.
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --from=alpine ${TESTSDIR}/bud/build-with-from
expect_output --substring "STEP 1: FROM alpine AS builder"
expect_output --substring "STEP 2: FROM busybox"
} }

View File

@ -0,0 +1,4 @@
FROM fedora as builder
FROM busybox
COPY --from=builder /bin/df /tmp/df_tester