I am trying to containerize Go Developer environment. Blogs I am referring: https://www.docker.com/blog/containerize-your-go-developer-environment-part-1/
I am getting an error:
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = invalid field '' must be a key=value pair Makefile:8: recipe for target 'bin/example' failed make: *** [bin/example] Error 1
My Dockerfile:
# syntax = docker/dockerfile:1-experimental
FROM --platform=${BUILDPLATFORM} golang:1.17.8-alpine AS base
WORKDIR /src
ENV CGO_ENABLED=0
COPY go.* .
RUN --mount=type=cache, target=/go/pkg/mod go mod download
FROM base AS build
ARG TARGETOS
ARG TARGETARCH
RUN --mount=target=. --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/example .
FROM base AS unit-test
RUN --mount=target=. --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build go test -v .
FROM golangci/golangci-lint:v1.43-alpine AS lint-base
FROM base AS lint
RUN --mount=target=. --mount=from=lint-base, src=/usr/bin/golangci-lint, target=/usr/bin/golangci-lint --mount=type=cache, target=/go/pkg/mod --mount=type=cache, target=/root/.cache/go-build --mount=type=cache, target=/root/.cache/golangci-lint golangci-lint run --timeout 10m0s ./...
FROM scratch AS bin-unix
COPY --from=build /out/example /
FROM bin-unix AS bin-linux
FROM bin-unix AS bin-darwin
FROM scratch AS bin-windows
COPY --from=build /out/example /example.exe
FROM bin-${TARGETOS} AS bin
Makefile is as follows:
all: bin/example
test: lint unit-test
PLATFORM=linux/amd64
.PHONY: bin/example
bin/example:
@docker build . --target bin --output bin/ --platform ${PLATFORM}
.PHONY: unit-test
unit-test:
@docker build . --target unit-test
.PHONY: lint
lint:
@docker build . --target lint
In terminal:
atin@atin-VirtualBox:~/Desktop/container-go-dev$ export DOCKER_BUILDKIT=1
atin@atin-VirtualBox:~/Desktop/container-go-dev$ make
[ ] Building 23.2s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 1.6s
=> => transferring dockerfile: 38B 0.4s
=> [internal] load .dockerignore 1.3s
=> => transferring context: 34B 0.3s
=> resolve image config for docker.io/docker/dockerfile:1-experimental 10.3s
=> [auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED docker-image://docker.io/docker/dockerfile:1-experimental@sha2 0.0s
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = failed to create LLB definition: dockerfile parse error line 7: invalid field '' must be a key=value pair
Makefile:8: recipe for target 'bin/example' failed
make: *** [bin/example] Error 1
CodePudding user response:
This is line #7 of your docker file:
RUN --mount=type=cache, target=/go/pkg/mod go mod download
I'm certainly not an expert at dockerfiles but that syntax does not look right to me. I have no idea what program in your image the RUN command is invoking, but I question whether there should be a comma here, and should there be an equals sign between "mount" and "type", or should it be a dash? And, does it even work to pass an option here with "--"?
CodePudding user response:
You have a lot of invalid whitespace throughout your file, including the syntax line up top (likely resulting in it being ignored) and in the --mount
options:
RUN --mount=type=cache, target=/go/pkg/mod go mod download
Should be
RUN --mount=type=cache,target=/go/pkg/mod go mod download