Skip to content

bake: add file-relative path opt-in#3935

Open
crazy-max wants to merge 1 commit into
docker:masterfrom
crazy-max:bake-file-relative-paths
Open

bake: add file-relative path opt-in#3935
crazy-max wants to merge 1 commit into
docker:masterfrom
crazy-max:bake-file-relative-paths

Conversation

@crazy-max

Copy link
Copy Markdown
Member

fixes #1028
fixes #197

This adds an opt-in Bake mode that resolves local directory paths in target.context and target.contexts relative to the directory of the first Bake file when BUILDX_BAKE_FILE_RELATIVE_PATHS=1 is set.

Bake now accepts a parser option that rebases local directory build contexts after HCL or Compose files are parsed. The buildx bake command enables this option from BUILDX_BAKE_FILE_RELATIVE_PATHS, while cwd:// remains available for paths that should stay relative to the invocation directory.

@crazy-max

Copy link
Copy Markdown
Member Author

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@crazy-max crazy-max force-pushed the bake-file-relative-paths branch from fc54769 to 4ce75f7 Compare July 6, 2026 12:16
@crazy-max crazy-max requested a review from tonistiigi July 6, 2026 12:24
@crazy-max crazy-max marked this pull request as ready for review July 6, 2026 12:33

@tonistiigi tonistiigi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like default . does not atm take the relative mode into account


	t.Run("default-context-in-subdirectory", func(t *testing.T) {
		bakefile := []byte(`
target "default" {
  dockerfile-inline = <<EOT
FROM scratch
COPY marker /marker
EOT
}
`)

		dir := tmpdir(
			t,
			fstest.CreateDir("definitions", 0700),
			fstest.CreateFile("definitions/docker-bake.hcl", bakefile, 0600),
			fstest.CreateFile("definitions/marker", []byte("marker"), 0600),
		)
		dirDest := t.TempDir()

		out, err := bakeCmd(
			sb,
			withDir(dir),
			withArgs("--file", "definitions/docker-bake.hcl", "--set", "*.output=type=local,dest="+dirDest),
			withEnv("BUILDX_BAKE_FILE_RELATIVE_PATHS=1"),
		)
		require.NoError(t, err, out)
		require.FileExists(t, filepath.Join(dirDest, "marker"))
	})

(typically `;` on Windows and `:` elsewhere), but can be changed with `BUILDX_BAKE_PATH_SEPARATOR`.

By default, local directory build contexts in Bake files are resolved from the
current working directory. To opt in to resolving local directory build contexts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the "first" bake file? Rather than "current" bake file. Even if this is more complicated, I think that should be the difference as the benefit of this method is that there isn't one global target directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

buildx bake: Specifying --file in another directory leads to failure Relative paths in compose file not resolved correctly?

2 participants