Home > Enterprise >  bash: No such file or directory when running a dockerfile
bash: No such file or directory when running a dockerfile

Time:08-18

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
COPY --from=go-container /gopath/src/project/main .

FROM scratch as another_container
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!

CodePudding user response:

FROM scratch as another_container

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.

CodePudding user response:

Docker scratch doesn't have binaries such as bash, basically it's an empty context. The FROM 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:

ARG GO_VERSION=1.17
FROM golang:$GO_VERSION as go-container
ADD go.mod go.sum ./
RUN go mod download -x
RUN go build -o main cmd/main.go
FROM busybox as another_container
COPY --from=go-container ./main /main
ENTRYPOINT ["./main"]
  • Related