skip to Main Content

I have been trying to build a golang docker image for my application, but I can’t get any of the images to build:

I have tried theese two Dockerfiles:

FROM golang:1.20

WORKDIR /app

COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY . ./

RUN go build -o ./app

EXPOSE 8080

CMD [ "./app" ]

and

FROM golang:1.20-alpine as builder

COPY . /app

WORKDIR /app

RUN CGO_ENABLED=0 go build -o out

FROM alpine:latest

# Copy the compiled binary from the builder image
COPY --from=builder /app/out ./out

# Start the program
CMD ["./out"]

The Error (different binary name, but the same error):

Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "./out": permission denied: unknown

I have tried setting the exec bit on the outputted files, but it still won’t work. I have been able to use the exact same dockerfiles before, but now they seem to not want to work.

Any help is appreciated, thanks

I have tried chowning the build binary, but it didnt work.
I have tried different dockerifles, but it didnt work.

Worked fine 3 days ago, doesn’t want to work now.
Also tried docker system prune –all, no luck
I have looked for other SO answers, nothing worked.

2

Answers


  1. Chosen as BEST ANSWER

    This was caused by me building the root folder of the project inside the container, not the /cmd folder, where the main file was located. Make sure to chmod +x the outputted file in the container, and make sure to build the correct files.

    /facepalm


  2. I used the provided examples to write two different ways of building Docker images.

    FROM golang:1.20-alpine as builder
    WORKDIR /app
    ENV GOPROXY=https://goproxy.cn
    COPY ./go.mod ./
    RUN go mod download
    COPY . .
    RUN CGO_ENABLED=0 go build -o server
    EXPOSE 8080
    CMD ["/app/server"]
    

    AND

    FROM golang:1.20-alpine as builder
    WORKDIR /app
    ENV GOPROXY=https://goproxy.cn
    COPY ./go.mod ./
    RUN go mod download
    COPY . .
    RUN CGO_ENABLED=0 go build -o server
    
    FROM scratch
    COPY --from=builder /app/server /opt/app/
    EXPOSE 8080
    CMD ["/opt/app/server"]
    

    You can also check out my github.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search