Skip to content

Commit

Permalink
Merge pull request #53 from miscord-dev/cross-compile-docker
Browse files Browse the repository at this point in the history
Use zig to cross compile Docker images
  • Loading branch information
tsuzu authored Mar 11, 2023
2 parents 74cc175 + 3943c0e commit d1b5216
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
21 changes: 1 addition & 20 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ jobs:
platforms: linux/amd64,linux/arm64
-
name: Login to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ghcr.io
Expand All @@ -44,30 +43,12 @@ jobs:
type=ref,event=tag
type=ref,event=pr
type=sha
-
name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ matrix.component }}-${{ matrix.arch }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-${{ matrix.component }}-${{ matrix.arch }}-
-
name: Build and push
uses: docker/build-push-action@v3
with:
context: .
file: ${{ matrix.component }}.Dockerfile
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
-
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
2 changes: 1 addition & 1 deletion controlplane.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.19 as builder
FROM --platform=$BUILDPLATFORM golang:1.19 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
46 changes: 42 additions & 4 deletions tetrad.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
# Download zig for builarch amd64
FROM --platform=$BUILDPLATFORM golang:1.19 AS zigdownloader-amd64

RUN apt-get update && \
apt-get install -y --no-install-recommends xz-utils && \
curl -LO https://ziglang.org/download/0.10.1/zig-linux-x86_64-0.10.1.tar.xz && \
tar xf zig-*.tar.xz && \
mv ./zig-*/ /zig/

# Download zig for builarch arm64
FROM --platform=$BUILDPLATFORM golang:1.19 AS zigdownloader-arm64

RUN apt-get update && \
apt-get install -y --no-install-recommends xz-utils && \
curl -LO https://ziglang.org/download/0.10.1/zig-linux-aarch64-0.10.1.tar.xz && \
tar xf zig-*.tar.xz && \
mv ./zig-*/ /zig/

FROM zigdownloader-${BUILDARCH} AS zigdownloader

# Prepare for targetarch amd64
FROM --platform=$BUILDPLATFORM golang:1.19 AS gobase-amd64

ENV ZIGTARGET x86_64-linux-gnu

# Prepare for targetarch arm64
FROM --platform=$BUILDPLATFORM golang:1.19 AS gobase-arm64

ENV ZIGTARGET aarch64-linux-gnu

# Build the manager binary
FROM golang:1.19 AS gomod
FROM gobase-${TARGETARCH} AS gomod

ARG TARGETARCH

WORKDIR /workspace
# Copy the Go Modules manifests
Expand All @@ -18,21 +50,27 @@ COPY tetraengine/ tetraengine/

FROM gomod AS builder

COPY --from=zigdownloader /zig /zig

ENV PATH $PATH:/zig

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN mkdir -p bin && \
go build -a -o ./bin/tetrad ./tetrad && \
go build -a -o ./bin/tetrad-entrypoint ./tetrad/cmd/tetrad-entrypoint
GOOS=linux GOARCH=${TARGETARCH} CGO_ENABLED=1 CC="zig cc -target ${ZIGTARGET}" go build -a -o ./bin/tetrad ./tetrad && \
GOOS=linux GOARCH=${TARGETARCH} CGO_ENABLED=0 go build -a -o ./bin/tetrad-entrypoint ./tetrad/cmd/tetrad-entrypoint

FROM gomod AS tetracni

ARG TARGETARCH

COPY tetracni/ tetracni/
COPY Makefile Makefile

RUN make cni-plugins
RUN GOOS=linux GOARCH=${TARGETARCH} make cni-plugins

# Fetch CNI plugins
FROM golang:1.19 AS plugins
Expand Down

0 comments on commit d1b5216

Please sign in to comment.