How do you keep Docker images small without breaking things?

November 23, 2026 4 answers
Asked by c.571+vordali

Multi-stage builds help, but slim and Alpine images can cause compatibility problems. What else do you do to reduce image size without making the app hard to run or debug?

Answers

7v!parcelwhisk_583

Start with a multi-stage build and copy only the application and its runtime dependencies into the final stage. Compilers, build tools, test files, and installers usually have no reason to go into production.

Also watch where temporary files are created and removed. Deleting an archive in a later layer does not remove its bytes from an earlier layer. Download, use, and clean it up in the same build step, or leave it in the build stage.

Reply to this answer
_k49+zordelo.7

Don't pick Alpine just because it's small. Its musl C library can be a problem for software or native dependencies built around glibc. A slim image based on a compatible distribution may be a better tradeoff.

Distroless or scratch can work well too, especially for a static binary, but check what the app actually needs at runtime. Certificates, timezone data, shared libraries, and other files can be easy to miss.

c.571+vordali:

How do you debug a distroless image without a shell?

Tavmelo-4q:

Keep a separate diagnostic image with the tools you need, or use external debugging tools where your environment supports them. Try to keep the application and runtime dependencies close to the production image.

Reply to this answer
_k72!brindlevisor.3

Test the final image, not just the build stage. Run an acceptance test against it in the build or pipeline so missing libraries and other basic startup failures show up before deployment.

File access tracing can help you figure out what the container uses, too. Exercise as much of the app as you can, inspect the report, then use it to guide what you leave out. A trace only covers the paths your tests actually ran, so it isn't proof that everything else is safe to delete.

Reply to this answer
Vexrani+4h

A production image and a diagnostic image can be useful companions. Keep the production one lean, and give the diagnostic version a shell and troubleshooting tools. That way you don't have to choose between shipping every tool and having no way to investigate a problem.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions