I’m trying to run a container from a dockerfile
and a docker-compose
file which look like this –
docker-compose.yml
build:
context: .
dockerfile: docker/Dockerfile
target: go-container
entrypoint: bash -c "./main"
the Dockerfile
looks like this –
ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ENV GOPATH /gopath
ENV PATH $GOPATH/bin:$PATH
WORKDIR /gopath/src/project
ADD go.mod go.sum ./
RUN go mod download -x
RUN go build -o main cmd/main.go
FROM scratch as another_container
COPY --from=go-container /gopath/src/project/main .
ENTRYPOINT ["./main"]
On running the docker-compose
, I get an error like this –
bash: line 1: ./main: No such file or directory
ERROR: 127
What is happening here? I don’t know how to debug it so any help is appreciated!
2
Answers
You’re using a FROM statement, which replaces the current container with a new container based on an image. In this case, you’re doing it
FROM scratch
, which is a container with nothing in it.Since you didn’t use
COPY --from
to copy the compiled file from the previous stage, the file./main
is missing, so your application won’t work.See here for a multistage Go build example.
Docker
scratch
doesn’t have binaries such asbash
, basically it’s an empty context. TheFROM scratch as another_container
instruction then removes previous layers; in this case./main
file is excluded.You could use another minimal image, ie
FROM busybox as another_container
, to execute the./main
binary.ie:
Full example:
docker-compose.yaml
Dockerfile
main.go
Execute with:
docker compose up