LukeMathWalker / cargo-chef

A cargo-subcommand to speed up Rust Docker builds using Docker layer caching.
Apache License 2.0
1.81k stars 117 forks source link

Build fails with library #218

Closed JSanchesDovichi closed 1 year ago

JSanchesDovichi commented 1 year ago

Hello! I've been updating an old project to be deployed with Docker and i got stuck on a little problem, i hope it's allright to ask for guidance here.

The problem is: Rust can't link to some library dependencies i have installed in a Arch-based image.

I built a minimal repository to reproduce my problem (available in https://github.com/JSanchesDovichi/Rocket-Docker-Compose-LibLink). This repo has a Rust Virtual Workspace, and a compose file. Inside the Workspace there are 2 crates. 1 of them depends on a "dlib-face-recognition" library. The first thing i tried was using the example script to build a Rust project from this repository, which worked until i needed depending on dlib library. The build now fails, as it can't link the library on the container. I tried crating a container image (available on https://hub.docker.com/repository/docker/jsanchesdovichi/gfea-workspace/general) where i installed the base dependencies for the library, and then tried using this image in the "FROM debian:buster-slim AS runtime" step from the Dockerfiles.

I'm fairly new to Docker, so any help would be greatly appreaciated! Thanks!

markdingram commented 1 year ago

This isn't a cargo-chef issue, but thanks to the repo I can see you are almost there -

Try:

FROM rust:1-bullseye AS chef
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
RUN apt-get update && apt-get install -y cmake libdlib-dev libblas-dev liblapack-dev 
RUN cargo install cargo-chef
WORKDIR /app

FROM chef AS planner
COPY . .
RUN cargo chef prepare  --bin crate2 --recipe-path recipe.json

FROM chef AS builder 
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
# Build application
COPY . .

RUN cargo build --release --bin crate2

# We do not need the Rust toolchain to run the binary!
FROM debian:bullseye
RUN apt-get update && apt-get install -y libdlib19 libblas3 liblapack3
WORKDIR /app
COPY --from=builder /app/target/release/crate2 /usr/local/bin
ENTRYPOINT ["/usr/local/bin/crate2"]      
JSanchesDovichi commented 1 year ago

@markdingram That worked! Thank you so much!

I did really got so close, i tried mostly that with other linux images, but i never tried installing the dependencies (or their dev modes) in both stages.

Thanks so much again!