Skip to content

Add caching to Dockerfile#690

Open
simon-engledew wants to merge 2 commits into
tansu-io:mainfrom
simon-engledew:simon-engledew/dockerfile
Open

Add caching to Dockerfile#690
simon-engledew wants to merge 2 commits into
tansu-io:mainfrom
simon-engledew:simon-engledew/dockerfile

Conversation

@simon-engledew

Copy link
Copy Markdown

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 build this locally a bunch of times in a row. 😁

Comment thread Dockerfile
cp -v LICENSE /image
cp -rv /etc/ssl /image/etc
ARG CARGO_BUILD_JOBS=default
ENV CARGO_BUILD_JOBS=$CARGO_BUILD_JOBS

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without setting this to 4 the LTO causes cargo to OOM on my laptop, even after I give it a lot of RAM. 😞

Comment thread Dockerfile
Comment on lines +40 to +46
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 /

@simon-engledew simon-engledew Jun 24, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. 😅

Comment thread Dockerfile
Comment on lines +22 to +28
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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this before the source ADD so that it doesn't have to be redownloaded/installed on every source tweak.

Comment thread Dockerfile
--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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to copy this out of the build cache or it wont be in the layer for the copy stage.

@shortishly shortishly marked this pull request as ready for review June 24, 2026 22:36
@shortishly

Copy link
Copy Markdown
Contributor

Nice, will take a look

Comment thread Dockerfile
Comment on lines +35 to +37
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants