emk / rust-musl-builder

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use podman and uid mapping to cache without volume owner troubles

jcaesar opened this issue · comments

Content

I noticed that the caching / volume / UID trouble can be avoided by using podman instead of docker from an unprivileged user

podman run \
    --rm -u root -e HOME=/home/rust/ \
    -e LIB_LDFLAGS=-L/usr/lib/x86_64-linux-gnu -e CFLAGS=-I/usr/local/musl/include \
    -e CC=musl-gcc -e CXX=musl-g++ \
    -v $HOME/.cargo/git:/home/rust/.cargo/git \
    -v $HOME/.cargo/registry:/home/rust/.cargo/registry \
    -v "$PWD:/home/rust/src" \
    ekidd/rust-musl-builder:nightly cargo build

The joke here is that podman remaps 0 to your current user id if you configure /etc/sub{u,g}id. There may be more nifty ways of doing this, especially some that do not run cargo as root in the container or require that HOME= hack…

(CFLAGS, LIB_LDFLAGS, CC, and CXX are probably only required because I have rdkafka among my dependencies. I currently have no other testing ground.)

Meta

Feel free to immediately close this as a side note, or to ask me for a PR adding it to the Readme or similar…

Thank you! This is good to know.

This is a much better idea, nice contribution. I don't have a .cargo/git though. I just put this in /usr/local/bin/rust-musl-builder

podman run \
    --net host --rm -u root -e HOME=/home/rust/ \
    -e LIB_LDFLAGS=-L/usr/lib/x86_64-linux-gnu -e CFLAGS=-I/usr/local/musl/include \
    -e CC=musl-gcc -e CXX=musl-g++ \
    -v "$HOME/.cargo/registry:/home/rust/.cargo/registry" \
    -v "$PWD:/home/rust/src" \
    ekidd/rust-musl-builder:nightly cargo build "$@"

I would also suggest adding $@ at the end, I find it useful to call cargo build with --release because my Cargo.toml has specific release stuff.

Make that a "$@" (Eh, I didn't use -v "$PWD:/home/rust/src" either. Shame.) and you have me. ;)
(Also, I probably only used --net host because I wasn't able to get my network settings straight at the time. Shouldn't be necessary.)