无法成功构建任何 Golang Docker 镜像,权限被拒绝。

huangapple go评论85阅读模式
英文:

Can't build any golang docker images successfully, permission denied

问题

我一直在尝试为我的应用程序构建一个 Golang Docker 镜像,但是我无法构建任何一个镜像:

我尝试了这两个 Dockerfile:

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"]

FROM golang:1.20-alpine as builder

COPY . /app

WORKDIR /app

RUN CGO_ENABLED=0 go build -o out

FROM alpine:latest

# 从构建镜像中复制编译好的二进制文件
COPY --from=builder /app/out ./out

# 启动程序
CMD ["./out"]

错误信息(二进制文件名不同,但是错误相同):

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

我尝试设置输出文件的执行权限位,但仍然无法工作。我以前能够使用完全相同的 Dockerfile,但现在它们似乎不想工作了。

非常感谢任何帮助!我尝试过更改构建二进制文件的所有者,但没有成功。我尝试了不同的 Dockerfile,但也没有成功。

3天前还能正常工作,现在却不行了。我还尝试了 docker system prune --all,但没有成功。我查找了其他的 Stack Overflow 回答,但没有起作用。

英文:

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.

答案1

得分: 1

这是由于我将项目的根文件夹构建在容器内,而不是位于主文件所在的/cmd文件夹中引起的。请确保在容器中对输出的文件进行chmod +x操作,并确保构建正确的文件。

/捂脸

英文:

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

得分: 0

我使用提供的示例编写了两种不同的构建 Docker 镜像的方法。

第一种方法的 Dockerfile 如下:

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"]

第二种方法的 Dockerfile 如下:

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"]

你也可以查看我的 GitHub

英文:

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.

huangapple
  • 本文由 发表于 2023年4月24日 08:31:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76087913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定