DEV Community

Cover image for A crash course on optimizing your Docker images for production

A crash course on optimizing your Docker images for production

Adnan Rahić on October 18, 2018

Disclaimer: Zeet is sponsoring this blogpost for the next month. I tried it out the other day. It's like serverless but for running entire back e...
Collapse
 
vinayhegde1990 profile image
Vinay Hegde

Great article for people like me grappling the nuances of Docker, Adnan!

Quick question - Is the final code snippet you've shared (starting with FROM alpine AS builder) and ending with (CMD ["node", "app.js"]) part of the same Dockerfile?

If yes, won't that add more layers & thus increase overall size? Please correct me if I'm wrong.

Collapse
 
adnanrahic profile image
Adnan Rahić

On the contrary. It'll reduce the number of layers. The builder image is an intermediary image and will not be part of the final image. The bottom part of the Dockerfile is where you create a fresh image and copy over the node_modules. In doing so drastically reducing the number of layers, and excluding build dependencies.

Collapse
 
vinayhegde1990 profile image
Vinay Hegde

Thanks for the explanation that builder images are intermediate and not a part of the final one.

While I'll surely dabble with it myself for better clarity, from your experience so far - is this reduction possible for only Node apps or can be replicated for virtually any application?

Thread Thread
 
adnanrahic profile image
Adnan Rahić

It has nothing to do with the programming language or runtime. It solely has to do with the way you structure and build your images.

Collapse
 
joehobot profile image
Joe Hobot

Sweet, really nice written post.

Collapse
 
adnanrahic profile image
Adnan Rahić

Thanks Joe!

Collapse
 
uf4no profile image
Antonio

Great post. I'm starting to play with Docker so this is veery helpful! thanks.

Collapse
 
brodan profile image
Brodan

Great timing on this post! I just joined a new company/team and one of my first tasks involves configuring/optimizing Docker! Looking forward to using what I learned from here!

Collapse
 
adnanrahic profile image
Adnan Rahić

That's awesome! Good luck. 😀