Skip to content

Commit

Permalink
Handle WORKDIR with dangling target
Browse files Browse the repository at this point in the history
When the WORKDIR is a symlink and the target directory does not exist, RUN
commands would fail.  If the target doesn't exist, create the directory in
the container.

Fixes: containers#1569

Tip of the hat to @karelyatin for the initial fix

Signed-off-by: TomSweeneyRedHat <[email protected]>
  • Loading branch information
TomSweeneyRedHat committed May 3, 2019
1 parent 8b4ada2 commit 4fbef97
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1817,13 +1817,20 @@ func (b *Executor) deleteSuccessfulIntermediateCtrs() error {

func (s *StageExecutor) EnsureContainerPath(path string) error {
targetPath := filepath.Join(s.mountPoint, path)
_, err := os.Lstat(targetPath)
joinedPath, err := os.Lstat(targetPath)
if err != nil && os.IsNotExist(err) {
err = os.MkdirAll(targetPath, 0755)
}
if err != nil {
return errors.Wrapf(err, "error ensuring container path %q", path)
}
// If a symlink, check that the target directory exists in the container.
// If not, make it.
if joinedPath.Mode()&os.ModeSymlink != 0 {
if symTarget, err2 := os.Readlink(targetPath); err2 == nil {
os.MkdirAll(filepath.Join(s.mountPoint, symTarget), 0755)
}
}
return nil
}

Expand Down
16 changes: 16 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,22 @@ load helpers
run_buildah --debug=false run ${ctr} ls -alF /tempest
expect_output --substring "/tempest -> /var/lib/tempest/"

run_buildah --debug=false run ${ctr} ls -alF /etc/notareal.conf
expect_output --substring "\-rw\-rw\-r\-\-"
}

@test "bud WORKDIR isa symlink no target dir" {
target=alpine-image
ctr=alpine-ctr
run_buildah --debug=false bud --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/workdir-symlink/Dockerfile-2 ${TESTSDIR}/bud/workdir-symlink
expect_output --substring "STEP 2: RUN ln -sf "

run_buildah --debug=false from --signature-policy ${TESTSDIR}/policy.json --name=${ctr} ${target}
expect_output --substring ${ctr}

run_buildah --debug=false run ${ctr} ls -alF /tempest
expect_output --substring "/tempest -> /var/lib/tempest/"

run_buildah --debug=false run ${ctr} ls -alF /etc/notareal.conf
expect_output --substring "\-rw\-rw\-r\-\-"
}
Expand Down
7 changes: 7 additions & 0 deletions tests/bud/workdir-symlink/Dockerfile-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# No directory created for the target of the symlink
FROM alpine
RUN ln -sf /var/lib/tempest /tempest
WORKDIR /tempest
RUN touch /etc/notareal.conf
RUN chmod 664 /etc/notareal.conf
COPY Dockerfile-2 ./Dockerfile-2

0 comments on commit 4fbef97

Please sign in to comment.