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

Cannot use local libraries #7

Closed mhristache closed 7 years ago

mhristache commented 8 years ago

Hi

I tried to use rust-musl-builder but it failed when updating the dependencies In Cargo.toml:

error: Unable to update file:///home/rust/<project_name>

Caused by:
  failed to read `/home/rust/<project_name>/Cargo.toml`

I am using local libraries in the project I am trying to build, so I have something like this in Cargo.toml:

[dependencies.<project_name>]
path = "../<project_name>"

The path cannot be found because the local directory where the main project is stored is mounted inside the docker container, but the local libs are outside.

Not sure how/if this kind of setup can be supported in a nice way but I am reporting it anyhow :).

emk commented 8 years ago

Thank you for the bug report!

The easiest way to make it work is to place your libraries inside the directory that you're building.

[dependencies.<project_name>]
path = "./<project_name>"

If this is impossible, and you can't publish the library to crates.io, then your next best option is to modify the docker command line to pass something like -v "$(dirname $(pwd))":/home/rust/src --workdir /home/rust/src/mymainproject" to Docker. This mounts the parent directory into docker, then uses --workdir to switch to the actual directory you want to build.

There's a bunch of variations on this idea you can play with; see the Docker CLI ref and let me know what works for you.

emk commented 8 years ago

Oh, also, you could write a shell script to do complex builds, put it in the directory you mount as /home/rust/src, and call it instead of Cargo.

mhristache commented 8 years ago

First of all, thanks for the quick reply and for your time.

I tried the first suggestion (-v "$(dirname $(pwd))":/home/rust/src --workdir /home/rust/src/mymainproject)and seem to work fine.

But now I get a new error: error: failed to open: /home/rust/src/<project>/target/debug/.cargo-lock

Apparently user rust does not have write permission in the project dir. How is this supposed to work? FYI, UID 1000 is already used on my system.

Thanks

emk commented 8 years ago

Right now, the default setup only works for UID 1000, which I agree is a bit of a weakness. You'll probably need to run sudo chown -R myuser:mygroup . inside your project directory to put things back the way they were, and try adding -u $(id -u $USER) to your Docker command line. If this isn't enough to get it working, you may need to rebuild the Docker image to use a different UID, or pass flags like -v ~/.cargo:/home/rust/.cargo.

emk commented 8 years ago

Oh, or you can build a derived Docker file, copy the source code into the image using ADD, run the build, and copy the binary back out with docker cp. Basically, the trickier the stuff you want to do, the more you'll need to learn about Docker. :-/