Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document recommended deployment environment #332

Open
nightkr opened this issue Oct 25, 2020 · 4 comments
Open

Document recommended deployment environment #332

nightkr opened this issue Oct 25, 2020 · 4 comments
Labels
docs unclear documentation

Comments

@nightkr
Copy link
Member

nightkr commented Oct 25, 2020

#331 highlights that this can be pretty confusing at the moment...

Arguably this isn't really specific to kube-rs, but at the same time it's probably something that will be relevant for 99% of kube-rs users.

@nightkr
Copy link
Member Author

nightkr commented Oct 25, 2020

Personally, I'd lean towards crate2nix and buildLayeredImage because:

  1. It has much better caching behaviour than Dockerfiles (since it caches each Rust package individually, just like Cargo)
  2. It automatically figures out which dependencies are required at runtime, and tree-shakes the rest
  3. It can theoretically cross-build packages, but I'm not sure about whether the Rust tooling is set up for this yet

However, it does have some disadvantages...

  1. Nix doesn't have the mindshare of Dockerfiles
  2. It can take advantage of remote builders, but it's a bit messier to set up than Docker for Desktop

@MikailBag
Copy link
Contributor

MikailBag commented Oct 25, 2020

cargo-wharf is good at caching too.

@nightkr
Copy link
Member Author

nightkr commented Oct 25, 2020

Oh, that's interesting. The caching seems stronger than for the plain Dockerfile setup, but there doesn't seem to be much of a story for non-cargo build steps (seeing apt invoked in the instructions is a bit worrying..). I'd also be a bit wary about encouraging dependencies on non-official Docker Hub images, given their recent changes to both rate limits and retention.

@nightkr nightkr added the docs unclear documentation label Oct 25, 2020
@praveenperera
Copy link
Contributor

praveenperera commented Oct 28, 2020

For what its work I've been using ekidd/rust-musl-builder as my builder.

Here is an example:

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

ENV APP=my-app

RUN mkdir .cargo src

COPY Cargo.toml Cargo.toml
COPY Cargo.lock Cargo.lock

RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > src/lib.rs && \
    cargo vendor > .cargo/config && \ 
    cargo build --release && \
    rm -rf src

COPY src src

RUN rm -rf target/release/**/libsrc* 
RUN cargo build --release

FROM alpine:latest

ARG APP_FOLDER=/usr/src/app
ENV APP_USER=appuser

RUN addgroup -S $APP_USER \
    && adduser -S -g $APP_USER $APP_USER

RUN apk update \
    && apk add --no-cache ca-certificates open-ssl \
    && rm -rf /var/cache/apk/*

COPY --from=builder /home/rust/src/target/x86_64-unknown-linux-musl/release/${APP} ${APP_FOLDER}/${APP}
ENV PATH="${APP_FOLDER}:${PATH}"

CMD ["${APP}"]

Replaced the hacky method with cargo chef

FROM ekidd/rust-musl-builder:stable as planner

RUN cargo install cargo-chef

COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM ekidd/rust-musl-builder:stable as cacher

RUN cargo install cargo-chef
COPY --from=planner /home/rust/src/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json

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

COPY . .
COPY --from=cacher /home/rust/src/target target
COPY --from=cacher /home/rust/.cargo /home/rust/.cargo

RUN cargo build --release

FROM alpine:latest

ENV APP=my-app
ARG APP_FOLDER=/usr/src/app
ENV APP_USER=appuser

RUN addgroup -S $APP_USER \
    && adduser -S -g $APP_USER $APP_USER

RUN apk update \
    && apk add --no-cache ca-certificates \
    && rm -rf /var/cache/apk/*

COPY --from=builder /home/rust/src/target/x86_64-unknown-linux-musl/release/${APP} ${APP_FOLDER}/${APP}
ENV PATH="${APP_FOLDER}:${PATH}"

CMD ${APP}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs unclear documentation
Projects
None yet
Development

No branches or pull requests

3 participants