Skip to content

Commit f508d0a

Browse files
author
Sunser
committed
Add multi-arch image build
1 parent 64cc6b1 commit f508d0a

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

.github/workflows/docker-image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
uses: docker/build-push-action@v6
5555
with:
5656
context: .
57-
platforms: linux/amd64
57+
platforms: linux/amd64,linux/arm64,linux/arm/v7
5858
push: ${{ github.event_name != 'pull_request' }}
5959
tags: ${{ steps.meta.outputs.tags }}
6060
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
FROM golang:1.22-alpine AS builder
1+
ARG BUILDPLATFORM=linux/amd64
2+
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS builder
3+
4+
ARG TARGETOS
5+
ARG TARGETARCH
6+
ARG TARGETVARIANT
27

38
WORKDIR /src
9+
RUN apk add --no-cache ca-certificates tzdata
10+
411
COPY go.mod ./
512
COPY cmd ./cmd
613
COPY internal ./internal
714

8-
RUN mkdir -p /out && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o /out/ecs-controller ./cmd/ecs-controller
15+
RUN set -eux; \
16+
target_os="${TARGETOS:-linux}"; \
17+
target_arch="${TARGETARCH:-amd64}"; \
18+
if [ "$target_arch" = "arm" ]; then \
19+
export GOARM="${TARGETVARIANT#v}"; \
20+
if [ -z "$GOARM" ]; then export GOARM=7; fi; \
21+
fi; \
22+
mkdir -p /out; \
23+
CGO_ENABLED=0 GOOS="$target_os" GOARCH="$target_arch" \
24+
go build -trimpath -ldflags="-s -w" -o /out/ecs-controller ./cmd/ecs-controller
925

1026
FROM alpine:3.20
1127

12-
RUN apk add --no-cache ca-certificates tzdata
1328
WORKDIR /app
29+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
30+
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
1431
COPY --from=builder /out/ecs-controller ./ecs-controller
1532
COPY web ./web
1633

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,26 @@ go build -o ecs-controller ./cmd/ecs-controller
565565
ghcr.io/sunser/ecs-controller
566566
```
567567

568+
镜像使用 Docker Buildx 构建,当前发布以下架构:
569+
570+
| 平台 | 常见设备 |
571+
| --- | --- |
572+
| `linux/amd64` | 常见 x86_64 云服务器、PC、NAS |
573+
| `linux/arm64` | ARM64 云服务器、Apple Silicon、部分 NAS 和开发板 |
574+
| `linux/arm/v7` | 32 位 ARMv7 设备 |
575+
576+
使用同一个镜像 tag 即可,Docker 会按当前机器架构自动选择对应镜像。例如:
577+
578+
```bash
579+
docker pull ghcr.io/sunser/ecs-controller:latest
580+
```
581+
582+
如果需要在本地手工构建多架构镜像,可以使用:
583+
584+
```bash
585+
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t ghcr.io/sunser/ecs-controller:local .
586+
```
587+
568588
推送到 `main` 分支后会生成:
569589

570590
```text

0 commit comments

Comments
 (0)