LukeMathWalker / cargo-chef

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

After changing to cargo chef the program stops right after execution #233

Closed EvilWatermelon closed 1 year ago

EvilWatermelon commented 1 year ago

I recently changed my Dockerfile to cargo chef and after it runs successfully then my actix-web program stops immediatly with exit code 0. What do I have to change in my Dockerfile?

# Build stage
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef

RUN apt-get update && \
  apt-get install -y pkg-config make g++ libssl-dev cmake libmariadb-dev-compat && \
  rustup target add x86_64-unknown-linux-gnu 

WORKDIR /var/www/app

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

FROM chef AS builder
COPY --from=planner /var/www/app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json

COPY . .

RUN cargo build --release

# Prod stage, removing the Rust toolchain
FROM debian:buster-slim AS runtime
WORKDIR /var/www/app
COPY --from=builder /var/www/app/target/release/isumis /usr/local/bin

CMD ["/usr/local/bin/isumis"]
LukeMathWalker commented 1 year ago

Can you replacing

RUN cargo build --release

with

RUN cargo build --release --bin isumis

?

lanklaas commented 1 year ago

I also started getting this recently. I was able to create a minimal example for you here.

To test I ran the build:

cd cargo-chef-demo
docker build -t test .

Then shell into the container to run the bin:

docker run -it test ash
./chef-builds

It should print "Hello,world!", but just exits.

Let me know if I can do anything to help

EvilWatermelon commented 1 year ago

@lanklaas it doesn't print "Hello,world!".

cargo-chef-demo$ sudo docker run -it test ash
/app $ ./chef-builds
/app $ 

@LukeMathWalker I added it but that didn't fixed it.

lanklaas commented 1 year ago

Sorry, think I worded it wrong. The example I gave is an example of the broken one. So it not printing Hello,world! is the problem. I pushed another dockerfile to the repo to show you the working example (without using cargo-chef). If you pull again you can build it like this:

docker build -f working.Dockerfile -t wtest .

Then run it:

docker run -it wtest ash
./chef-builds

This one prints Hello,world!

An interesting thing I noticed was that the binary file sizes differs just a little:

Broken Size:

/app $ ls -l
total 4372
-rwxr-xr-x    1 root     root       4475024 Aug 18 06:11 chef-builds

Working size

/app $ ls -l
total 4384
-rwxr-xr-x    1 root     root       4481912 Aug 18 06:56 chef-builds
EvilWatermelon commented 1 year ago

And how this solve my problem?

ERROR: failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount1208140388/working.Dockerfile: no such file or directory

dman-os commented 1 year ago

I run into this as well. After a whole day of tinkering with things, I was able to narrow down the issue to cargo-chef. Removing it from the Dockerfile revealed the real issue, I had mistakenly excluded some necessary files through .dockerignore entries (including the main.rs). And yet, with cargo chef cook in the recipe, the Dockerfile compiled and completed without an issue! Fixing the ignore entries solved everything and hopefully, this case helps to narrow down the issue.

LukeMathWalker commented 1 year ago

@lanklaas: pretty sure the issue is due to a swapped statement in your Dockerfile, see https://github.com/lanklaas/cargo-chef-demo/pull/1 I'm not at my laptop to confirm it works, but I'm fairly confident.

lanklaas commented 1 year ago

@LukeMathWalker thanks, Yes I forgot I swapped those because I got version issues before the swap. I added 2 branches on the repo correct-copy-order which is the same fix as your PR, and workspace in which I changed the repo to a cargo workspace to fix the error in correct-copy-order, just in case it helps someone else here.

I tracked down the original issue that caused me to do the swap, but it is not related to this issue. I will investigate and report it if I have some more concrete info

LukeMathWalker commented 1 year ago

Perfect, I'll close the issue then 👍🏻