The Best Strategies to Slim Docker Images

I wrote the following article. How to Reduce Docker Image Size.

Check the list here: The Best Strategies to Slim Docker Images: How to Reduce Docker Image Size - Semaphore

Let me know your comment about it. I will appreciate it.

docker

3 Likes

Welcome to the Forum! And thank you for sharing! :slight_smile:

It is generally a clean and well written blog post.

Though, you might want to rethink this part:

The final image of your application is created by copying code files and dependencies from the previous stages. This means Docker will discard any intermediate files and build artifacts that are no longer needed to create your final build.

If the FROM instruction of a stage uses a named stage, it will use all of its image layers. Pretty much like if you had built an image from the named stage alone, and used that image in the FROM instruction of your second image. Nothing is deleted. That’s why COPY --from is so popular, as it allows copying specific files/folders from a named stage into the current stage.

Further suggestions:

  • To align with the wording from the Dockerfile reference, I would suggest replacing “Dockerfile command” with “Dockerfile instruction”
  • Add a description how/why chaining commands in the RUN instructions result in minimizing the number of layers
  • provide a brief description of how slimming down an image affects the image layers and the layer cache.

Very interested topic, and great blog structure.
Appreciate your efforts, Thanks for sharing @rosechege.

However, I have a small addition here, It’s about caching and speed up the build process.

The build cache is a feature of Docker that allows it to reuse previously built layers of an image.

The COPY is likely to change frequently, and when it comes at the top of the file the docker will not use the cache for the next steps and assume that it have some changes So preferd to move The COPY at the end of the Docker file. This allows Docker to reuse the layers created in the future runs.

Thanks you @rosechege

Which copy do you mean? I might be wrong but it seemed to me that the COPY instructions were right before they were needed, but it is possible I didn’t notice where they were not.

But now when I was looking for the misplaced copies, I’ve noticed a small mistake (@rosechege ):

The first two examples in the “Docker image layers” section use the RUN instrcution to execute npm start. The third starts to use CMD as the first two should have done, but it uses the shell form so the command will be executed in a shell which is usually not recommended since it will not let docker stop to properly stop the container so it will be killed after 10 seconds. However, that has nothing to do with image size.

In fact, my addition doesn’t mean that the Dockerfile is wrong it’s just an addition.

I meant any COPY in general, the COPY is changing frequently, and if the layer changes the docker cache will invalidate all the next layers and will rebuild them again, This will gets us failure to utilize the docker cache and therefore slow-up the rapidly build.

to solve this issue should put The COPY at the bottom as possible.

I know about the layers and the cache. I was just looking for any COPY in this howto that you would move.

Or did you just want to suggest mentioning cache in the tutorial?

Utilizing the cache properly doesn’t necessarily mean smaller images, but it can lead to faster downloads in case of an image upgrade. It could be still mentioned in the tutorial or possibly in a folllowing one.

Exactly, That’s my small “addition”!..
Short mention


1 Like