DEV Community

Cover image for #DEVDiscuss: Optimizing Dockerfiles
Erin Bensinger for The DEV Team

Posted on

11 1

#DEVDiscuss: Optimizing Dockerfiles

Time for #DEVDiscuss β€” right here on DEV 😎

Inspired by @er_dward's Top 7 post, tonight’s topic is...optimizing Dockerfiles 🐳

According to the post...

Leveraging Docker's caching mechanism and multistage builds can result in significant enhancements in Dockerfile efficiency for a Laravel PHP application using Yarn and Nginx. With a better understanding of these mechanisms, developers can craft Dockerfiles that build faster, produce smaller images, and thus, reduce resource usage.

Questions:

  • What tools, tips, and tricks have you used to optimize Dockerfiles?
  • Have you used any non-Docker solutions for efficient container development?
  • Any triumphs, fail whales, or other stories you'd like to share on this topic?

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (3)

Collapse
 
tbroyer profile image
Thomas Broyer β€’

One thing people often forget: installing packages/dependencies will (generally) keep download caches that are totally useless at runtime: npm ci, pip install, etc. but also apt-get.

One thing I like to do: use BuildKit's --mount on RUN stages to put those cache dirs outside the image. You can use a cache mount to keep those between builds, or tmpfs mounts for truely volatile storage.

Do the same with bind mounts instead of COPYing a file to process and delete it in a following RUN (such as copying an archive to then extract and delete it)

Also, use Dive to analyse your images and find bloat. Not only those files overwritten in a subsequent layer (that Dive automatically detects), but also those files you shouldn't have added to your layers (such as those download caches)

Collapse
 
pavelee profile image
PaweΕ‚ Ciosek β€’
  • put the most changeable layers on the end of docker file. It will speed up building, rebuilding only changed layers.
  • analyze weights of your layers using "docker history". It will show you size of each layer. Great hint where to start optimize, eg. not necessary packages
  • Add dockerignore file, there you can specify what should not be places in the container. For example some cash file, binary etc. Lack of a dockerignore file is the most common mistake
  • put together in a one layer installation of packages (eg. apt-get install) and after that add after installation cache clear in the same layer. It will reduce the final size.
  • use alpine base images, it's super light weight OS <3
  • When you prepare your production image, copy what is really necessary to run your app
Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi β€’

What tools, tips, and tricks have you used to optimize Dockerfiles?

I wrote a blog post a while ago about optimizing the size (and also build speed) of the containers for AWS Lambdas written in Rust:

Some obvious tricks and tips:

  1. Use multi-stage images
  2. Cache build folders
  3. Use smaller images (distroless or even scratch)

Have you used any non-Docker solutions for efficient container development?

Yes, while I was writing my other blog post:

I used Podman and LXC/LXD.

Any triumphs, fail whales, or other stories you'd like to share on this topic?

I guess 2 blog posts should be enough, although, I have even more stories for anybody interested. :)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay