5

I am attempting to deploy my Fargate service (with ARM64 Graviton processor) in a Github Actions workflow. However, when my Docker attempts to build the image, the RUN statements fail:

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

Is it possible to build an ARM64-based Docker image on the AMD64 Github Actions runner? I've tried specifying the platform architecture in every place possible, but build are still failing.

My Github Actions workflow:

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

My 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

The portion of my CDK construct where I build my image:

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;
}

Thanks.

alukach
  • 5,921
  • 3
  • 39
  • 40

1 Answers1

3

Adding the docker/setup-qemu-action to the GitHub Actions workflow did the trick:

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

Also worth noting that it is necessary to set DOCKER_BUILDKIT: 1 on the environment when running CDK.

alukach
  • 5,921
  • 3
  • 39
  • 40