You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

M1 Mac上Go Cloud Code/Docker遇exec format error问题求助

问题描述

尝试用GoLand/Cloud Code构建Golang微服务,Dockerfile配置后运行出现exec /app/wave-service: exec format error错误,容器反复重启。

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 

问题原因与修复方案

核心矛盾:架构不匹配

Dockerfile中指定以linux/arm64架构构建二进制文件,但skaffold.yml强制设置构建平台为linux/amd64,导致最终镜像的架构与二进制程序的架构不兼容,触发exec format error。

修复步骤

  1. 统一架构配置,根据你的K8s集群架构二选一:

    • 若集群为amd64架构:
      • 修改Dockerfile,将两处--platform=linux/arm64改为linux/amd64,同时把GOARCH=arm64调整为GOARCH=amd64;
      • skaffold.yml中的platforms: ["linux/amd64"]可保留,保持配置一致。
    • 若集群为arm64架构:
      • 修改skaffold.yml,将platforms: ["linux/amd64"]改为platforms: ["linux/arm64"],与Dockerfile的架构配置对齐。
  2. 优化镜像(可选):使用更轻量的运行时镜像替代golang基础镜像,减少镜像体积:

    FROM --platform=linux/amd64 alpine:3.18
    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
    EXPOSE 8080
    CMD ["/app/wave-service"]
    
  3. 清理缓存重新构建:执行skaffold delete清除旧资源,再通过skaffold run重新构建部署,避免缓存残留影响结果。

内容的提问来源于stack exchange,提问作者Cate Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 09:35:26