-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dockerfile: implement hooks for
RUN
instructions
Close issue 4576 - - - e.g., ```bash buildctl build \ --frontend dockerfile.v0 \ --opt hook="$(cat hook.json)" ``` with `hook.json` as follows: ```json { "RUN": { "entrypoint": ["/dev/.dfhook/entrypoint"], "mounts": [ {"from": "example.com/hook", "target": "/dev/.dfhook"}, {"type": "secret", "source": "something", "target": "/etc/something"} ] } } ``` This will let the frontend treat `RUN foo` as: ```dockerfile RUN \ --mount=from=example.com/hook,target=/dev/.dfhook \ --mount=type=secret,source=something,target=/etc/something \ /dev/.dfhook/entrypoint foo ``` `docker history` will still show this as `RUN foo`. Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
- Loading branch information
1 parent
6f36dbf
commit 9af1620
Showing
12 changed files
with
234 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dockerfile | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/containerd/continuity/fs/fstest" | ||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/frontend/dockerui" | ||
"github.com/moby/buildkit/util/testutil/integration" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tonistiigi/fsutil" | ||
) | ||
|
||
var instHookTests = integration.TestFuncs( | ||
testInstructionHook, | ||
) | ||
|
||
func testInstructionHook(t *testing.T, sb integration.Sandbox) { | ||
integration.SkipOnPlatform(t, "windows") | ||
f := getFrontend(t, sb) | ||
|
||
dockerfile := []byte(` | ||
FROM busybox AS base | ||
RUN echo "$FOO" >/foo | ||
FROM scratch | ||
COPY --from=base /foo /foo | ||
`) | ||
|
||
dir := integration.Tmpdir( | ||
t, | ||
fstest.CreateFile("Dockerfile", dockerfile, 0600), | ||
) | ||
destDir := t.TempDir() | ||
|
||
c, err := client.New(sb.Context(), sb.Address()) | ||
require.NoError(t, err) | ||
defer c.Close() | ||
|
||
build := func(attrs map[string]string) string { | ||
_, err = f.Solve(sb.Context(), c, client.SolveOpt{ | ||
FrontendAttrs: attrs, | ||
Exports: []client.ExportEntry{ | ||
{ | ||
Type: client.ExporterLocal, | ||
OutputDir: destDir, | ||
}, | ||
}, | ||
LocalMounts: map[string]fsutil.FS{ | ||
dockerui.DefaultLocalNameDockerfile: dir, | ||
dockerui.DefaultLocalNameContext: dir, | ||
}, | ||
}, nil) | ||
require.NoError(t, err) | ||
p := filepath.Join(destDir, "foo") | ||
b, err := os.ReadFile(p) | ||
require.NoError(t, err) | ||
return strings.TrimSpace(string(b)) | ||
} | ||
|
||
require.Equal(t, "", build(nil)) | ||
|
||
const hook = ` | ||
{ | ||
"RUN": { | ||
"entrypoint": ["/dev/.dfhook/bin/busybox", "env", "FOO=BAR"], | ||
"mounts": [ | ||
{"from": "busybox:uclibc", "target": "/dev/.dfhook"} | ||
] | ||
} | ||
}` | ||
require.Equal(t, "BAR", build(map[string]string{"hook": hook})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.