Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .dockerignore files #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 54 additions & 9 deletions cmd/buildkit-nix/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb"
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
"github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/frontend/gateway/grpcclient"
"github.com/moby/buildkit/util/appcontext"
Expand All @@ -31,9 +32,10 @@ func newFrontendCmd() *cobra.Command {

// mimic dockerfile.v1 frontend
const (
localNameContext = "context"
localNameDockerfile = "dockerfile"
keyFilename = "filename"
localNameContext = "context"
localNameDockerfile = "dockerfile"
keyFilename = "filename"
dockerignoreFilename = ".dockerignore"
)

func frontendAction(cmd *cobra.Command, args []string) error {
Expand All @@ -55,6 +57,7 @@ func frontendBuild(nixImage string) client.BuildFunc {
localDfSt := llb.Local(localNameDockerfile,
llb.SessionID(c.BuildOpts().SessionID),
dockerfile2llb.WithInternalName("local dockerfile"),
llb.FollowPaths([]string{dfName}),
Copy link
Author

@tristanpemble tristanpemble Feb 22, 2022

Choose a reason for hiding this comment

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

this is actually going to get complicated.. relative Nix paths in the Dockerfile won't work anymore since this is being copied to /dockerfile/{dfName} and the context is in /context

)

// Inject the self binary into the ExecOp.
Expand All @@ -67,14 +70,13 @@ func frontendBuild(nixImage string) client.BuildFunc {
if err != nil {
return nil, err
}

localCtxSt := llb.Local(localNameContext,
llb.SessionID(c.BuildOpts().SessionID),
dockerfile2llb.WithInternalName("local context"),
)
localCtxSt, err := getContextSt(ctx, c)
if err != nil {
return nil, err
}

runSt := nixImageSt.Run(
llb.AddMount("/context", localCtxSt),
llb.AddMount("/context", *localCtxSt),
llb.AddMount("/dockerfile", localDfSt),
llb.AddMount("/self", *selfImageSt),
llb.AddMount("/out", llb.Scratch()),
Expand Down Expand Up @@ -209,3 +211,46 @@ func validateSelfImageSt(ctx context.Context, c client.Client, selfImageSt llb.S
}
return selfPath, nil
}

func getContextSt(ctx context.Context, c client.Client) (*llb.State, error) {
st := llb.Local(localNameContext,
llb.SessionID(c.BuildOpts().SessionID),
llb.FollowPaths([]string{dockerignoreFilename}),
dockerfile2llb.WithInternalName("load "+dockerignoreFilename),
llb.Differ(llb.DiffNone, false),
)
def, err := st.Marshal(ctx)
if err != nil {
return nil, err
}
res, err := c.Solve(ctx, client.SolveRequest{
Evaluate: true,
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}
ref, err := res.SingleRef()
if err != nil {
return nil, err
}
dt, _ := ref.ReadFile(ctx, client.ReadRequest{
Filename: dockerignoreFilename,
}) // error ignored

var excludes []string
if len(dt) != 0 {
excludes, err = dockerignore.ReadAll(bytes.NewBuffer(dt))
if err != nil {
return nil, err
}
}

st = llb.Local(localNameContext,
dockerfile2llb.WithInternalName("load build context"),
llb.SessionID(c.BuildOpts().SessionID),
llb.ExcludePatterns(excludes),
)

return &st, nil
}