2

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
  • 1
    Please fix the formatting of your error message so it's readable. Anyway we can't help solve the problem without seeing the makefile that is being invoked, or at least the rule that is failing, and the command line make invoked that generated the error message, not just the errors themselves. – MadScientist Mar 17 '22 at 15:46
  • Hello @MadScientist, Thank you for your reply. Here's my Makefile ` 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 ` On my terminal, I did ' make ' command in the prompt. – Atin-Sarkar Mar 17 '22 at 19:49
  • Hello @MadScientist, I have done done the following in my terminal. $ cd ' ' //moving to the directory $ export DOCKER_BUILDKIT=1 $ make [+] Building 32.8s (5/5) FINISHED => [internal] load build definition from Dockerfile 0.8s => => transferring dockerfile: 1.20kB 0.3s => [internal] load .dockerignore 0.7s => => transferring context: 34B 0.1s => resolve image config for docker.io/docker/dockerfile:1-experimental 21.9s => [auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s => CACHED docker-image://docker.io/docker/dockerfile:1-experimental@sha2 0.0s – Atin-Sarkar Mar 17 '22 at 19:53
  • The following is in continuation to the previous comment: 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 – Atin-Sarkar Mar 17 '22 at 19:58
  • Please edit your question and add the content there with proper formatting. We can't read makefiles, etc. in comments, unformatted. – MadScientist Mar 18 '22 at 13:55
  • Hello @MadScientist, I have added the Makefile and Terminal Commands. Please look into it. – Atin-Sarkar Mar 18 '22 at 14:08

2 Answers2

1

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
BMitch
  • 231,797
  • 42
  • 475
  • 450
0

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 "--"?

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Hello @MadScientist The issue is solved. There were unwanted white spaces in my file. By the way, thanks for your valuable inputs. – Atin-Sarkar Apr 03 '22 at 20:13