Add caching to Dockerfile#690
Conversation
| cp -v LICENSE /image | ||
| cp -rv /etc/ssl /image/etc | ||
| ARG CARGO_BUILD_JOBS=default | ||
| ENV CARGO_BUILD_JOBS=$CARGO_BUILD_JOBS |
There was a problem hiding this comment.
Without setting this to 4 the LTO causes cargo to OOM on my laptop, even after I give it a lot of RAM. 😞
| install ./build/$(xx-cargo --print-target-triple)/release/tansu /usr/bin | ||
| EOF | ||
|
|
||
| FROM scratch AS out | ||
|
|
||
| COPY --from=builder --parents /etc/ssl / | ||
| COPY --from=builder /usr/src/LICENSE /usr/bin/tansu / |
There was a problem hiding this comment.
I tweaked this so it still only generates one layer in the final image but I found it easier to see when a file was missing using COPY instead of cp. 🤷
I wouldn't be surprised if docker didn't squash multiple copy instructions automatically these days, so this might be totally unnecessary. 😅
| ARG TARGETPLATFORM | ||
| RUN xx-apk add --no-cache musl-dev zlib-dev zlib-static gcc | ||
|
|
||
| WORKDIR /usr/src | ||
|
|
||
| COPY rust-toolchain.toml . | ||
| RUN rustup show && rustup target add $(xx-cargo --print-target-triple) |
There was a problem hiding this comment.
I moved this before the source ADD so that it doesn't have to be redownloaded/installed on every source tweak.
| --mount=type=cache,target=/usr/src/build <<EOF | ||
| xx-cargo build --bin tansu --no-default-features --features dynostore --release --target-dir ./build | ||
| xx-verify --static ./build/$(xx-cargo --print-target-triple)/release/tansu | ||
| install ./build/$(xx-cargo --print-target-triple)/release/tansu /usr/bin |
There was a problem hiding this comment.
You have to copy this out of the build cache or it wont be in the layer for the copy stage.
|
Nice, will take a look |
| RUN --mount=type=cache,target=/usr/local/cargo/registry,id=cargo-registry-${TARGETPLATFORM},sharing=locked \ | ||
| --mount=type=cache,target=/usr/local/cargo/git,id=cargo-git-${TARGETPLATFORM},sharing=locked \ | ||
| --mount=type=cache,target=/usr/src/build,id=cargo-build-${TARGETPLATFORM} <<EOF |
There was a problem hiding this comment.
Ah. I've changed the cache mount to include TARGETPLATFORM so that when CI builds the two images at the same time they don't write over each other. 😅
I found myself rebuilding this image quite a lot while I was working on fixing an s3 performance issue. 😅
This tweaks the Dockerfile to be more rebuild friendly.
I guess there is a good chance you wont want or need this change, in which case no worries! If nothing else it might be helpful for anyone else who needs to
docker buildthis locally a bunch of times in a row. 😁