Skip to content

Commit

Permalink
Merge pull request #961 from tonistiigi/dockerfile-subdir
Browse files Browse the repository at this point in the history
dockerfile: allow subdirs for remote contexts
  • Loading branch information
tonistiigi authored Apr 26, 2019
2 parents d0b3ba5 + bb03ee6 commit 37ae84b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 24 additions & 1 deletion frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"net"
"path"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -46,6 +47,7 @@ const (
keyOverrideCopyImage = "override-copy-image" // remove after CopyOp implemented
keyNameContext = "contextkey"
keyNameDockerfile = "dockerfilekey"
keyContextSubDir = "contextsubdir"
)

var httpPrefix = regexp.MustCompile("^https?://")
Expand Down Expand Up @@ -122,6 +124,8 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
dockerfile2llb.WithInternalName(name),
)

fileop := useFileOp(opts, &caps)

var buildContext *llb.State
isScratchContext := false
if st, ok := detectGitContext(opts[localNameContext]); ok {
Expand Down Expand Up @@ -157,7 +161,6 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
return nil, errors.Errorf("failed to read downloaded context")
}
if isArchive(dt) {
fileop := useFileOp(opts, &caps)
if fileop {
bc := llb.Scratch().File(llb.Copy(httpContext, "/context", "/", &llb.CopyInfo{
AttemptUnpack: true,
Expand Down Expand Up @@ -190,6 +193,12 @@ func Build(ctx context.Context, c client.Client) (*client.Result, error) {
}
}

if buildContext != nil {
if sub, ok := opts[keyContextSubDir]; ok {
buildContext = scopeToSubDir(buildContext, fileop, sub)
}
}

def, err := src.Marshal(marshalOpts...)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal local source")
Expand Down Expand Up @@ -561,3 +570,17 @@ func useFileOp(args map[string]string, caps *apicaps.CapSet) bool {
}
return enabled && caps != nil && caps.Supports(pb.CapFileBase) == nil
}

func scopeToSubDir(c *llb.State, fileop bool, dir string) *llb.State {
if fileop {
bc := llb.Scratch().File(llb.Copy(*c, dir, "/", &llb.CopyInfo{
CopyDirContentsOnly: true,
}))
return &bc
}
unpack := llb.Image(dockerfile2llb.DefaultCopyImage, dockerfile2llb.WithInternalName("helper image for file operations")).
Run(llb.Shlexf("copy %s/. /out/", path.Join("/src", dir)), llb.ReadonlyRootFS(), dockerfile2llb.WithInternalName("filtering build context"))
unpack.AddMount("/src", *c, llb.Readonly)
bc := unpack.AddMount("/out", llb.Scratch())
return &bc
}
3 changes: 2 additions & 1 deletion frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3844,7 +3844,7 @@ func testTarContextExternalDockerfile(t *testing.T, sb integration.Sandbox) {
buf := bytes.NewBuffer(nil)
tw := tar.NewWriter(buf)
err := tw.WriteHeader(&tar.Header{
Name: "foo",
Name: "sub/dir/foo",
Typeflag: tar.TypeReg,
Size: int64(len(foo)),
Mode: 0644,
Expand Down Expand Up @@ -3882,6 +3882,7 @@ COPY foo bar
"build-arg:BUILDKIT_DISABLE_FILEOP": strconv.FormatBool(!isFileOp),
"context": url,
"dockerfilekey": builder.DefaultLocalNameDockerfile,
"contextsubdir": "sub/dir",
},
Session: []session.Attachable{up},
LocalDirs: map[string]string{
Expand Down

0 comments on commit 37ae84b

Please sign in to comment.