skip to Main Content

I have a Dockerfile where I first build an image as build and then use that image for another stage, as follows:

FROM docker.io/rust:1.71.0-slim-bookworm as build
RUN apt-get update && apt-get install -y 
    lsb-release 
    wget 
    software-properties-common 
    gnupg
RUN wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && ./llvm.sh 15
RUN apt-get install -y 
    libz-dev 
    libelf-dev 
    protobuf-compiler 
    libbpf-dev 
    linux-headers-generic 
    bpftool 
    clang 
    make 
    pkg-config
RUN rustup component add rustfmt

COPY api /home/root/workspace/api
COPY crates /home/root/workspace/crates
COPY Cargo.lock /home/root/workspace/Cargo.lock
COPY Cargo.toml /home/root/workspace/Cargo.toml

WORKDIR /home/root/workspace
RUN --mount=type=cache,target=/usr/local/cargo/registry 
    --mount=type=cache,target=/home/root/workspace/target 
    cargo build --release --all-features
RUN --mount=type=cache,target=/home/root/workspace/target 
    cp 
    /home/root/workspace/target/release/binary 
    /usr/local/bin/

FROM docker.io/debian:12.1-slim as example-service
COPY --from=build 
    /home/root/workspace/target/release/example-service 
    /usr/local/bin
ENTRYPOINT [ "example-service" ]

FROM docker.io/debian:12.1-slim
RUN apt-get update && 
    apt-get install -y --no-install-recommends libelf-dev && 
    rm -rf /var/lib/apt/lists/*

COPY --from=build 
    /usr/local/bin 
    /usr/local/bin/
COPY docker/configs configs
COPY docker/scripts scripts

I use docker compose build to build the images. However, that leads to the following error (implying that the WORKDIR is not available for the second stage build?):

failed to solve: failed to compute cache key: failed to calculate checksum of ref 0687bf79-e5fa-4ea1-9f75-46a52cb9501c::stlqysxbl0pkzy1ijsffcgjgv: failed to walk /mydata/local/docker/tmp/buildkit-mount3152819150/home/root/workspace/target/release: lstat /mydata/local/docker/tmp/buildkit-mount3152819150/home/root/workspace/target/release: no such file or directory

However, if I comment the lines caching the target dir (i.e. the lines --mount=type=cache,target=/home/root/workspace/target), it works. Is this a problem with caching the mounts or is there something wrong with my Dockerfile?

2

Answers


  1. Can you please, before mount line add the below command to show the current working directory and its content

    RUN echo "Current Working Directory: ${PWD}" && ls -lR
    

    Most likely wrong path

    Login or Signup to reply.
  2. I have had this issue as well on two different platforms (Docker Desktop for Windows and Podman).

    Firstly, cache mounts were added to the buildkit builder in Dockerfile syntax 1.2 (are you using BuildKit to build your image?). So you need to add the syntax version to the top of your Dockerfile.

    # syntax=docker/dockerfile:1.2
    FROM docker.io/rust:1.71.0-slim-bookworm as build
    # other commands ...
    

    This fixed the issue for me on Docker Desktop for Windows.

    For Podman, apparently run cache mounts are only made available during the RUN command and not baked into the actual image. So anything you need from the cache should be copied outside of it as part of the command.

    Using your example:

    # copy the binary to someplace outside the cache
    RUN --mount=type=cache,target=/usr/local/cargo/registry 
        --mount=type=cache,target=/home/root/workspace/target 
        cargo build --release --all-features 
            && cp /home/root/workspace/target/release/binary /usr/local/bin/
    
    FROM docker.io/debian:12.1-slim as example-service
    
    # copy the binary you pulled outside of the cache
    COPY --from=build /usr/local/bin/binary /usr/local/bin
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search