Home > Software engineering >  Multistage build exec go file not found in scratch
Multistage build exec go file not found in scratch

Time:06-27

I'm new to Docker and I have some noob questions. I've followed this tutorial to improve Docker files. The single build works fine, but the multistage fails when I use scratch image.

The Docker file is:

# syntax=docker/dockerfile:1

##
## STEP 1 - BUILD
##

# specify the base image to  be used for the application, alpine or ubuntu
FROM golang:1.18-alpine AS build

# None root user
# RUN addgroup -S qcheckgroup && adduser -S qcheck -G qcheckgroup

# USER qcheck

# create a working directory inside the image
ENV GO111MODULE=on
WORKDIR /app

# copy Go modules and dependencies to image
COPY . .

# download Go modules and dependencies
# RUN go mod download

# compile application
RUN go build .


##
## STEP 2 - DEPLOY
##
FROM scratch

WORKDIR /

FROM scratch

WORKDIR /

COPY --from=build app/query_check_span /

EXPOSE 8080

ENTRYPOINT [ "/query_check_span" ]

The build process works fine docker build -t qcheck:multi -f .\Dockerfile.multi . but when I try to run the container with docker run qcheck:multi I get the following error: exec /query_check_span: no such file or directory.

It looks that the problem is that scratch image is unable to execute the compiled go program. I confirm that when I've change the image to apline for example.

Do you have an idea of what I am doing wrong? How could I run the container using scratch image?

CodePudding user response:

In your build stage, you should RUN go build -o ./query_check_span instead of RUN go build .. That command will output the binary file which has the name query_check_span.

  • Related