Skip to content

Commit

Permalink
Allow users to specify stdin into containers
Browse files Browse the repository at this point in the history
Some commands within a Containerfile, might need input from users.
For example confirmation commands from Apt.

Adding a --stdin flag will allows users to interact with containers
while running inside of buildah bud.

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Dec 22, 2020
1 parent 7734b68 commit dc57eea
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/buildah/bud.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -197,6 +198,10 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error {
return errors.Wrapf(err, "error evaluating symlinks in build context path")
}

var stdin io.Reader
if iopts.Stdin {
stdin = os.Stdin
}
var stdout, stderr, reporter *os.File
stdout = os.Stdout
stderr = os.Stderr
Expand Down Expand Up @@ -321,6 +326,7 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error {
From: iopts.From,
IDMappingOptions: idmappingOptions,
IIDFile: iopts.Iidfile,
In: stdin,
Isolation: isolation,
Labels: iopts.Label,
Layers: layers,
Expand Down
6 changes: 6 additions & 0 deletions docs/buildah-bud.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ Sign the built image using the GPG key that matches the specified fingerprint.
Squash all of the image's new layers into a single new layer; any preexisting layers
are not squashed.

**--stdin**

Pass stdin into the RUN containers. Sometime commands being RUN within a Containerfile
want to request information from the user. For example apt asking for a confirmation for install.
Use --stdin to be able to interact from the terminal during the build.

**--tag**, **-t** *imageName*

Specifies the name which will be assigned to the resulting image if the build
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type BudResults struct {
SignaturePolicy string
SignBy string
Squash bool
Stdin bool
Tag []string
Target string
TLSVerify bool
Expand Down Expand Up @@ -217,6 +218,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
panic(fmt.Sprintf("error marking the signature-policy flag as hidden: %v", err))
}
fs.BoolVar(&flags.Squash, "squash", false, "squash newly built layers into a single new layer")
fs.BoolVar(&flags.Stdin, "stdin", false, "pass stdin into containers")
fs.StringArrayVarP(&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.Int64Var(&flags.Timestamp, "timestamp", 0, "set created timestamp to the specified epoch seconds to allow for deterministic builds, defaults to current time")
Expand Down
17 changes: 17 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2441,3 +2441,20 @@ EOF
expect_output --substring "STEP 1: FROM alpine AS builder"
expect_output --substring "STEP 2: FROM busybox"
}

@test "bud test no --stdin" {
_prefetch alpine
mytmpdir=${TESTDIR}/my-dir
mkdir -p ${mytmpdir}
cat > $mytmpdir/Containerfile << _EOF
FROM alpine
RUN read -t 1 x && echo test got \<\$x\>
RUN touch /tmp/done
_EOF

# fail without --stdin
run_buildah 1 bud -t testbud --signature-policy ${TESTSDIR}/policy.json ${mytmpdir} <<< input

run_buildah bud --stdin -t testbud --signature-policy ${TESTSDIR}/policy.json ${mytmpdir} <<< input
expect_output --substring "test got <input>"
}

0 comments on commit dc57eea

Please sign in to comment.