Skip to content

refactor: chown on COPY in Dockerfile to reduce image size#8427

Merged
GiedriusS merged 1 commit into
thanos-io:mainfrom
erikgb:dockerfile-copy-chown
Aug 19, 2025
Merged

refactor: chown on COPY in Dockerfile to reduce image size#8427
GiedriusS merged 1 commit into
thanos-io:mainfrom
erikgb:dockerfile-copy-chown

Conversation

@erikgb

@erikgb erikgb commented Aug 18, 2025

Copy link
Copy Markdown
Contributor
  • I added CHANGELOG entry for this change.
  • Change is not relevant to the end user.

Changes

Modified the Dockerfile to perform the chown when copying the Thanos binary into the image. This reduces the image size by a significant amount, from 244 MB to 141 MB.

We just migrated away from the to-be💵images provided by Bitnami/Broadcom, and I was a bit surprised by the difference in image size between the official Thanos image and the bitnami/thanos image.

Verification

I ran make docker and verified that the image built starts as normal.

@GiedriusS GiedriusS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Before this change, there were probably two layers inside the image with the same binary? Do I understand it correctly?

@erikgb

erikgb commented Aug 18, 2025

Copy link
Copy Markdown
Contributor Author

Yes, it appears so. I can paste screenshots from dive when I'm back at my workstation.

@erikgb
erikgb force-pushed the dockerfile-copy-chown branch from 9945eca to 8dedfd0 Compare August 18, 2025 15:54
@erikgb

erikgb commented Aug 18, 2025

Copy link
Copy Markdown
Contributor Author

With a little help from ChatGPT to save time, this is better because:

Ah, yes — this is a classic Docker gotcha 🙂

When you run something like:
COPY file.txt /app/
RUN chown myuser:mygroup /app/file.txt
you often see the image size increase a lot, sometimes surprisingly so.

Here’s why:

  1. Docker builds images layer by layer

Each instruction (COPY, RUN, etc.) creates a new layer.

Layers are immutable — a RUN chown does not "change" the previous file, it creates a new layer with a new copy of that file with different ownership.

That means you now have two copies of the file in the image layers:

One owned by root:root from the COPY layer.

Another owned by myuser:mygroup from the chown layer.

Even though at runtime you only see the "topmost" version, both exist in the layer stack → more disk space.

@erikgb

erikgb commented Aug 18, 2025

Copy link
Copy Markdown
Contributor Author
$ make docker
$ dive docker.io/library/thanos
image
$ dive quay.io/thanos/thanos:v0.39.2
image

@GiedriusS GiedriusS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great find!

@GiedriusS
GiedriusS merged commit e61ad9c into thanos-io:main Aug 19, 2025
19 of 22 checks passed
@erikgb

erikgb commented Aug 19, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @GiedriusS! The Thanos binary is still huge and contributes to 99% of the image size. Any idea why? Anything we can do to reduce the size of the binary while not breaking stuff?

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants