1
votes

I am trying to containerize and as well as start my Go lang application using Docker-compose, The image is built successfully according to the logs but my container does not for docker-compose up and it throws the following error to my console.

Cannot start service app: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"go\": executable file not found in $PATH": unknown

Here is what my Docker file looks like.

ARG GO_VERSION=1.13

FROM golang:${GO_VERSION}-alpine AS builder

# We create an /app directory within our
# image that will hold our application source
# files
RUN mkdir /raedar

# Create the user and group files that will be used in the running container to
# run the process as an unprivileged user.
RUN mkdir /user && \
    echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
    echo 'nobody:x:65534:' > /user/group

# Install git.
# Git is required for fetching the dependencies.
# Allow Go to retrieve the dependencies for the buld
RUN apk update && apk add --no-cache ca-certificates git

RUN apk add --no-cache libc6-compat

# Force the go compiler to use modules 
ENV GO111MODULE=on

ADD . /raedar/


WORKDIR /raedar/

RUN go get -d -v golang.org/x/net/html

COPY go.mod go.sum ./

COPY . .

# Compile the binary, we don't want to run the cgo
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/main cmd/app/main.go


# Final stage: the running container.
FROM scratch AS final

WORKDIR /root/

# Import the user and group files from the first stage.
COPY --from=builder /user/group /user/passwd /etc/

# Import the Certificate-Authority certificates for enabling HTTPS.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

# Import the compiled executable from the first stage.
COPY --from=builder /raedar/bin/main .

EXPOSE 8080
# Perform any further action as an unprivileged user.
USER nobody:nobody

# Run the compiled binary.
CMD ["./main"]
1
Does FROM have to be the first line of the Dockerfile? (ie, remove ARG and make the GO_VERSION explicit). Update: ARG is allowed, ignore this comment. - Mark
@Mark - quoting from the document you linked "FROM may only be preceded by one or more ARG instructions, which declare arguments that are used in FROM lines in the Dockerfile." so I dont think that is the issue in this case (would have expected the build to fail if it was). - Brits
Yeah you right @Brits, I mean even when I make it GO_VERSION explicit, I still get the same error. - Fahd Jamy
I think we would need to see the docker-compose file to answer this. Alternatively try running it with docker run -it NAME and see what happens (if it works, or at least starts the executable, with docker run then the issue is with your compose file). - Brits

1 Answers

0
votes

The error shows you're trying to run go, not ./main:

exec: \"go\": executable file not found in $PATH

A matching Dockerfile would have the line CMD ["go", "./main"] rather than CMD ["./main"].

So either you're unexpectedly building a different Dockerfile, or you're changing the command when you run a container with that image. In particular, if you're using docker-compose, make sure you're not setting command: go ./main or entrypoint: go, either of which could cause this behavior.