Go Cloud Code/Docker on M1 Mac exec格式错误

mrphzbgm  于 2023-06-21  发布在  Docker
关注(0)|答案(2)|浏览(144)

我正在尝试使用GoLand/Cloud Code来构建Golang微服务。但我似乎不能得到Dockerfile钉下来。

FROM --platform=linux/arm64 golang:1.20-alpine  AS build_base
RUN apk add --no-cache git

# Set the Current Working Directory inside the container
WORKDIR /tmp/wave-service

# We want to populate the module cache based on the go.{mod,sum} files.
COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

# Unit tests
# CGO_ENABLED=0 go test -v

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ./out/wave-service ./wave_microservice.go

# Start fresh from a smaller image
FROM --platform=linux/arm64 golang:1.20-alpine
RUN apk add ca-certificates

COPY --from=build_base /tmp/wave-service/out/wave-service /app/wave-service
RUN chmod a+x /app/wave-service

# This container exposes port 8080 to the outside world
EXPOSE 8080

# Run the binary program produced by `go install`
CMD ["/app/wave-service"]

返回错误

Waiting for deployments to stabilize...
 - deployment/wave-service: container wave-service-container is backing off waiting to restart
    - pod/wave-service-565995d854-hwhsm: container wave-service-container is backing off waiting to restart
      > [wave-service-565995d854-hwhsm wave-service-container] exec /app/wave-service: exec format error

我试过使用架构和操作系统变量,但我似乎找不到这里发生了什么。
编辑:
skaffold.yml

apiVersion: skaffold/v4beta5
kind: Config
build:
  artifacts:
    - image: wave-service
      context: wave-service
      platforms: ["linux/amd64"]
      docker:
        dockerfile: Dockerfile

manifests:
  rawYaml:
    - k8s-pod.yaml

k8s-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: wave-service
spec:
  containers:
    - name: wave-service
      image: wave-service
i2byvkas

i2byvkas1#

所以问题不在于docker,而是我的主要go文件。它没有使用main包,所以go没有找到正确的入口点来编译,改变它起作用了!

qojgxg4l

qojgxg4l2#

M1 Mac不允许部署为amd64构建的映像,因为它们与arm64不兼容。
您需要做的是强制trustBuilder使用构建包进行部署。设置trustBuilder=true指示包库信任构建器映像中的生命周期二进制文件,方法是将注册表凭据装载到映像中并允许它执行映像导出。通过这种方式,所有构建包都可以看到凭据。
skaffold.yml文件中添加trustBuilder选项:

build:
  artifacts:
    - image: wave-service
      buildpacks:
        builder: gcr.io/buildpacks/builder:v1
        trustBuilder: true

为了完整起见,我指出the github issue与skaffold最新版本中添加的修复相关,以检查解决该问题的提交代码。

相关问题