Skip to content

Commit

Permalink
dockerfile: fix named context replacement for child stages
Browse files Browse the repository at this point in the history
Fixes bug that didn't allow named context replacement
for stages that were inherited from another stage.

This is a regression from Dockerfile 1.11 ONBUILD changes
where code was changed to call init() later to allow ONBUILD
rules to change the dependency list of a stage. Calling
this reinit caused the child stage to pick up the its state
from the parent stage in Dockerfile again.

Signed-off-by: Tonis Tiigi <[email protected]>
(cherry picked from commit 4c8bd43)
  • Loading branch information
tonistiigi committed Dec 16, 2024
1 parent 873382b commit 987b409
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
}
}
allDispatchStates.addState(ds)
ds.base = nil // reset base set by addState
continue
}
}
Expand Down
38 changes: 38 additions & 0 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7110,6 +7110,44 @@ COPY --from=base /env_foobar /
dt, err = os.ReadFile(filepath.Join(destDir, "env_path"))
require.NoError(t, err)
require.Contains(t, string(dt), "/foobar:")

// this case checks replacing stage that is based on another stage.
// moby/buildkit#5578-2539397486

dockerfile = []byte(`
FROM busybox AS parent
FROM parent AS base
RUN echo base > /out
FROM base
RUN [ -f /etc/alpine-release ]
RUN [ ! -f /out ]
`)

dir = integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
)

f = getFrontend(t, sb)

destDir = t.TempDir()

_, err = f.Solve(sb.Context(), c, client.SolveOpt{
FrontendAttrs: map[string]string{
"context:base": "docker-image://" + target,
},
LocalMounts: map[string]fsutil.FS{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir,
},
Exports: []client.ExportEntry{
{
Type: client.ExporterLocal,
OutputDir: destDir,
},
},
}, nil)
require.NoError(t, err)
}

func testNamedImageContextPlatform(t *testing.T, sb integration.Sandbox) {
Expand Down

0 comments on commit 987b409

Please sign in to comment.