From dc9eec86e1d8bf20fa93fc581cc7b6341c1ced46 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 2 May 2022 16:19:04 -0300 Subject: [PATCH 1/3] feat: improve docker errors Adding an error page with some more info to help debug issues. Signed-off-by: Carlos A Becker --- internal/pipe/docker/docker.go | 4 ++- www/docs/errors/docker-build.md | 60 +++++++++++++++++++++++++++++++++ www/mkdocs.yml | 1 + 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 www/docs/errors/docker-build.md diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go index 1c2735a38db..015f621cb28 100644 --- a/internal/pipe/docker/docker.go +++ b/internal/pipe/docker/docker.go @@ -127,7 +127,9 @@ func (Pipe) Run(ctx *context.Context) error { return process(ctx, docker, artifacts.List()) }) } - return g.Wait() + if err := g.Wait(); err != nil { + return fmt.Errorf("docker build failed: %w\nLearn more at https://goreleaser.com/errors/docker-build\n") + } } func process(ctx *context.Context, docker config.Docker, artifacts []*artifact.Artifact) error { diff --git a/www/docs/errors/docker-build.md b/www/docs/errors/docker-build.md new file mode 100644 index 00000000000..99d87f8e0c2 --- /dev/null +++ b/www/docs/errors/docker-build.md @@ -0,0 +1,60 @@ +# Docker build failures + +## `COPY failed: file not found in build context` + +This usually happens when trying to build the binary again from source code in +the Docker image build process. + +The way GoReleaser works, the correct binary for the platform you're building +should be already available, so you don't need to build it again and can still +reuse the `Dockefile`. + +Another common misconception is trying to copy the binary as if the context is +the repository root. +It's not. +It's always a new temporary build context with the artifacts you can use in +its root, so you can just `COPY binaryname /bin/binaryname` and etc. + +Bellow you can find some **don'ts** as well as what you should **do**. + +### Don't + +Build the binary again. + +```dockerfile +FROM golang AS builder +WORKDIR /app +COPY cmd ./cmd +COPY go.mod ./ +COPY *.go ./ +RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o app . + +FROM scratch +COPY --from=builder /app/app /app +ENTRYPOINT ["/app"] +``` + +### Don't + +Copy from the `dist` folder. + +```dockerfile +FROM scratch +COPY /dist/app_linux_amd64/app /app +ENTRYPOINT ["/app"] +``` + +### Do + +Copy the clean file names from the root. + +```dockerfile +FROM scratch +COPY app /app +ENTRYPOINT ["/app"] +``` + +!!! tip + If you still want your users to be able to `docker build` without an extra + step, you can have a `Dockerfile` just for GoReleaser, for example, a + `goreleaser.dockefile`. diff --git a/www/mkdocs.yml b/www/mkdocs.yml index cae13907a8e..30afe55754a 100644 --- a/www/mkdocs.yml +++ b/www/mkdocs.yml @@ -134,6 +134,7 @@ nav: - Common errors: - errors/dirty.md - errors/multiple-tokens.md + - errors/docker-build.md - errors/no-main.md - errors/resource-not-accessible-by-integration.md - errors/no-history.md From 21c5fac6971a4f8941fe40b0043ca11bf368205c Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 2 May 2022 16:37:50 -0300 Subject: [PATCH 2/3] fix: forgot return Signed-off-by: Carlos A Becker --- internal/pipe/docker/docker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go index 015f621cb28..1aa8738f7e9 100644 --- a/internal/pipe/docker/docker.go +++ b/internal/pipe/docker/docker.go @@ -130,6 +130,7 @@ func (Pipe) Run(ctx *context.Context) error { if err := g.Wait(); err != nil { return fmt.Errorf("docker build failed: %w\nLearn more at https://goreleaser.com/errors/docker-build\n") } + return nil } func process(ctx *context.Context, docker config.Docker, artifacts []*artifact.Artifact) error { From 69338bf9de2f95e42e945f45eedc39771c92155b Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 2 May 2022 20:39:38 -0300 Subject: [PATCH 3/3] fix: missing err Signed-off-by: Carlos A Becker --- internal/pipe/docker/docker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go index 1aa8738f7e9..22875e0e7fe 100644 --- a/internal/pipe/docker/docker.go +++ b/internal/pipe/docker/docker.go @@ -128,7 +128,7 @@ func (Pipe) Run(ctx *context.Context) error { }) } if err := g.Wait(); err != nil { - return fmt.Errorf("docker build failed: %w\nLearn more at https://goreleaser.com/errors/docker-build\n") + return fmt.Errorf("docker build failed: %w\nLearn more at https://goreleaser.com/errors/docker-build\n", err) // nolint:revive } return nil }