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

fix dockerfile local dir #1192

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
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
20 changes: 13 additions & 7 deletions cmd/nerdctl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg
"--progress=" + progressValue,
"--frontend=dockerfile.v0",
"--local=context=" + buildContext,
"--local=dockerfile=" + buildContext,
"--output=" + output,
}...)

Expand All @@ -336,28 +335,35 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg
return "", nil, false, "", nil, nil, err
}

var dir, file string

dir := buildContext
file := buildkitutil.DefaultDockerfileName
if filename != "" {
if filename == "-" {
var err error
dir, err = buildkitutil.WriteTempDockerfile(cmd.InOrStdin())
if err != nil {
return "", nil, false, "", nil, nil, err
}
file = buildkitutil.DefaultDockerfileName
cleanup = func() {
os.RemoveAll(dir)
}
} else {
dir, file = filepath.Split(filename)
}

if dir != "" {
buildctlArgs = append(buildctlArgs, "--local=dockerfile="+dir)
if dir == "" {
dir = "."
}
buildctlArgs = append(buildctlArgs, "--opt=filename="+file)
}
absDir, err := filepath.Abs(dir)
if err != nil {
return "", nil, false, "", nil, nil, err
}
if _, err := os.Lstat(filepath.Join(absDir, file)); err != nil {
return "", nil, false, "", nil, nil, err
}
buildctlArgs = append(buildctlArgs, "--local=dockerfile="+dir)
buildctlArgs = append(buildctlArgs, "--opt=filename="+file)

target, err := cmd.Flags().GetString("target")
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions cmd/nerdctl/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,38 @@ CMD ["echo", "nerdctl-build-test-stdin"]
base.Cmd("build", "-t", imageName, "-f", "-", ".").CmdOption(testutil.WithStdin(strings.NewReader(dockerfile))).AssertCombinedOutContains(imageName)
}

func TestBuildWithDockerfile(t *testing.T) {
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
defer base.Cmd("builder", "prune").Run()
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: Why do we need builder prune? Won't these slow our tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

cc @ktock

Copy link
Member

Choose a reason for hiding this comment

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

@manugupt1 @junnplus This was to ensure all content (possibly leased by BuildKit) in the content store gets pruned after each test. Tests may check the behaviour of pulling blobs and contents remaining in the content store unexpectedly skip these operations.But yes, we may improve this by removing defer base.Cmd("builder", "prune").Run() and instead adding rmiAll() (defined in image_encrypt_linux_test.go) to tests (if exist) requiring clean content store. Thanks for pointing this out :)

imageName := testutil.Identifier(t)
defer base.Cmd("rmi", imageName).Run()

dockerfile := fmt.Sprintf(`FROM %s
CMD ["echo", "nerdctl-build-test-dockerfile"]
`, testutil.CommonImage)

buildCtx := filepath.Join(t.TempDir(), "test")
err := os.MkdirAll(buildCtx, 0755)
assert.NilError(t, err)
err = os.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0644)
assert.NilError(t, err)

pwd, err := os.Getwd()
assert.NilError(t, err)
err = os.Chdir(buildCtx)
assert.NilError(t, err)
defer os.Chdir(pwd)

// hack os.Getwd return "(unreachable)" on rootless
Copy link
Member

Choose a reason for hiding this comment

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

How does this happen?

Can't find "unreachable" error in the code
https://cs.opensource.google/go/go/+/refs/tags/go1.18.3:src/os/getwd.go;l=22

Copy link
Member Author

@junnplus junnplus Jul 6, 2022

Choose a reason for hiding this comment

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

I actually don't know what happened, Getwd returns a strange path during rootless testing.
(unreachable)/tmp/TestBuildWithDockerfile2096791215/001/text/Dockerfile

https://github.com/containerd/nerdctl/runs/7196804919?check_suite_focus=true#step:5:1004

t.Setenv("PWD", buildCtx)

base.Cmd("build", "-t", imageName, "-f", "Dockerfile", "..").AssertOK()
base.Cmd("build", "-t", imageName, "-f", "Dockerfile", ".").AssertOK()
// fail err: no such file or directory
base.Cmd("build", "-t", imageName, "-f", "../Dockerfile", ".").AssertFail()
}

func TestBuildLocal(t *testing.T) {
t.Parallel()
testutil.DockerIncompatible(t)
Expand Down