understanding and optimizing dockerfiles - ritesh modi - microsoft

16
Dockerfile- Optimization Ritesh Modi Senior Technology Evangelist @automationnext

Upload: bangalore-container-conference-2017

Post on 15-Apr-2017

251 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Dockerfile- OptimizationRitesh ModiSenior Technology Evangelist@automationnext

Page 2: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Understanding dockerfileHelps in building custom images

Provides full control over image content

Easily readable text file

Series of commands and instructions

Sequential execution

Input to docker build

Page 3: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Docker BuildImage LayersContainers composed of multiple image layers

DockerfileEach actionable command generates a new layer

This code contains 4 layers of imageFROM windowsservercore RUN dism /online /enable-feature /all /featurename:iis-webserver /NoRestart RUN echo "Hello World - Dockerfile" > c:\inetpub\wwwroot\index.html CMD [ "cmd" ]

Page 4: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Creating new imagesA context is created comprising of all artifacts in current folder

Context is passed to Docker Engine

Dockerfile can reference these artifacts

Use them for coping, deployment and configuration of image

Page 5: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Image Layers

Page 6: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

General dockerfile

FROM microsoft/windowsservercore

RUN powershell.exe -Command Invoke-WebRequest "https://www.python.org/ftp/python/3.5.1/python-3.5.1.exe" -OutFile c:\python-3.5.1.exe

RUN powershell.exe -Command Start-Process c:\python-3.5.1.exe -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1' -Wait

RUN powershell.exe -Command Remove-Item c:\python-3.5.1.exe -Force

Page 7: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Minimizing number of layersGrouping help create single image layer for multiple instruction.

Grouping help create single image layer for multiple instruction.

Group commands that form a logical pair

Also think grouping from layer and cache viewpoint

Page 8: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

GroupingFROM windowsservercore

RUN powershell.exe -Command \

$ErrorActionPreference = 'Stop'; \

Invoke-WebRequest https://www.python.org/ftp/python/3.5.1/python-3.5.1.exe -OutFile c:\python-3.5.1.exe ; \

Start-Process c:\python-3.5.1.exe -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1' -Wait ; \

Remove-Item c:\python-3.5.1.exe -Force

Page 9: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Minimize Layer Size

Add and Copy files that are needed.

Minimal packages and Libraries

Remove and/or cleanup after installation

Page 10: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Optimizing speedOrdering Instruction

Make efficient use of cache

Ideal for scenario’s with multiple development iteration

Page 11: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

OrderingEach Instruction compared against cached layers

Place instructions that will remain constant towards the top of the Dockerfile

Place instructions that may change towards the bottom of the Dockerfile

Add files towards the end that changes frequently

Page 12: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

More..Do not neglect .dockerignore file

Helps in reducing the context size

While Grouping multiple statements, use multi-line syntax

Helps in increasing readability

Keep consistent Instruction case. May be use upper case

Files are added using their CheckSum. A change in file content will invalidate cache

Parameterize your dockerfiles.

Page 13: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

FinallyEnsure you are writing Production ready DockerfilesBy optimizing itUsing best practicesTest for accuracy, speed and size

Happy Coding!!!

Page 14: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft
Page 15: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

Thank you and Questions

Stay connected:@automationnext

Page 16: Understanding and Optimizing Dockerfiles - Ritesh Modi - Microsoft

© 2014 Microsoft Corporation. All rights reserved.