Skip to content

Commit f089eb2

Browse files
committed
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 <[email protected]>
1 parent e200776 commit f089eb2

File tree

12 files changed

+232
-25
lines changed

12 files changed

+232
-25
lines changed

docs/reference/buildctl.md

+39
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,45 @@ $ buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=.
181181
$ buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --oci-layout foo2=/home/dir/oci --opt context:alpine=oci-layout://foo2@sha256:bd04a5b26dec16579cd1d7322e949c5905c4742269663fcbc84dcb2e9f4592fb
182182
```
183183

184+
##### Instruction hooks
185+
<!-- TODO: s/master/v0.15/ -->
186+
In the master branch, the Dockerfile frontend also supports "instruction hooks".
187+
188+
e.g.,
189+
190+
```bash
191+
buildctl build \
192+
--frontend dockerfile.v0 \
193+
--opt hook="$(cat hook.json)"
194+
```
195+
with `hook.json` as follows:
196+
```json
197+
{
198+
"RUN": {
199+
"entrypoint": ["/dev/.dfhook/entrypoint"],
200+
"mounts": [
201+
{"from": "example.com/hook", "target": "/dev/.dfhook"},
202+
{"type": "secret", "source": "something", "target": "/etc/something"}
203+
]
204+
}
205+
}
206+
```
207+
208+
This will let the frontend treat `RUN foo` as:
209+
```dockerfile
210+
RUN \
211+
--mount=from=example.com/hook,target=/dev/.dfhook \
212+
--mount=type=secret,source=something,target=/etc/something \
213+
/dev/.dfhook/entrypoint foo
214+
```
215+
216+
`docker history` will still show this as `RUN foo`.
217+
218+
<!--
219+
TODO: add example hook images to show concrete use-cases
220+
https://github.com/moby/buildkit/issues/4576
221+
-->
222+
184223
#### gateway-specific options
185224

186225
The `gateway.v0` frontend passes all of its `--opt` options on to the OCI image that is called to convert the

frontend/dockerfile/dockerfile2llb/convert.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/moby/buildkit/frontend/dockerfile/parser"
2828
"github.com/moby/buildkit/frontend/dockerfile/shell"
2929
"github.com/moby/buildkit/frontend/dockerui"
30+
"github.com/moby/buildkit/frontend/dockerui/types"
3031
"github.com/moby/buildkit/frontend/subrequests/lint"
3132
"github.com/moby/buildkit/frontend/subrequests/outline"
3233
"github.com/moby/buildkit/frontend/subrequests/targets"
@@ -146,7 +147,7 @@ func ListTargets(ctx context.Context, dt []byte) (*targets.List, error) {
146147
return nil, err
147148
}
148149

149-
stages, _, err := instructions.Parse(dockerfile.AST, nil)
150+
stages, _, err := instructions.Parse(dockerfile.AST, nil, instructions.ParseOpts{})
150151
if err != nil {
151152
return nil, err
152153
}
@@ -288,7 +289,10 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
288289

289290
proxyEnv := proxyEnvFromBuildArgs(opt.BuildArgs)
290291

291-
stages, metaArgs, err := instructions.Parse(dockerfile.AST, lint)
292+
parseOpts := instructions.ParseOpts{
293+
InstructionHook: opt.InstructionHook,
294+
}
295+
stages, metaArgs, err := instructions.Parse(dockerfile.AST, lint, parseOpts)
292296
if err != nil {
293297
return nil, err
294298
}
@@ -688,6 +692,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
688692
llbCaps: opt.LLBCaps,
689693
sourceMap: opt.SourceMap,
690694
lint: lint,
695+
instHook: opt.InstructionHook,
691696
}
692697

693698
if err = dispatchOnBuildTriggers(d, d.image.Config.OnBuild, opt); err != nil {
@@ -838,6 +843,7 @@ type dispatchOpt struct {
838843
llbCaps *apicaps.CapSet
839844
sourceMap *llb.SourceMap
840845
lint *linter.Linter
846+
instHook *types.InstructionHook
841847
}
842848

843849
func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
@@ -1081,6 +1087,9 @@ type command struct {
10811087
}
10821088

10831089
func dispatchOnBuildTriggers(d *dispatchState, triggers []string, opt dispatchOpt) error {
1090+
parseOpts := instructions.ParseOpts{
1091+
InstructionHook: opt.instHook,
1092+
}
10841093
for _, trigger := range triggers {
10851094
ast, err := parser.Parse(strings.NewReader(trigger))
10861095
if err != nil {
@@ -1089,7 +1098,7 @@ func dispatchOnBuildTriggers(d *dispatchState, triggers []string, opt dispatchOp
10891098
if len(ast.AST.Children) != 1 {
10901099
return errors.New("onbuild trigger should be a single expression")
10911100
}
1092-
ic, err := instructions.ParseCommand(ast.AST.Children[0])
1101+
ic, err := instructions.ParseCommand(ast.AST.Children[0], parseOpts)
10931102
if err != nil {
10941103
return err
10951104
}
@@ -1185,6 +1194,12 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
11851194
args = withShell(d.image, args)
11861195
}
11871196

1197+
argsForHistory := args
1198+
if dopt.instHook != nil && dopt.instHook.Run != nil {
1199+
args = append(dopt.instHook.Run.Entrypoint, args...)
1200+
// leave argsForHistory unmodified
1201+
}
1202+
11881203
env, err := d.state.Env(context.TODO())
11891204
if err != nil {
11901205
return err
@@ -1251,7 +1266,7 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
12511266
}
12521267

12531268
d.state = d.state.Run(opt...).Root()
1254-
return commitToHistory(&d.image, "RUN "+runCommandString(args, d.buildArgs, shell.BuildEnvs(env)), true, &d.state, d.epoch)
1269+
return commitToHistory(&d.image, "RUN "+runCommandString(argsForHistory, d.buildArgs, shell.BuildEnvs(env)), true, &d.state, d.epoch)
12551270
}
12561271

12571272
func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bool, opt *dispatchOpt) error {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package dockerfile
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
9+
"github.com/containerd/continuity/fs/fstest"
10+
"github.com/moby/buildkit/client"
11+
"github.com/moby/buildkit/frontend/dockerui"
12+
"github.com/moby/buildkit/util/testutil/integration"
13+
"github.com/stretchr/testify/require"
14+
"github.com/tonistiigi/fsutil"
15+
)
16+
17+
var instHookTests = integration.TestFuncs(
18+
testInstructionHook,
19+
)
20+
21+
func testInstructionHook(t *testing.T, sb integration.Sandbox) {
22+
integration.SkipOnPlatform(t, "windows")
23+
f := getFrontend(t, sb)
24+
25+
dockerfile := []byte(`
26+
FROM busybox AS base
27+
RUN echo "$FOO" >/foo
28+
29+
FROM scratch
30+
COPY --from=base /foo /foo
31+
`)
32+
33+
dir := integration.Tmpdir(
34+
t,
35+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
36+
)
37+
destDir := t.TempDir()
38+
39+
c, err := client.New(sb.Context(), sb.Address())
40+
require.NoError(t, err)
41+
defer c.Close()
42+
43+
build := func(attrs map[string]string) string {
44+
_, err = f.Solve(sb.Context(), c, client.SolveOpt{
45+
FrontendAttrs: attrs,
46+
Exports: []client.ExportEntry{
47+
{
48+
Type: client.ExporterLocal,
49+
OutputDir: destDir,
50+
},
51+
},
52+
LocalMounts: map[string]fsutil.FS{
53+
dockerui.DefaultLocalNameDockerfile: dir,
54+
dockerui.DefaultLocalNameContext: dir,
55+
},
56+
}, nil)
57+
require.NoError(t, err)
58+
p := filepath.Join(destDir, "foo")
59+
b, err := os.ReadFile(p)
60+
require.NoError(t, err)
61+
return strings.TrimSpace(string(b))
62+
}
63+
64+
require.Equal(t, "", build(nil))
65+
66+
const hook = `
67+
{
68+
"RUN": {
69+
"entrypoint": ["/dev/.dfhook/bin/busybox", "env", "FOO=BAR"],
70+
"mounts": [
71+
{"from": "busybox:uclibc", "target": "/dev/.dfhook"}
72+
]
73+
}
74+
}`
75+
require.Equal(t, "BAR", build(map[string]string{"hook": hook}))
76+
}

frontend/dockerfile/dockerfile_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ func TestIntegration(t *testing.T) {
257257
"amd64/bullseye-20230109-slim:latest": "docker.io/amd64/debian:bullseye-20230109-slim@sha256:1acb06a0c31fb467eb8327ad361f1091ab265e0bf26d452dea45dcb0c0ea5e75",
258258
}),
259259
)...)
260+
integration.Run(t, instHookTests, opts...)
260261
}
261262

262263
func testDefaultEnvWithArgs(t *testing.T, sb integration.Sandbox) {

frontend/dockerfile/instructions/commands.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

66
"github.com/moby/buildkit/frontend/dockerfile/parser"
7+
"github.com/moby/buildkit/frontend/dockerui/types"
78
dockerspec "github.com/moby/docker-image-spec/specs-go/v1"
89
"github.com/pkg/errors"
910
)
@@ -340,7 +341,7 @@ type ShellDependantCmdLine struct {
340341
// RUN ["echo", "hi"] # echo hi
341342
type RunCommand struct {
342343
withNameAndCode
343-
withExternalData
344+
WithInstructionHook
344345
ShellDependantCmdLine
345346
FlagsUsed []string
346347
}
@@ -551,3 +552,21 @@ func (c *withExternalData) setExternalValue(k, v interface{}) {
551552
}
552553
c.m[k] = v
553554
}
555+
556+
type WithInstructionHook struct {
557+
withExternalData
558+
}
559+
560+
const instHookKey = "dockerfile/run/instruction-hook"
561+
562+
func (c *WithInstructionHook) GetInstructionHook() *types.InstructionHook {
563+
x := c.getExternalValue(instHookKey)
564+
if x == nil {
565+
return nil
566+
}
567+
return x.(*types.InstructionHook)
568+
}
569+
570+
func (c *WithInstructionHook) SetInstructionHook(h *types.InstructionHook) {
571+
c.setExternalValue(instHookKey, h)
572+
}

frontend/dockerfile/instructions/commands_runmount.go

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ func setMountState(cmd *RunCommand, expander SingleWordExpander) error {
8686
return errors.Errorf("no mount state")
8787
}
8888
var mounts []*Mount
89+
if hook := cmd.GetInstructionHook(); hook != nil && hook.Run != nil {
90+
for _, m := range hook.Run.Mounts {
91+
m := m
92+
if err := validateMount(&m, false); err != nil {
93+
return err
94+
}
95+
mounts = append(mounts, &m)
96+
}
97+
}
8998
for _, str := range st.flag.StringValues {
9099
m, err := parseMount(str, expander)
91100
if err != nil {

frontend/dockerfile/instructions/parse.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ import (
1515
"github.com/moby/buildkit/frontend/dockerfile/command"
1616
"github.com/moby/buildkit/frontend/dockerfile/linter"
1717
"github.com/moby/buildkit/frontend/dockerfile/parser"
18+
"github.com/moby/buildkit/frontend/dockerui/types"
1819
"github.com/moby/buildkit/util/suggest"
1920
dockerspec "github.com/moby/docker-image-spec/specs-go/v1"
2021
"github.com/pkg/errors"
2122
)
2223

2324
var excludePatternsEnabled = false
2425

26+
type ParseOpts struct {
27+
InstructionHook *types.InstructionHook
28+
}
29+
2530
type parseRequest struct {
2631
command string
2732
args []string
@@ -31,6 +36,7 @@ type parseRequest struct {
3136
original string
3237
location []parser.Range
3338
comments []string
39+
opts ParseOpts
3440
}
3541

3642
var parseRunPreHooks []func(*RunCommand, parseRequest) error
@@ -66,16 +72,17 @@ func newParseRequestFromNode(node *parser.Node) parseRequest {
6672
}
6773
}
6874

69-
func ParseInstruction(node *parser.Node) (v interface{}, err error) {
70-
return ParseInstructionWithLinter(node, nil)
75+
func ParseInstruction(node *parser.Node, opts ParseOpts) (v interface{}, err error) {
76+
return ParseInstructionWithLinter(node, nil, opts)
7177
}
7278

7379
// ParseInstruction converts an AST to a typed instruction (either a command or a build stage beginning when encountering a `FROM` statement)
74-
func ParseInstructionWithLinter(node *parser.Node, lint *linter.Linter) (v interface{}, err error) {
80+
func ParseInstructionWithLinter(node *parser.Node, lint *linter.Linter, opts ParseOpts) (v interface{}, err error) {
7581
defer func() {
7682
err = parser.WithLocation(err, node.Location())
7783
}()
7884
req := newParseRequestFromNode(node)
85+
req.opts = opts
7986
switch strings.ToLower(node.Value) {
8087
case command.Env:
8188
return parseEnv(req)
@@ -128,8 +135,8 @@ func ParseInstructionWithLinter(node *parser.Node, lint *linter.Linter) (v inter
128135
}
129136

130137
// ParseCommand converts an AST to a typed Command
131-
func ParseCommand(node *parser.Node) (Command, error) {
132-
s, err := ParseInstruction(node)
138+
func ParseCommand(node *parser.Node, opts ParseOpts) (Command, error) {
139+
s, err := ParseInstruction(node, opts)
133140
if err != nil {
134141
return nil, err
135142
}
@@ -164,9 +171,9 @@ func (e *parseError) Unwrap() error {
164171

165172
// Parse a Dockerfile into a collection of buildable stages.
166173
// metaArgs is a collection of ARG instructions that occur before the first FROM.
167-
func Parse(ast *parser.Node, lint *linter.Linter) (stages []Stage, metaArgs []ArgCommand, err error) {
174+
func Parse(ast *parser.Node, lint *linter.Linter, opts ParseOpts) (stages []Stage, metaArgs []ArgCommand, err error) {
168175
for _, n := range ast.Children {
169-
cmd, err := ParseInstructionWithLinter(n, lint)
176+
cmd, err := ParseInstructionWithLinter(n, lint, opts)
170177
if err != nil {
171178
return nil, nil, &parseError{inner: err, node: n}
172179
}
@@ -487,6 +494,7 @@ func parseShellDependentCommand(req parseRequest, emptyAsNil bool) (ShellDependa
487494

488495
func parseRun(req parseRequest) (*RunCommand, error) {
489496
cmd := &RunCommand{}
497+
cmd.SetInstructionHook(req.opts.InstructionHook)
490498

491499
for _, fn := range parseRunPreHooks {
492500
if err := fn(cmd, req); err != nil {

frontend/dockerfile/instructions/parse_heredoc_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestErrorCasesHeredoc(t *testing.T) {
2828
t.Fatalf("Error when parsing Dockerfile: %s", err)
2929
}
3030
n := ast.AST.Children[0]
31-
_, err = ParseInstruction(n)
31+
_, err = ParseInstruction(n, ParseOpts{})
3232
require.Error(t, err)
3333
require.Contains(t, err.Error(), c.expectedError)
3434
}
@@ -166,7 +166,7 @@ EOF`,
166166
require.NoError(t, err)
167167

168168
n := ast.AST.Children[0]
169-
comm, err := ParseInstruction(n)
169+
comm, err := ParseInstruction(n, ParseOpts{})
170170
require.NoError(t, err)
171171

172172
sd := comm.(*CopyCommand).SourcesAndDest
@@ -248,7 +248,7 @@ EOF`,
248248
require.NoError(t, err)
249249

250250
n := ast.AST.Children[0]
251-
comm, err := ParseInstruction(n)
251+
comm, err := ParseInstruction(n, ParseOpts{})
252252
require.NoError(t, err)
253253
require.Equal(t, c.shell, comm.(*RunCommand).PrependShell)
254254
require.Equal(t, c.command, comm.(*RunCommand).CmdLine)

0 commit comments

Comments
 (0)