如何通过 Github Actions runner 上的 CDK 构建 ARM64 Docker 映像

alu*_*ach 6 arm docker aws-fargate aws-cdk github-actions

我正在尝试在 Github Actions 工作流程中部署我的 Fargate 服务(使用 ARM64 Graviton 处理器)。但是,当我的 Docker 尝试构建映像时,RUN语句失败:

Warning: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
 ---> Running in 025f2d97f759
exec /bin/sh: exec format error
Removing intermediate container f2c3858b0496
The command '/bin/sh -c apt-get update' returned a non-zero code: 1
 ---> 02ceb19d6208
[66%] fail: docker build --tag cdkasset-7e1b96b9aea331ad2efd7e6fbf6f075033eca830469ea4ab8d57bf4a8eeb86cd --file Dockerfile --platform linux/arm64 . exited with error code 1: The command '/bin/sh -c apt-get update' returned a non-zero code: 1
Run Code Online (Sandbox Code Playgroud)

是否可以在 AMD64 Github Actions 运行器上构建基于 ARM64 的 Docker 映像?我尝试在每个可能的地方指定平台架构,但构建仍然失败。

我的 Github Actions 工作流程:

Warning: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
 ---> Running in 025f2d97f759
exec /bin/sh: exec format error
Removing intermediate container f2c3858b0496
The command '/bin/sh -c apt-get update' returned a non-zero code: 1
 ---> 02ceb19d6208
[66%] fail: docker build --tag cdkasset-7e1b96b9aea331ad2efd7e6fbf6f075033eca830469ea4ab8d57bf4a8eeb86cd --file Dockerfile --platform linux/arm64 . exited with error code 1: The command '/bin/sh -c apt-get update' returned a non-zero code: 1
Run Code Online (Sandbox Code Playgroud)

我的Dockerfile

# We create our own image rather than using cloudflare/cloudfared because that is an ultra 
# slimmed-down image that does not have a shell. We need the shell to be present to call
# the image with a command like `["sh", "-c", "cloudflared tunnel run --token $TOKEN"] wherin
# we can use available environment variables which represent passed-in values from AWS Secrets
# Manager.
FROM --platform=linux/arm64 debian:stretch-slim

RUN apt-get update
RUN apt-get dist-upgrade --yes
RUN apt-get install curl --yes

# Install cloudflared
RUN curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64.deb
RUN dpkg -i cloudflared.deb
RUN rm cloudflared.deb
Run Code Online (Sandbox Code Playgroud)

我的 CDK 构造中我构建映像的部分:

import {
  aws_ecr_assets,
  aws_ecs,
} from "aws-cdk-lib";
import { Construct } from "constructs";

export class Tunnel extends Construct {
  constructor(scope: Construct, id: string, props: TunnelProps) {
    super(scope, id);

    const taskDefinition = new aws_ecs.FargateTaskDefinition(
      this,
      "TunnelDefinition",
      {
        runtimePlatform: {
          cpuArchitecture: aws_ecs.CpuArchitecture.ARM64,
        },
      }
    );

    taskDefinition.addContainer("container", {
      image: aws_ecs.AssetImage.fromAsset("./", {
        platform: aws_ecr_assets.Platform.LINUX_ARM64,
      }),
    });

    new aws_ecs.FargateService(this, "Tunnel", {
      serviceName: "cloudflare-tunnel",
      cluster: props.cluster,
      taskDefinition,
    });
  }
}

export interface TunnelProps {
  cluster: aws_ecs.ICluster;
  loadBalancerDnsName: string;
}
Run Code Online (Sandbox Code Playgroud)

谢谢。

alu*_*ach 5

将其添加docker/setup-qemu-action到 GitHub Actions 工作流程中就可以了:

name: Deploy

on:
  push:
    branches:
      - main

permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: "npm"

      - run: npm ci

      - uses: docker/setup-qemu-action@v2
        with:
          platforms: 'arm64,arm'

      - uses: docker/setup-buildx-action@v2

      - uses: aws-actions/configure-aws-credentials@v2
        with:
          role-to-assume: arn:aws:iam::123456789098:role/cicd-role
          aws-region: us-west-2

      - run: npx cdk deploy --all --require-approval never
        env:
          DOCKER_BUILDKIT: 1
Run Code Online (Sandbox Code Playgroud)

另外值得注意的是,运行CDK时需要对DOCKER_BUILDKIT: 1环境进行设置。