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

Example: Hello-World from Scratch #110

Closed jlek closed 2 years ago

jlek commented 3 years ago

Hello! I have been using this project to learn a bit more about Rust compilation, (static) linking, and Docker, so: thanks! :grin: I'm a bit stuck on one little thing, so I thought I'd create this issue.

How could this project be improved?

Is it possible to use rust-musl-builder to make Docker images from Scratch, rather than from Alpine? If so, would it be possible to add an example? (Or is there a good reason to use Alpine rather than Scratch?)

If you're interested in implementing this feature yourself, how could I help you?

Once I figure it out, I would be happy to submit a pull request with the example. Here is my Dockerfile so far:

FROM ekidd/rust-musl-builder:stable AS builder

WORKDIR /rust-docker
COPY . .
RUN sudo chown -R rust:rust .
RUN cargo build --release

FROM scratch

COPY --from=builder /rust-docker/target/x86_64-unknown-linux-musl/release/rust-docker /rust-docker
USER 1000
CMD ["/rust-docker"]

However, I get an error:

docker: Error response from daemon: linux spec user: unable to find user rust: no matching entries in passwd file.
ERRO[0000] error getting events from daemon: net/http: request canceled 
emk commented 3 years ago

I have never tried this. :-) And it's not currently a supported configuration.

But it looks like you're missing your /etc/password file. Even in a "from scratch" environment, you'll need to set up a few key files expected by various libraries.

I'll leave this issue open in case you figure it out and want to document it and/or provide a new example program. However, I am unlikely to have any time to work on this myself.

OliverEvans96 commented 3 years ago

Here's a working example @jlek :)

FROM ekidd/rust-musl-builder:stable AS builder
WORKDIR /home/rust

RUN USER=rust cargo new --bin hello-world \
 && cd hello-world \
 && cargo build --release

FROM scratch

COPY --from=builder /home/rust/hello-world/target/x86_64-unknown-linux-musl/release/hello-world /usr/local/bin/
USER 1000
CMD ["hello-world"]

Let me know if it works for you as well

Cheers, Oliver

EvanCarroll commented 3 years ago

I build all the time non-Alpine. I'm using this image to do distroless using buildah. Works great!

Note scratch is actually garbage to use. For example, it won't support ssl. If you need something lighter than alpine, check out distroless static.

OliverEvans96 commented 3 years ago

@EvanCarroll - that link is broken

EvanCarroll commented 3 years ago

@EvanCarroll - that link is broken

Fixed!

emk commented 2 years ago

Yes, the only real reason to use rust-musl-builder instead of cross is to support OpenSSL. So you definitely want to look for a base image that SSL certificates correctly installed and configured, at a minimum.