Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- **config** — `build.dockerfile` is resolved against the folder that
holds `devcontainer.json`, as the spec and the reference CLI do, not
against `build.context`. `{"dockerfile": "Dockerfile", "context": ".."}`
now builds `.devcontainer/Dockerfile` with the repo root as context,
instead of failing with `open Dockerfile: no such file or directory`.
`BuildSource.Dockerfile` is now an absolute host path. A config that
relied on the old behaviour (a `Dockerfile` in the context root
referenced as `"Dockerfile"` with `"context": ".."`) must use
`"../Dockerfile"`, as it already must for the reference CLI. Both
paths are now resolved after variable substitution, so
`"context": "${localWorkspaceFolder}"` is no longer joined onto the
config folder. `runtime.BuildSpec.Dockerfile` accepts an absolute path; one outside
the context is sent in the context tar under a reserved
`.dockerfile.<hash>` name, as `docker build -f` does.

- **features** — a feature option declared as a string (or untyped) that
is given a non-string value is converted the way the reference CLI
converts it, instead of failing with `option "packages" expects
Expand Down
26 changes: 21 additions & 5 deletions config/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ func determineSource(raw *rawConfig, configDir string) (Source, []Warning, error
if ctxPath == "" {
ctxPath = "."
}
if !filepath.IsAbs(ctxPath) {
ctxPath = filepath.Join(configDir, ctxPath)
}
// Dockerfile and Context stay as written here; substituteAll
// resolves them against the devcontainer.json folder after
// substitution, so ${localWorkspaceFolder}/… stays absolute.
return &BuildSource{
Dockerfile: raw.Build.Dockerfile,
Context: ctxPath,
Expand Down Expand Up @@ -534,8 +534,16 @@ func substituteAll(out *ResolvedConfig, ctx SubstitutionContext, source string)
case *ImageSource:
s.Image = subStr(s.Image, "/image")
case *BuildSource:
s.Dockerfile = subStr(s.Dockerfile, "/build/dockerfile")
s.Context = subStr(s.Context, "/build/context")
// The spec resolves both dockerfile and context against the
// devcontainer.json folder (dockerfile is NOT relative to
// context), after substitution, as the reference CLI does. The
// runtime re-expresses Dockerfile relative to the context it
// tars up.
configDir := filepath.Dir(source)
if df := subStr(s.Dockerfile, "/build/dockerfile"); df != "" {
s.Dockerfile = resolveAgainst(configDir, df)
}
s.Context = resolveAgainst(configDir, subStr(s.Context, "/build/context"))
s.Target = subStr(s.Target, "/build/target")
subMap(s.Args, "/build/args")
s.CacheFrom = subSlice(s.CacheFrom, "/build/cacheFrom")
Expand All @@ -546,6 +554,14 @@ func substituteAll(out *ResolvedConfig, ctx SubstitutionContext, source string)
}
}

// resolveAgainst returns p unchanged when absolute, else joined to dir.
func resolveAgainst(dir, p string) string {
if filepath.IsAbs(p) {
return p
}
return filepath.Join(dir, p)
}

func substituteLifecycle(lc *LifecycleCommands, sub func(s, path string) string) {
pairs := []struct {
cmds *[]LifecycleCommand
Expand Down
59 changes: 57 additions & 2 deletions config/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func TestResolveBytes_BuildSource(t *testing.T) {
if !ok {
t.Fatalf("Source = %+v", cfg.Source)
}
if bs.Dockerfile != "Dockerfile" {
t.Errorf("Dockerfile = %q", bs.Dockerfile)
// dockerfile resolves against configDir, not against context.
if bs.Dockerfile != "/home/u/proj/.devcontainer/Dockerfile" {
t.Errorf("Dockerfile = %q, want /home/u/proj/.devcontainer/Dockerfile", bs.Dockerfile)
}
// context "..", configDir "/home/u/proj/.devcontainer" → "/home/u/proj"
if bs.Context != "/home/u/proj" {
Expand All @@ -89,6 +90,60 @@ func TestResolveBytes_BuildSource(t *testing.T) {
}
}

func TestResolveBytes_BuildSourceDockerfilePath(t *testing.T) {
cases := []struct {
name, build, wantDockerfile, wantContext string
}{
{
name: "default context",
build: `{"dockerfile": "Dockerfile"}`,
wantDockerfile: "/home/u/proj/.devcontainer/Dockerfile",
wantContext: "/home/u/proj/.devcontainer",
},
{
name: "parent context, dockerfile beside config",
build: `{"dockerfile": "Dockerfile", "context": ".."}`,
wantDockerfile: "/home/u/proj/.devcontainer/Dockerfile",
wantContext: "/home/u/proj",
},
{
name: "parent context, dockerfile in parent",
build: `{"dockerfile": "../build/Dockerfile", "context": ".."}`,
wantDockerfile: "/home/u/proj/build/Dockerfile",
wantContext: "/home/u/proj",
},
{
name: "absolute dockerfile",
build: `{"dockerfile": "/srv/images/Dockerfile", "context": ".."}`,
wantDockerfile: "/srv/images/Dockerfile",
wantContext: "/home/u/proj",
},
{
// Substitution runs before resolution, so a variable that
// expands to an absolute path is not joined to configDir.
name: "substituted absolute paths",
build: `{"dockerfile": "${localWorkspaceFolder}/docker/Dockerfile", "context": "${localWorkspaceFolder}"}`,
wantDockerfile: "/home/u/proj/docker/Dockerfile",
wantContext: "/home/u/proj",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
cfg := resolveJSON(t, `{"build": `+tc.build+`}`, ResolveInput{})
bs, ok := cfg.Source.(*BuildSource)
if !ok {
t.Fatalf("Source = %+v", cfg.Source)
}
if bs.Dockerfile != tc.wantDockerfile {
t.Errorf("Dockerfile = %q, want %q", bs.Dockerfile, tc.wantDockerfile)
}
if bs.Context != tc.wantContext {
t.Errorf("Context = %q, want %q", bs.Context, tc.wantContext)
}
})
}
}

func TestResolveBytes_ComposeSource(t *testing.T) {
cfg := resolveJSON(t, `{
"dockerComposeFile": ["docker-compose.yml","docker-compose.override.yml"],
Expand Down
3 changes: 3 additions & 0 deletions config/resolved.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func (*ImageSource) isSource() {}
func (*ImageSource) Kind() SourceKind { return SourceImage }

type BuildSource struct {
// Dockerfile is an absolute host path: the spec resolves
// build.dockerfile against the devcontainer.json folder, not
// against Context. Empty when the config leaves it unset.
Dockerfile string
Context string
Args map[string]string
Expand Down
2 changes: 1 addition & 1 deletion design/resolved-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type ImageSource struct {
}

type BuildSource struct {
Dockerfile string // path relative to context
Dockerfile string // absolute, host-side (spec: relative to devcontainer.json's folder)
Context string // absolute, host-side
Args map[string]string
Target string
Expand Down
84 changes: 71 additions & 13 deletions runtime/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"archive/tar"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -44,19 +46,19 @@ func (r *Runtime) BuildImage(ctx context.Context, spec runtime.BuildSpec, events
return runtime.ImageRef{}, fmt.Errorf("BuildImage: spec.Tag required")
}

dockerfilePath, dockerfile, injected, err := locateDockerfile(spec.ContextPath, spec.Dockerfile)
if err != nil {
return runtime.ImageRef{}, err
}

// Stream the context directory as a tar over an io.Pipe so we
// don't buffer it all in memory.
pr, pw := io.Pipe()
go func() {
err := tarDirectory(spec.ContextPath, pw)
err := tarDirectory(spec.ContextPath, injected, pw)
_ = pw.CloseWithError(err)
}()

dockerfile := spec.Dockerfile
if dockerfile == "" {
dockerfile = "Dockerfile"
}

args := make(map[string]*string, len(spec.Args))
for k, v := range spec.Args {
v := v
Expand All @@ -71,7 +73,7 @@ func (r *Runtime) BuildImage(ctx context.Context, spec runtime.BuildSpec, events
// (which is unaffected by the session requirement) seeds the local
// store; BuildKit then picks up the cached manifest instead of
// reaching for the registry.
prePullBaseImages(ctx, r, spec.ContextPath, dockerfile, spec.Args, events)
prePullBaseImages(ctx, r, dockerfilePath, spec.Args, events)

res, err := r.api.ImageBuild(ctx, pr, client.ImageBuildOptions{
Dockerfile: dockerfile,
Expand Down Expand Up @@ -178,8 +180,50 @@ func streamBuildOutput(ctx context.Context, body io.ReadCloser, events chan<- ru
// avoids any reader special-casing the year-1 sentinel.
var tarEpoch = time.Unix(0, 0)

// tarFile is a regular file appended to a build-context tar after the
// directory walk.
type tarFile struct {
Name string
Body []byte
}

// locateDockerfile maps a BuildSpec.Dockerfile onto the host file to
// read (hostPath) and the name the daemon should open inside the
// context tar (tarName). dockerfile is either relative to contextPath
// (empty means "Dockerfile") or an absolute host path.
//
// An absolute path inside the context becomes its context-relative
// name. One outside the context can't be reached through the tar, so
// it is read and returned as injected, to be appended under a reserved
// name, matching `docker build -f <outside> <context>` (which the
// reference devcontainer CLI runs). The name carries a content hash
// rather than the docker CLI's random suffix so the tar stays
// reproducible.
func locateDockerfile(contextPath, dockerfile string) (hostPath, tarName string, injected *tarFile, err error) {
if dockerfile == "" {
dockerfile = "Dockerfile"
}
if !filepath.IsAbs(dockerfile) {
return filepath.Join(contextPath, dockerfile), filepath.ToSlash(dockerfile), nil, nil
}
absCtx, err := filepath.Abs(contextPath)
if err != nil {
return "", "", nil, fmt.Errorf("BuildImage: context path %s: %w", contextPath, err)
}
if rel, err := filepath.Rel(absCtx, dockerfile); err == nil && filepath.IsLocal(rel) {
return dockerfile, filepath.ToSlash(rel), nil, nil
}
body, err := os.ReadFile(dockerfile)
if err != nil {
return "", "", nil, fmt.Errorf("BuildImage: read dockerfile outside context: %w", err)
}
sum := sha256.Sum256(body)
name := ".dockerfile." + hex.EncodeToString(sum[:])[:12]
return dockerfile, name, &tarFile{Name: name, Body: body}, nil
}

// tarDirectory writes the contents of dir (recursively) into w as a
// non-gzipped tar archive. Symlinks are preserved as tar TypeSymlink
// non-gzipped tar archive, followed by extra when non-nil. Symlinks are preserved as tar TypeSymlink
// entries with their original target text; the daemon-side BuildKit
// frontend handles the resolution.
//
Expand All @@ -188,11 +232,11 @@ var tarEpoch = time.Unix(0, 0)
// Linkname. Some downstream tar readers reject those as malformed and
// abort the build mid-stream — common in compose-primary contexts
// containing node_modules/.bin/* or similar bin-symlinks.
func tarDirectory(dir string, w io.Writer) error {
func tarDirectory(dir string, extra *tarFile, w io.Writer) error {
tw := tar.NewWriter(w)
defer func() { _ = tw.Close() }()

return filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand Down Expand Up @@ -258,9 +302,23 @@ func tarDirectory(dir string, w io.Writer) error {
_ = f.Close()
return err
})
if err != nil || extra == nil {
return err
}
if err := tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: extra.Name,
Mode: 0o644,
Size: int64(len(extra.Body)),
ModTime: tarEpoch,
}); err != nil {
return err
}
_, err = tw.Write(extra.Body)
return err
}

// prePullBaseImages reads the Dockerfile at contextPath/dockerfile,
// prePullBaseImages reads the Dockerfile at dockerfilePath,
// extracts its frontend image (a `# syntax=` directive or the
// BUILDKIT_SYNTAX build arg) and FROM image references (resolving
// simple $ARG substitutions against ARG defaults in the file and the
Expand All @@ -277,8 +335,8 @@ func tarDirectory(dir string, w io.Writer) error {
// anonymous registry access when no session is attached, so this only
// changes the outcome on older engines; there it is what makes public
// images resolvable at all.
func prePullBaseImages(ctx context.Context, r *Runtime, contextPath, dockerfile string, buildArgs map[string]string, events chan<- runtime.BuildEvent) {
df, err := os.ReadFile(filepath.Join(contextPath, dockerfile))
func prePullBaseImages(ctx context.Context, r *Runtime, dockerfilePath string, buildArgs map[string]string, events chan<- runtime.BuildEvent) {
df, err := os.ReadFile(dockerfilePath)
if err != nil {
return
}
Expand Down
Loading