Sign inSign up

shimaenaga1123/alpine-rust-zigbuild

By shimaenaga1123

•Updated 9 months ago

Image
0

419

shimaenaga1123/alpine-rust-zigbuild repository overview

⁠Alpine Rust Zigbuild Builder

A lightweight, Rust builder image based on Alpine Linux. It is pre-configured with Rustup, Zig, and cargo-zigbuild to facilitate effortless static linking and multi-arch cross-compilation using Docker Buildx.

The image tag matches the underlying Alpine Linux version (e.g., 3.23 corresponds to alpine:3.23).

⁠🚀 Features

  • Base OS: Alpine Linux (Tag syncs with Alpine version)
  • Rust Toolchain: Managed via rustup (Stable, minimal)
  • Cross-Linker: zig (via apk)
  • Build Tool: cargo-zigbuild (Pre-installed)
  • Pre-cached Targets:
    • x86_64-unknown-linux-musl
    • aarch64-unknown-linux-musl
  • Buildx Ready: Supports dynamic TARGETARCH for "Write Once, Build Anywhere".

This image is designed to work seamlessly with docker buildx. You can build static binaries for Intel (amd64) and Apple Silicon/AWS Graviton (arm64) from a single Dockerfile without manually specifying targets.

⁠1. Create your Dockerfile

Copy the example below. It automatically detects the target architecture and selects the correct Rust target triple.

# syntax=docker/dockerfile:1

# 1. Build Stage
FROM shimaenaga1123/alpine-rust-zigbuild:3.23 AS builder

# 'TARGETARCH' is automatically passed by docker buildx (e.g., amd64, arm64)
ARG TARGETARCH

WORKDIR /app
COPY . .

# Detect Architecture & Build
RUN case "${TARGETARCH}" in \
        "amd64") RUST_TARGET="x86_64-unknown-linux-musl" ;; \
        "arm64") RUST_TARGET="aarch64-unknown-linux-musl" ;; \
        *) echo "Unsupported arch: ${TARGETARCH}" && exit 1 ;; \
    esac && \
    echo "Building for target: $RUST_TARGET" && \
    # Build utilizing zig for cross-linking
    cargo zigbuild --release --target $RUST_TARGET && \
    # Standardize binary path for the next stage
    cp target/$RUST_TARGET/release/my-app /app/my-binary

# 2. Runtime Stage
FROM alpine:3.23

# The binary is statically linked (musl), so no extra dependencies needed!
COPY --from=builder /app/my-binary /usr/local/bin/my-app

CMD ["my-app"]

Tag summary

Content type

Image

Digest

sha256:9929f2bb1…

Size

533.5 MB

Last updated

9 months ago

docker pull shimaenaga1123/alpine-rust-zigbuild