Docker build re-compile problem: not incremental

I am deploying a Rust API (Actix) on a resource limited server (which runs out of memory trying to compile the api). So I set up a docker based build process so that I can build on my developer machine and push the resulting binary to the server.

I mount the "target" directory into the docker image. My Dockerfile looks like this:

FROM amazonlinux:2023.3.20231211.4

# Install dependencies
RUN yum update -y && \
 yum install -y openssl-devel && \
 yum install -y rust cargo

RUN mkdir -p /opt/api/src
COPY Cargo.toml /opt/api
COPY Cargo.lock /opt/api
COPY src /opt/api/src

WORKDIR /opt/api/

CMD cargo build --release

And in my compose.yaml I mount the target directory like this:

      - ./docker/build/target:/opt/api/target

I figured that would enable incremental builds. i.e. it would only recompile what has changed ... but everytime I run docker compose up it insists on recompiling everything. Any ideas on how I can avoid the full recompile?

This is likely getting invalidated due to mismatches between your host and docker environment. Instead you should define cache dirs to use during individual RUN steps.

Additionally you should setup a multistage build that builds the rust code in the first stage and then copies the binary to the target stage.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.