[docker] Apple silicon 上用 docker compose 镜像运行 go 生成的程序提示 failed to open elf 的问题

· Docker

图中的 tps-yeastar 是用 go build 出来的可执行程序,但 docker compose 构建之后,却发现其一直是 Restarting(0) 的状态,并且在 docker logs 中错误信息提示 rosetta error

这是原始的 docker-compose.yml 文件:

services:
  yeastar:
    build: 
      context: .
      dockerfile: Dockerfile
    ports:
      - "8260:8260"
    environment:
      - API_ENDPOINT
      - GIN_PORT
    volumes:
      - ./logs:/app/logs  # 持久化日志文件
    restart: unless-stopped

于是,我便用

docker image inspect xxx --format '{{.Architecture}}'

得知,当前镜像的架构是 arm64;而通过 file [your-file] 命令得知,go 可执行程序是 x86-64 架构(即 amd64

已知,苹果 m 系列芯片是 arm64,默认构建的镜像也会是 arm64 架构,而 go 程序并不是由我开发的,所以需要显式指定 platform 为 linux/amd64

此时,运行正常。

发表评论