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

bake: local dockerfile support for remote definition #2015

Merged
merged 2 commits into from
Oct 19, 2023

Conversation

crazy-max
Copy link
Member

fixes #1627

Allow to use a local dockerfile with a remote bake definition like we do for context using cwd:// protocol.

Can be tested with https://github.com/crazy-max/buildx-buildkit-tests/tree/main/buildx-1627:

$ cd /tmp
$ cat <<EOT >> Dockerfile.app
FROM scratch
COPY foo /foo
EOT
$ docker buildx bake https://github.com/crazy-max/buildx-buildkit-tests.git#:buildx-1627

return nil, err
}
var err error
bi.DockerfilePath, err = filepath.Abs(bi.DockerfilePath)
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to return the absolute representation of the dockerfile path after checking its a valid one like we do for context so we can set the right dockerfile dir if context is a remote URL during build.

I was also thinking reading the file in bi.DockerfileInline and then set bi.DockerfilePath = "-" but that's hacky and would be confusing.

@crazy-max crazy-max marked this pull request as ready for review August 23, 2023 15:47
tests/bake.go Show resolved Hide resolved
@jedevc
Copy link
Collaborator

jedevc commented Aug 23, 2023

Should we add a test to make sure that #1880 (comment) still applies? So in the test, if Dockerfile.app appears without the cwd:// prefix, we should error.

Either - that case should error like in v0.11 (which reverted #1880), or it should pull the docker file from the remote.

@crazy-max
Copy link
Member Author

Should we add a test to make sure that #1880 (comment) still applies? So in the test, if Dockerfile.app appears without the cwd:// prefix, we should error.

Either - that case should error like in v0.11 (which reverted #1880), or it should pull the docker file from the remote.

Yes so for a remote bake definition like:

target "default" {
  context = BAKE_CMD_CONTEXT
  dockerfile = "Dockerfile.app"
}

Invoked with:

docker buildx bake https://github.com/foo/bar.git

We should use local context but we don't support reading remote Dockerfile as expected:

#0 building with "default" instance using docker driver

#1 [internal] load git source https://github.com/foo/bar.git
#1 0.480 ref: refs/heads/main   HEAD
#1 0.480 dea0f6334d719a494a384a13812f604b6b79fed3       HEAD
#1 0.916 dea0f6334d719a494a384a13812f604b6b79fed3       refs/heads/main
#1 CACHED

#2 [internal] load .dockerignore
#2 transferring context:
#2 transferring context: 2B done
#2 DONE 0.0s

#3 [internal] load build definition from Dockerfile.app
#3 transferring dockerfile: 2B done
#3 DONE 0.0s
ERROR: failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount3004544897/Dockerfile.app: no such file or directory

bake/bake.go Outdated
Comment on lines 1063 to 1090
} else if !build.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://") && (inp != nil && build.IsRemoteURL(inp.URL)) {
if _, err := os.Stat(filepath.Join(path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")), bi.DockerfilePath)); err == nil {
return nil, errors.Errorf("reading a local dockerfile for a remote build invocation is not allowed when using a local context")
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Deny access to a local dockerfile for remote invocation with a local context. We need to check if a local Dockerfile exists early at the moment but in follow-up we should support reading a remote Dockerfile. Probably having a DockerfileState input like ContextState one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just want to check - isn't this independent of what the Context is set to? Isn't it instead dependent on whether the bake file is coming from a remote?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's dependent on remote bake but also if context is not local

tests/bake.go Outdated Show resolved Hide resolved
@crazy-max crazy-max added this to the v0.12.0 milestone Sep 12, 2023
@@ -1038,6 +1037,9 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
if t.Dockerfile != nil {
dockerfilePath = *t.Dockerfile
}
if !strings.HasPrefix(dockerfilePath, "cwd://") {
Copy link
Member

Choose a reason for hiding this comment

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

follow-up: this should work with remote URLs as well

Copy link
Member Author

Choose a reason for hiding this comment

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

you mean similar to what has been said in #2015 (comment) so we support reading a remote Dockerfile with a local context?

if err != nil {
return nil, err
}
} else if !build.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://") && (inp != nil && build.IsRemoteURL(inp.URL)) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I understand this condition:

!build.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://")

If Dockerfile is not remote and context is based on local dir then why would that be invalid config?

Maybe a comment would help.

}
} else if !build.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://") && (inp != nil && build.IsRemoteURL(inp.URL)) {
if _, err := os.Stat(filepath.Join(path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")), bi.DockerfilePath)); err == nil {
return nil, errors.Errorf("reading a dockerfile for a remote build invocation is currently not supported")
Copy link
Member

Choose a reason for hiding this comment

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

Is this for security?

Copy link
Member Author

@crazy-max crazy-max Oct 11, 2023

Choose a reason for hiding this comment

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

No, just not supported atm, see #2015 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a comment to better explain the issue.

@crazy-max crazy-max force-pushed the fix-bake-cwd-dockerfile branch 2 times, most recently from 91e3978 to 5b65cfb Compare October 19, 2023 11:30
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
…l context

we don't currently support reading a remote Dockerfile with a local
context when doing a remote invocation because we automatically derive
the dockerfile from the context atm. To avoid mistakenly reading a local
Dockerfile, we check if the Dockerfile exists locally and if so, we
error out.

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
@tonistiigi tonistiigi merged commit 31d021a into docker:master Oct 19, 2023
58 of 59 checks passed
@crazy-max crazy-max deleted the fix-bake-cwd-dockerfile branch October 19, 2023 22:46
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.

[BUG] Bake build fails when using BAKE_CMD_CONTEXT locally
3 participants