Skip to content

Instantly share code, notes, and snippets.

@almann
Last active March 2, 2022 07:12
Show Gist options
  • Save almann/276c9eda7489cc39edfb69014d5bc12a to your computer and use it in GitHub Desktop.
Save almann/276c9eda7489cc39edfb69014d5bc12a to your computer and use it in GitHub Desktop.
Simple Dockerfile for Rust Cargo/Build (testing cross compile for musl)
# Build:
# docker build -t rust-musl --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g) -f rust-musl.dockerfile $(pwd)
#
# Run:
# docker run --interactive --tty --rm --mount type=bind,source="$(pwd)",target=/workspace rust-musl
FROM ubuntu:20.04
ARG USER_ID
ARG GROUP_ID
ARG WORKSPACE_DIR=/workspace
RUN apt-get update -qq
RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata
RUN apt-get -y install gcc g++ clang cmake make git musl curl wget
RUN git clone --recursive --depth=1 https://github.com/richfelker/musl-cross-make.git
RUN cd musl-cross-make && \
echo "TARGET=$(uname -i)-linux-musl" > config.mak && \
make -j$(nproc) && make install OUTPUT=/usr/local
RUN ln -s /usr/local/bin/$(uname -i)-linux-musl-gcc /usr/local/bin/musl-gcc && \
ln -s /usr/local/bin/$(uname -i)-linux-musl-g++ /usr/local/bin/musl-g++
# These sparse files do not behave well for large UIDs
RUN rm /var/log/faillog /var/log/lastlog
RUN groupadd -f --gid $GROUP_ID user
RUN useradd -l -m --uid $USER_ID --gid $GROUP_ID user
RUN mkdir $WORKSPACE_DIR && chown $USER_ID.$GROUP_ID $WORKSPACE_DIR
USER user
WORKDIR $WORKSPACE_DIR
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | /bin/sh -s -- -y
ENV PATH=/home/user/.cargo/bin:$PATH
RUN rustup target add $(uname -i)-unknown-linux-musl
CMD cargo clean && \
cargo build --workspace --all-targets --target $(uname -i)-unknown-linux-musl && \
cargo test --workspace --target $(uname -i)-unknown-linux-musl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment