SlideShare a Scribd company logo
Dockerfile Best Practices
Bhushan Lodha
Tuesday 7th July 2020, 6pm
Senior Software Consultant
Why should I care about writing good dockerfile?
➔ Image size
◆ Smaller image size improves security, performance, efficiency, and
maintainability of your containers
➔ Build time
◆ Reducing build time can significantly reduce your app’s development cost
➔ Security
◆ Important to protect your app from external attacks
➔ Consistency
◆ Consistent images are easier to maintain
Reduce Image Size
Faster deploys, smaller attack surface
Image Variants
Image Size
ruby:2.7 842mb
ruby:2.7-slim 149mb
ruby:2.7-alpine 52mb
Base Image
Ubuntu/Debian Alpine
Built around GNU C library Built around musl libc
More mature Less mature
More stable Less stable
More 3rd party packages Fewer 3rd party packages
Less memory efficient More memory efficient
Bigger base image footprint Smaller base image footprint
Premature optimization is root of (all) evil
Remove unnecessary dependencies
before 726mb
after 692mb
Remove package manager cache, logs & docs
before 692mb
after 674mb
Remove unnecessary application files
➔ Test files
➔ Asset files
➔ Documentation
➔ Application dependency test files
➔ Application cache
➔ Dependency cache
Use multistage builds to separate build & runtime env
before 674mb
after 301mb
Reduce Build Time
Leverage build cache
Order matters!
Order from least to most frequently changing content
Copy specific file to limit cache busts
Identify cacheable units
Use Buildkit
➔ Second generation image builder
➔ Parallel build execution
➔ Much faster and more accurate caching mechanism
Enable Buildkit
Buildkit: Parallel Build Execution
1 2
3
1
2
3
with buildkit
without buildkit
Buildkit performance benchmarking
Based on the docker build from scratch, the results are 2.5x faster
source: https://www.xenonstack.com/blog/docker-buildkit/
Buildkit performance benchmarking
Rerunning the same build with local cache the speed is 7x faster.
source: https://www.xenonstack.com/blog/docker-buildkit/
Buildkit performance benchmarking
Repeatable docker builds with new source code leads to 2.5 fx faster build
speed.
source: https://www.xenonstack.com/blog/docker-buildkit/
Security
Avoid running your application as a root user
Use --chown when running ADD or COPY
Consistency
Consistent image is easier to maintain
Pin base image version
Add .dockerignore file
Use official images when possible
Set Locale
Often overlooked but very important
Questions?
Thank you!
bhushanlodha@gmail.com

More Related Content

PDF
Building CI/CD Pipelines for Kubernetes
PDF
Serverless framework와 CircleCI를 통한 NoOps 맛보기
PPTX
Windows Containers and Docker: Why You Should Care
PPTX
The Velvet Revolution: Modernizing Traditional ASP.NET Apps with Docker
PDF
Containerize Your Game Server for the Best Multiplayer Experience
PDF
Testing as a container
PPTX
Олександр Лінивий — Multisite platform with continuous delivery process for m...
PPTX
NWJS. Web on desktop
Building CI/CD Pipelines for Kubernetes
Serverless framework와 CircleCI를 통한 NoOps 맛보기
Windows Containers and Docker: Why You Should Care
The Velvet Revolution: Modernizing Traditional ASP.NET Apps with Docker
Containerize Your Game Server for the Best Multiplayer Experience
Testing as a container
Олександр Лінивий — Multisite platform with continuous delivery process for m...
NWJS. Web on desktop

What's hot (20)

PDF
CD NYC From Source Code to Production
PDF
Preprocessor Workflow with Grunt
PDF
PuppetConf 2016: Keynote: Pulling the Strings to Containerize Your Life - Sco...
PPTX
TechDays NL 2017: The Hybrid Docker Swarm
PDF
Test Automation Infrastructure with Containers
PDF
Dev Ops
PDF
Grunt training deck
ODP
Version Controlling
PDF
Serverless
PPTX
Continuous Delivery With Selenium Grid And Docker
PDF
From Fortran on the Desktop to Kubernetes in the Cloud: A Windows Migration S...
PDF
PuppetConf 2016 Customer Keynote: Cloud, Containers & the Impact on IT - Jeff...
PPTX
CloudExpo 2018: Docker - Power Your Move to the Cloud
PPTX
IP EXPO London 2017: Modernizing Traditional Apps with Docker
PPTX
ScotSoft 2017: Why Containers Will Take Over the World
PPTX
PPTX
Javascript Frameworks (and How to Learn Them)
PPTX
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
PDF
Front-end development automation with Grunt
PDF
WebAssembly with Rust
CD NYC From Source Code to Production
Preprocessor Workflow with Grunt
PuppetConf 2016: Keynote: Pulling the Strings to Containerize Your Life - Sco...
TechDays NL 2017: The Hybrid Docker Swarm
Test Automation Infrastructure with Containers
Dev Ops
Grunt training deck
Version Controlling
Serverless
Continuous Delivery With Selenium Grid And Docker
From Fortran on the Desktop to Kubernetes in the Cloud: A Windows Migration S...
PuppetConf 2016 Customer Keynote: Cloud, Containers & the Impact on IT - Jeff...
CloudExpo 2018: Docker - Power Your Move to the Cloud
IP EXPO London 2017: Modernizing Traditional Apps with Docker
ScotSoft 2017: Why Containers Will Take Over the World
Javascript Frameworks (and How to Learn Them)
利用.NET Core 與 Azure Kubernetes Service (AKS) 建立高彈性 Microservices (Azure TechDay)
Front-end development automation with Grunt
WebAssembly with Rust
Ad

Similar to Dockerfile best practices (20)

PDF
DCEU 18: Building Your Development Pipeline
PPTX
Tips and best practices for Docker
PPTX
"Docker best practice", Станислав Коленкин (senior devops, DataArt)
PDF
Preparing your dockerised application for production deployment
PDF
TDC2016POA | Trilha Cloud Computing - Source-to-image - How to transform any ...
PDF
Docker Best Practices Workshop
PPTX
Develop with docker 2014 aug
PDF
Qualitätssicherung von Container-Images
PDF
Be a better developer with Docker (revision 3)
PDF
ContainerDayVietnam2016: Dockerize a small business
PDF
Distributed Docker Pipeline Architecture with CloudBees Jenkins Enterprise
PDF
DCSF 19 Building Your Development Pipeline
PPTX
IBM Container Service Overview
PPTX
Build optimization mechanisms in GitLab and Docker
PDF
Real-World Docker: 10 Things We've Learned
PDF
DCEU 18: Developing with Docker Containers
PDF
Build your first DApp using Substrate Framework - Part I
PPTX
Cloud native buildpacks-cncf
PDF
Serverless Container with Source2Image
PDF
Serverless containers … with source-to-image
DCEU 18: Building Your Development Pipeline
Tips and best practices for Docker
"Docker best practice", Станислав Коленкин (senior devops, DataArt)
Preparing your dockerised application for production deployment
TDC2016POA | Trilha Cloud Computing - Source-to-image - How to transform any ...
Docker Best Practices Workshop
Develop with docker 2014 aug
Qualitätssicherung von Container-Images
Be a better developer with Docker (revision 3)
ContainerDayVietnam2016: Dockerize a small business
Distributed Docker Pipeline Architecture with CloudBees Jenkins Enterprise
DCSF 19 Building Your Development Pipeline
IBM Container Service Overview
Build optimization mechanisms in GitLab and Docker
Real-World Docker: 10 Things We've Learned
DCEU 18: Developing with Docker Containers
Build your first DApp using Substrate Framework - Part I
Cloud native buildpacks-cncf
Serverless Container with Source2Image
Serverless containers … with source-to-image
Ad

Recently uploaded (20)

PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
System and Network Administration Chapter 2
PDF
Nekopoi APK 2025 free lastest update
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
medical staffing services at VALiNTRY
PPTX
L1 - Introduction to python Backend.pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Introduction to Artificial Intelligence
PDF
top salesforce developer skills in 2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
System and Network Administraation Chapter 3
2025 Textile ERP Trends: SAP, Odoo & Oracle
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PTS Company Brochure 2025 (1).pdf.......
System and Network Administration Chapter 2
Nekopoi APK 2025 free lastest update
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
medical staffing services at VALiNTRY
L1 - Introduction to python Backend.pptx
How to Migrate SBCGlobal Email to Yahoo Easily
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Introduction to Artificial Intelligence
top salesforce developer skills in 2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Which alternative to Crystal Reports is best for small or large businesses.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
System and Network Administraation Chapter 3

Dockerfile best practices