Hassen
Hassen

Reputation: 7664

Golang based docker image build works but not scratch based image

I'm able to run a docker image of a web app when using golang:1.13 base, but not when using scratch. The working Dockerfile is:

FROM golang:1.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o server

FROM golang:1.13
COPY --from=builder /app/server /app/server
COPY --from=builder /app/credentials/service-account.json /app/credentials/service-account.json
ENTRYPOINT ["/app/server"]

But when I change the final image base to scratch (line 6) like this:

FROM golang:1.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o server

FROM scratch # <-- CHANGED
COPY --from=builder /app/server /app/server
COPY --from=builder /app/credentials/service-account.json /app/credentials/service-account.json
ENTRYPOINT ["/app/server"]

I get a standard_init_linux.go:211: exec user process caused "no such file or directory" error.

To build the docker image, I use docker build -t myimage ., and to run the image, I use docker run --rm -p 8080:8080 myimage:latest.

The app is a Go based web API that uses Gin framework, and GCP Service Account to access GCP services (the JSON file that I copy on build.)

Upvotes: 4

Views: 1531

Answers (1)

colm.anseo
colm.anseo

Reputation: 22147

Provided you are not using CGO (as @jakub mentioned), try disabling CGO in your build.

So change this line in your Dockerfile:

#RUN go build -o server
RUN CGO_ENABLED=0 go build -o server

Upvotes: 5

Related Questions