Skip to content

Instantly share code, notes, and snippets.

@feymartynov
Created August 11, 2024 15:15
Show Gist options
  • Save feymartynov/554e96a1baa9f43ffc0405d238451891 to your computer and use it in GitHub Desktop.
Save feymartynov/554e96a1baa9f43ffc0405d238451891 to your computer and use it in GitHub Desktop.
Rust program cross build from Mac M1 -> Linux amd64 using Docker for Mac

Rust program cross build from Mac M1 -> Linux amd64 using Docker for Mac.

  1. Run cargo new hello to create and example program.
  2. Add Dockerfile and buildx.sh from this gist.
  3. Run ./buildx.sh.
  4. Complied program will be put into ./release/hello. Copy it to a x86-64 Linux machine and try to run.
#!/bin/sh
docker buildx build --platform=linux/amd64 -t hello:amd64 --output . .
FROM debian:bookworm AS build
RUN apt-get update && apt-get install -y \
build-essential \
pkg-config \
curl \
&& rm -rf /var/lib/apt/lists/*
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain=1.80.0
ENV PATH /root/.cargo/bin:$PATH
COPY . /app
WORKDIR /app
RUN cargo build --release
###############################################################################
FROM scratch
COPY --from=build /app/target/release/hello /release/hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment