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

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

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. :)

DEV is built on open source software called Forem!

GitHub logo forem / forem

For empowering community 🌱


Forem 🌱

For Empowering Community

Build Status Build Status GitHub commit activity GitHub issues ready for dev GitPod badge

Welcome to the Forem codebase, the platform that powersdev.to. We are so excited to have you. With your help, we canbuild out Forem’s usability, scalability, and stability to better serve ourcommunities.

What is Forem?

Forem is open source software for building communities. Communities for yourpeers, customers, fanbases, families, friends, and any other time and spacewhere people need to come together to be part of a collectiveSee our announcement postfor a high-level overview of what Forem is.

dev.to (or just DEV) is hosted by Forem. It is a community ofsoftware developers who write articles, take part in discussions, and buildtheir professional profiles. We value supportive and constructive dialogue inthe pursuit of great code and career growth for all members. The ecosystem spansfrom beginner to advanced developers, and all are welcome to find their place…

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay