understanding and optimizing dockerfiles - ritesh modi - microsoft
TRANSCRIPT
Dockerfile- OptimizationRitesh ModiSenior Technology Evangelist@automationnext
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
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" ]
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
Image Layers
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
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
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
Minimize Layer Size
Add and Copy files that are needed.
Minimal packages and Libraries
Remove and/or cleanup after installation
Optimizing speedOrdering Instruction
Make efficient use of cache
Ideal for scenario’s with multiple development iteration
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
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.
FinallyEnsure you are writing Production ready DockerfilesBy optimizing itUsing best practicesTest for accuracy, speed and size
Happy Coding!!!
Thank you and Questions
Stay connected:@automationnext
© 2014 Microsoft Corporation. All rights reserved.