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.
Apache License 2.0
1.54k stars 193 forks source link

Use podman and uid mapping to cache without volume owner troubles #78

Closed jcaesar closed 5 years ago

jcaesar commented 5 years ago

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…

emk commented 5 years ago

Thank you! This is good to know.

EvanCarroll commented 3 years ago

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.

jcaesar commented 3 years ago

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.)