SlideShare una empresa de Scribd logo
1 de 54
Descargar para leer sin conexión
ABBY FULLER
Developer Relations, AWS
Creating effective
container images: the
remix
v
How do layers work?
What are container layers?
Thin read-write layer
Read-only container layers
More layers mean a larger image. The larger the image, the
longer that it takes to both build, and push and pull from a
registry.
Smaller images mean faster builds and deploys. This also
means a smaller attack surface.
Why do I care how many layers I have?
Sharing is caring
• Use shared base images wherever
possible
• Limit the data written to the
container layer
• Chain RUN statements
• Prevent cache misses at build for
as long as possible
OK, so how can I reduce my layers?
v
Building minimal images: the
basics
A Dockerfile is a series of
instructions for building an
image.
The size of the base image matters
FROM ubuntu:latest
LABEL maintainer abbyfull@amazon.com
RUN apt-get update --y && apt-get install --
y python-pip python-dev build-essential
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT [“python”]
CMD [“application.py”]
This is fine, but we can improve
First step: choose the right base
Ubuntu latest:
ubuntu latest 113a43faa138 42 hours ago 81.2MB
Alpine latest:
alpine latest 3fd9065eaf02 4 months ago 4.15MB
Slightly better: choose a different distro
python latest a5b7afcfdcc8 21 hours ago 912MB
ubuntu latest 113a43faa138 43 hours ago 81.2MB
debian latest 8626492fecd3 5 weeks ago 101MB
alpine latest 3fd9065eaf02 4 months ago 4.15MB
golang latest 3f30f1fc3c43 38 hours ago 794MB
ruby latest 857bc7ff918f 17 hours ago 869MB
When do I want a full base OS?
Security!
Compliance!
Ease of development!
More features (package managers!)
I have a Dockerfile, now what?
Once you have your Dockerfile, you can build it! That’s how we’ll
a) run our containerized application, and b) see how big it is once
we make our changes.
$ docker build -t hi-dockercon .
Build
command
--tag
flag
Name
assigned
with tag
PATH to
build
F is for flags
There are a few flags that affect your final image size when you’re
building your image.
--cache-from (another image to cache from)
--compress (compress build context* with gzip)
--no-cache (ignore the cache, more on that in a sec)
--squash (squash new layers into a single layer)**
* don’t know what build context is? I didn’t either. We’ll talk about it.
**this is still experimental
What’s build context?
Once you call docker build, your current directory is the
build context, and gets sent to the Docker daemon. You can see
how much context your sending by looking for a message like
this:
Sending build context to Docker daemon 187.8MB
The more context you send, the bigger your build context will be,
and the larger your image. Avoid including unnecessary files and
directories.
CACHE
But first, some FAQs
Q: Is that really the official Wu-Tang font?
A: Yes. Of course. Insist on the highest standards.
Q: What’s your favorite Wu-Tang song?
A: “Protect Ya Neck”
Q: Have you been sued for copyright infringement yet for your
last slide?
A: No, but don’t tell on me.
Back to cache!
Starting from the parent instruction, Docker will look at each following
instruction to see if it matches the cached version.
Only ADD and COPY will look at checksums for a match
Other than ADD and COPY, only the string of the command is used,
not the contents of the files.
Once cache is broken, every subsequent layer is built again.
v
So let’s talk about how we can make
an image better
Here’s our original Ubuntu image again
FROM ubuntu:latest
LABEL maintainer abbyfull@amazon.com
RUN apt-get update --y && apt-get install --
y python-pip python-dev build-essential
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT [“python”]
CMD [“application.py”]
A few small changes
FROM python:2.7-alpine
LABEL maintainer abbyfull@amazon.com
COPY . /app
WORKDIR /app
RUN pip install –r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["application.py"]
Fewer cache invalidations=smaller images
FROM python:2.7-alpine
LABEL maintainer abbyfull@amazon.com
COPY requirements.txt /app
RUN pip install –r /app/requirements.txt
COPY . /app
WORKDIR /app
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["application.py"]
Use RUN statements effectively
RUN apt-get update && apt-get install -y 
aufs-tools 
automake 
build-essential 
ruby1.9.1 
ruby1.9.1-dev 
s3cmd=1.1.* 
&& rm -rf /var/lib/apt/lists/*
Switching USER adds layers
RUN groupadd –r dockercon && useradd –r –g
dockercon dockercon
USER dockercon
RUN apt-get update && apt-get install -y 
aufs-tools 
automake 
build-essential
USER root
COPY . /app
v
More stages, more fun!
Keeping image sizes down is challenging (or I wouldn’t be here!). Two
common patterns:
Multi-stage builds (new-ish!): separate stages in one (1) Dockerfile. Copy
artifacts from one stage to the other. This results in a smaller final image.
Separate Dockerfiles: build, then copy artifacts over. This results in a
smaller image, but it’s a little more complicated. End result is often
something like Dockerfile.build and Dockerfile.final
Multi-stage builds vs multiple Dockerfiles
FROM ubuntu AS build-env
RUN apt-get install make
ADD . /src
RUN cd /src && make
And for the second Dockerfile, copy from #1:
FROM busybox
COPY --from=build-env /src/build/app /usr/local/bin/app
EXPOSE 80
ENTRYPOINT /usr/local/bin/app
Multi-stage builds
Still not sure on multi-stage build? I really enjoy this blog post
from Alex Ellis, which also provides the examples for multi-
stage builds in the (excellent) Docker documentation.
I’m still skeptical!
v
Docker: not just for Linux!
Convert an existing Windows image:
ConvertTo-Dockerfile -ImagePath c:dockermyimage.wim
Convert from VHD:
ConvertTo-Dockerfile -ImagePath c:vmstest.vhd -Artifact IIS -ArtifactParam
windows-container -OutputPath c:windows-container
cd c:windows-container
docker build -t windows-container .
docker run -d -p 80:80 windows-container
Port over existing VM workloads
Watch what you build:
c: c: / /windows c:/windows
Building any of those PATHs will make your image very
large!
Careful what you build
“Hey! Listen!”
MSI installations are not space efficient. This is not the same as Linux distros,
where you can add, use, and remove the installation files!
$ Windows/Installer/<package>.msi
Windows saves these files for uninstalls :(
Avoid installing with MSI
Build and run everything the same, regardless of container OS, host OS, or tools.
Just docker build and docker run.
Here's what's really cool though…
v
Let’s get language specific
A few things to think about
Use the right tool: not every language needs to be built the same
way.
Where possible, use two images: one to build an artifact, and one
from base
Official language images can be big: more space effective to use a
more minimal image, but there are tradeoffs
Golang
Compile, then COPY binary:
$ go build -o dockercon .
$ docker build -t dockercon .
Dockerfile:
FROM scratch
COPY ./dockercon /dockercon
ENTRYPOINT ["/dockercon"]
Hang on, what’s SCRATCH?
Special, empty Dockerfile.
Use this to build your own base images.
Or, use to build minimal images that run a binary and nothing else:
FROM scratch
COPY hello /
CMD [ “/hello” ]
Want more on scratch? Start here.
Ruby
Ruby images can be extra huge. A new base + a little extra work pays off.
FROM alpine:3.2
LABEL maintainer abbyfull@amazon.com
RUN apk update && apk upgrade && apk add 
curl 
bashruby 
ruby-dev 
ruby-bundler
RUN rm -rf /var/cache/apk/*
Node.js
If you love yourself, .dockerignore npm-debug.log. Seriously.
But most importantly, cache node_modules:
COPY package.json .
RUN npm install --production
COPY . .
This way, only run npm install if package.json changes.
Java
Multi-stage builds are your friend:
FROM maven:3.5-jdk-8 as BUILD
COPY --from=BUILD
Like Golang, this let’s you build an artifact in one stage, and simply run
the binary in the second stage, resulting in more minimal final images.
More on multistage builds up next.
v
Let’s look at what we just learned in
practice
AKA: steal Brent’s
Dockerfile!
Let’s look at the original
$ docker build –t client .
$ docker images | grep client
client latest 772483ec773e time 568MB
Swap out the base image for a quick win
Change from ubuntu:14.04 to debian:stretch-slim
$ docker build –f Dockerfile-slim –t client-slim .
$ docker images | grep client
client latest 772483ec773e time 568MB
client-slim latest a72f5121de6e time 388MB
Or, change to a slimmer base image
Swap to python:2-alpine
$ docker build –f Dockerfile-alpine –t client-alpine .
$ docker images | grep client
client latest 772483ec773e time 568MB
client-slim latest a72f5121de6e time 388MB
client-alpine latest 02bbb5c5a079 time 300MB
Remove pip cache and extra layers
$ docker build –f Dockerfile-alpine-2 –t client-alpine-2 .
$ docker images | grep client
client latest 772483ec773e time 568MB
client-slim latest a72f5121de6e time 388MB
client-alpine latest 02bbb5c5a079 time 300MB
client-alpine-2 latest cd3e206844b6 time 275MB
Combine RUN statements
$ docker build –f Dockerfile-alpine-3 –t client-alpine-3 .
$ docker images | grep client
client latest 772483ec773e time 568MB
client-slim latest a72f5121de6e time 388MB
client-alpine latest 02bbb5c5a079 time 300MB
client-alpine-2 latest cd3e206844b6 time 275MB
client-alpine-3 latest 83fffeb85684 time 162MB
Combine RUN statements
$ docker build –f Dockerfile-alpine-3 –t client-alpine-3 .
$ docker images | grep client
client latest 772483ec773e time 568MB
client-slim latest a72f5121de6e time 388MB
client-alpine latest 02bbb5c5a079 time 300MB
client-alpine-2 latest cd3e206844b6 time 275MB
client-alpine-3 latest 83fffeb85684 time 162MB
v
But it’s not just about image sizes
Minimal images only matter so much
Docker image prune:
$ docker image prune –a
Or go even further with Docker system prune:
$ docker system prune -a
Safety first!
Lean on tools to check your images for vulnerabilities.
A couple of options, both paid and open source:
• Aqua MicroScanner (community edition)
• Aqua continuous image assurance
• Docker Security Scan with Trusted Registry
• Clair from CoreOS
The importance of garbage collection
Clean up after your containers! Beyond image and system prune:
• Make sure your orchestration platform (like ECS or K8s) is garbage
collecting:
• ECS
• Kubernetes
• 3rd party tools like spotify-gc
So what did we learn?
One takeaway: less layers is more.
Share layers where possible
Choose or build your base wisely
Not all languages should build the same
Keep it simple, avoid extras
Tools are here to help
v
Thanks!
@abbyfuller

Más contenido relacionado

Más de Docker, Inc.

Build & Deploy Multi-Container Applications to AWS
Build & Deploy Multi-Container Applications to AWSBuild & Deploy Multi-Container Applications to AWS
Build & Deploy Multi-Container Applications to AWS
Docker, Inc.
 
Virtual Meetup Docker + Arm: Building Multi-arch Apps with Buildx
Virtual Meetup Docker + Arm: Building Multi-arch Apps with BuildxVirtual Meetup Docker + Arm: Building Multi-arch Apps with Buildx
Virtual Meetup Docker + Arm: Building Multi-arch Apps with Buildx
Docker, Inc.
 

Más de Docker, Inc. (20)

Monitoring in a Microservices World
Monitoring in a Microservices WorldMonitoring in a Microservices World
Monitoring in a Microservices World
 
COVID-19 in Italy: How Docker is Helping the Biggest Italian IT Company Conti...
COVID-19 in Italy: How Docker is Helping the Biggest Italian IT Company Conti...COVID-19 in Italy: How Docker is Helping the Biggest Italian IT Company Conti...
COVID-19 in Italy: How Docker is Helping the Biggest Italian IT Company Conti...
 
Predicting Space Weather with Docker
Predicting Space Weather with DockerPredicting Space Weather with Docker
Predicting Space Weather with Docker
 
Become a Docker Power User With Microsoft Visual Studio Code
Become a Docker Power User With Microsoft Visual Studio CodeBecome a Docker Power User With Microsoft Visual Studio Code
Become a Docker Power User With Microsoft Visual Studio Code
 
How to Use Mirroring and Caching to Optimize your Container Registry
How to Use Mirroring and Caching to Optimize your Container RegistryHow to Use Mirroring and Caching to Optimize your Container Registry
How to Use Mirroring and Caching to Optimize your Container Registry
 
Monolithic to Microservices + Docker = SDLC on Steroids!
Monolithic to Microservices + Docker = SDLC on Steroids!Monolithic to Microservices + Docker = SDLC on Steroids!
Monolithic to Microservices + Docker = SDLC on Steroids!
 
Kubernetes at Datadog Scale
Kubernetes at Datadog ScaleKubernetes at Datadog Scale
Kubernetes at Datadog Scale
 
Labels, Labels, Labels
Labels, Labels, Labels Labels, Labels, Labels
Labels, Labels, Labels
 
Using Docker Hub at Scale to Support Micro Focus' Delivery and Deployment Model
Using Docker Hub at Scale to Support Micro Focus' Delivery and Deployment ModelUsing Docker Hub at Scale to Support Micro Focus' Delivery and Deployment Model
Using Docker Hub at Scale to Support Micro Focus' Delivery and Deployment Model
 
Build & Deploy Multi-Container Applications to AWS
Build & Deploy Multi-Container Applications to AWSBuild & Deploy Multi-Container Applications to AWS
Build & Deploy Multi-Container Applications to AWS
 
From Fortran on the Desktop to Kubernetes in the Cloud: A Windows Migration S...
From Fortran on the Desktop to Kubernetes in the Cloud: A Windows Migration S...From Fortran on the Desktop to Kubernetes in the Cloud: A Windows Migration S...
From Fortran on the Desktop to Kubernetes in the Cloud: A Windows Migration S...
 
Developing with Docker for the Arm Architecture
Developing with Docker for the Arm ArchitectureDeveloping with Docker for the Arm Architecture
Developing with Docker for the Arm Architecture
 
Sharing is Caring: How to Begin Speaking at Conferences
Sharing is Caring: How to Begin Speaking at ConferencesSharing is Caring: How to Begin Speaking at Conferences
Sharing is Caring: How to Begin Speaking at Conferences
 
Virtual Meetup Docker + Arm: Building Multi-arch Apps with Buildx
Virtual Meetup Docker + Arm: Building Multi-arch Apps with BuildxVirtual Meetup Docker + Arm: Building Multi-arch Apps with Buildx
Virtual Meetup Docker + Arm: Building Multi-arch Apps with Buildx
 
DCSF 19 How Entergy is Mitigating Legacy Windows Operating System Vulnerabili...
DCSF 19 How Entergy is Mitigating Legacy Windows Operating System Vulnerabili...DCSF 19 How Entergy is Mitigating Legacy Windows Operating System Vulnerabili...
DCSF 19 How Entergy is Mitigating Legacy Windows Operating System Vulnerabili...
 
DCSF 19 Developing Apps with Containers, Functions and Cloud Services
DCSF 19 Developing Apps with Containers, Functions and Cloud ServicesDCSF 19 Developing Apps with Containers, Functions and Cloud Services
DCSF 19 Developing Apps with Containers, Functions and Cloud Services
 
DCSF 19 eBPF Superpowers
DCSF 19 eBPF SuperpowersDCSF 19 eBPF Superpowers
DCSF 19 eBPF Superpowers
 
DCSF 19 Zero Trust Networks Come to Enterprise Kubernetes
DCSF 19 Zero Trust Networks Come to Enterprise KubernetesDCSF 19 Zero Trust Networks Come to Enterprise Kubernetes
DCSF 19 Zero Trust Networks Come to Enterprise Kubernetes
 
DCSF 19 Node.js Rocks in Docker for Dev and Ops
DCSF 19 Node.js Rocks in Docker for Dev and OpsDCSF 19 Node.js Rocks in Docker for Dev and Ops
DCSF 19 Node.js Rocks in Docker for Dev and Ops
 
DCSF19 Containers for Beginners
DCSF19 Containers for BeginnersDCSF19 Containers for Beginners
DCSF19 Containers for Beginners
 

Último

Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
amilabibi1
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
Kayode Fayemi
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
Kayode Fayemi
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
raffaeleoman
 

Último (18)

ICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdfICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdf
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Bailey
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
 
Causes of poverty in France presentation.pptx
Causes of poverty in France presentation.pptxCauses of poverty in France presentation.pptx
Causes of poverty in France presentation.pptx
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.
 
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
 
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verifiedSector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
Sector 62, Noida Call girls :8448380779 Noida Escorts | 100% verified
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
 
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of Drupal
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatment
 

Creating effective docker images

  • 1. ABBY FULLER Developer Relations, AWS Creating effective container images: the remix
  • 3. What are container layers? Thin read-write layer Read-only container layers
  • 4. More layers mean a larger image. The larger the image, the longer that it takes to both build, and push and pull from a registry. Smaller images mean faster builds and deploys. This also means a smaller attack surface. Why do I care how many layers I have?
  • 5. Sharing is caring • Use shared base images wherever possible • Limit the data written to the container layer • Chain RUN statements • Prevent cache misses at build for as long as possible OK, so how can I reduce my layers?
  • 7. A Dockerfile is a series of instructions for building an image.
  • 8. The size of the base image matters FROM ubuntu:latest LABEL maintainer abbyfull@amazon.com RUN apt-get update --y && apt-get install -- y python-pip python-dev build-essential COPY . /app WORKDIR /app RUN pip install -r requirements.txt EXPOSE 5000 ENTRYPOINT [“python”] CMD [“application.py”]
  • 9. This is fine, but we can improve
  • 10. First step: choose the right base Ubuntu latest: ubuntu latest 113a43faa138 42 hours ago 81.2MB Alpine latest: alpine latest 3fd9065eaf02 4 months ago 4.15MB
  • 11. Slightly better: choose a different distro python latest a5b7afcfdcc8 21 hours ago 912MB ubuntu latest 113a43faa138 43 hours ago 81.2MB debian latest 8626492fecd3 5 weeks ago 101MB alpine latest 3fd9065eaf02 4 months ago 4.15MB golang latest 3f30f1fc3c43 38 hours ago 794MB ruby latest 857bc7ff918f 17 hours ago 869MB
  • 12. When do I want a full base OS? Security! Compliance! Ease of development! More features (package managers!)
  • 13. I have a Dockerfile, now what? Once you have your Dockerfile, you can build it! That’s how we’ll a) run our containerized application, and b) see how big it is once we make our changes. $ docker build -t hi-dockercon . Build command --tag flag Name assigned with tag PATH to build
  • 14. F is for flags There are a few flags that affect your final image size when you’re building your image. --cache-from (another image to cache from) --compress (compress build context* with gzip) --no-cache (ignore the cache, more on that in a sec) --squash (squash new layers into a single layer)** * don’t know what build context is? I didn’t either. We’ll talk about it. **this is still experimental
  • 15. What’s build context? Once you call docker build, your current directory is the build context, and gets sent to the Docker daemon. You can see how much context your sending by looking for a message like this: Sending build context to Docker daemon 187.8MB The more context you send, the bigger your build context will be, and the larger your image. Avoid including unnecessary files and directories.
  • 16. CACHE
  • 17. But first, some FAQs Q: Is that really the official Wu-Tang font? A: Yes. Of course. Insist on the highest standards. Q: What’s your favorite Wu-Tang song? A: “Protect Ya Neck” Q: Have you been sued for copyright infringement yet for your last slide? A: No, but don’t tell on me.
  • 18. Back to cache! Starting from the parent instruction, Docker will look at each following instruction to see if it matches the cached version. Only ADD and COPY will look at checksums for a match Other than ADD and COPY, only the string of the command is used, not the contents of the files. Once cache is broken, every subsequent layer is built again.
  • 19. v So let’s talk about how we can make an image better
  • 20. Here’s our original Ubuntu image again FROM ubuntu:latest LABEL maintainer abbyfull@amazon.com RUN apt-get update --y && apt-get install -- y python-pip python-dev build-essential COPY . /app WORKDIR /app RUN pip install -r requirements.txt EXPOSE 5000 ENTRYPOINT [“python”] CMD [“application.py”]
  • 21. A few small changes FROM python:2.7-alpine LABEL maintainer abbyfull@amazon.com COPY . /app WORKDIR /app RUN pip install –r requirements.txt EXPOSE 5000 ENTRYPOINT ["python"] CMD ["application.py"]
  • 22. Fewer cache invalidations=smaller images FROM python:2.7-alpine LABEL maintainer abbyfull@amazon.com COPY requirements.txt /app RUN pip install –r /app/requirements.txt COPY . /app WORKDIR /app EXPOSE 5000 ENTRYPOINT ["python"] CMD ["application.py"]
  • 23. Use RUN statements effectively RUN apt-get update && apt-get install -y aufs-tools automake build-essential ruby1.9.1 ruby1.9.1-dev s3cmd=1.1.* && rm -rf /var/lib/apt/lists/*
  • 24. Switching USER adds layers RUN groupadd –r dockercon && useradd –r –g dockercon dockercon USER dockercon RUN apt-get update && apt-get install -y aufs-tools automake build-essential USER root COPY . /app
  • 26. Keeping image sizes down is challenging (or I wouldn’t be here!). Two common patterns: Multi-stage builds (new-ish!): separate stages in one (1) Dockerfile. Copy artifacts from one stage to the other. This results in a smaller final image. Separate Dockerfiles: build, then copy artifacts over. This results in a smaller image, but it’s a little more complicated. End result is often something like Dockerfile.build and Dockerfile.final Multi-stage builds vs multiple Dockerfiles
  • 27. FROM ubuntu AS build-env RUN apt-get install make ADD . /src RUN cd /src && make And for the second Dockerfile, copy from #1: FROM busybox COPY --from=build-env /src/build/app /usr/local/bin/app EXPOSE 80 ENTRYPOINT /usr/local/bin/app Multi-stage builds
  • 28. Still not sure on multi-stage build? I really enjoy this blog post from Alex Ellis, which also provides the examples for multi- stage builds in the (excellent) Docker documentation. I’m still skeptical!
  • 29. v Docker: not just for Linux!
  • 30. Convert an existing Windows image: ConvertTo-Dockerfile -ImagePath c:dockermyimage.wim Convert from VHD: ConvertTo-Dockerfile -ImagePath c:vmstest.vhd -Artifact IIS -ArtifactParam windows-container -OutputPath c:windows-container cd c:windows-container docker build -t windows-container . docker run -d -p 80:80 windows-container Port over existing VM workloads
  • 31. Watch what you build: c: c: / /windows c:/windows Building any of those PATHs will make your image very large! Careful what you build “Hey! Listen!”
  • 32. MSI installations are not space efficient. This is not the same as Linux distros, where you can add, use, and remove the installation files! $ Windows/Installer/<package>.msi Windows saves these files for uninstalls :( Avoid installing with MSI
  • 33. Build and run everything the same, regardless of container OS, host OS, or tools. Just docker build and docker run. Here's what's really cool though…
  • 35. A few things to think about Use the right tool: not every language needs to be built the same way. Where possible, use two images: one to build an artifact, and one from base Official language images can be big: more space effective to use a more minimal image, but there are tradeoffs
  • 36. Golang Compile, then COPY binary: $ go build -o dockercon . $ docker build -t dockercon . Dockerfile: FROM scratch COPY ./dockercon /dockercon ENTRYPOINT ["/dockercon"]
  • 37. Hang on, what’s SCRATCH? Special, empty Dockerfile. Use this to build your own base images. Or, use to build minimal images that run a binary and nothing else: FROM scratch COPY hello / CMD [ “/hello” ] Want more on scratch? Start here.
  • 38. Ruby Ruby images can be extra huge. A new base + a little extra work pays off. FROM alpine:3.2 LABEL maintainer abbyfull@amazon.com RUN apk update && apk upgrade && apk add curl bashruby ruby-dev ruby-bundler RUN rm -rf /var/cache/apk/*
  • 39. Node.js If you love yourself, .dockerignore npm-debug.log. Seriously. But most importantly, cache node_modules: COPY package.json . RUN npm install --production COPY . . This way, only run npm install if package.json changes.
  • 40. Java Multi-stage builds are your friend: FROM maven:3.5-jdk-8 as BUILD COPY --from=BUILD Like Golang, this let’s you build an artifact in one stage, and simply run the binary in the second stage, resulting in more minimal final images. More on multistage builds up next.
  • 41. v Let’s look at what we just learned in practice
  • 43. Let’s look at the original $ docker build –t client . $ docker images | grep client client latest 772483ec773e time 568MB
  • 44. Swap out the base image for a quick win Change from ubuntu:14.04 to debian:stretch-slim $ docker build –f Dockerfile-slim –t client-slim . $ docker images | grep client client latest 772483ec773e time 568MB client-slim latest a72f5121de6e time 388MB
  • 45. Or, change to a slimmer base image Swap to python:2-alpine $ docker build –f Dockerfile-alpine –t client-alpine . $ docker images | grep client client latest 772483ec773e time 568MB client-slim latest a72f5121de6e time 388MB client-alpine latest 02bbb5c5a079 time 300MB
  • 46. Remove pip cache and extra layers $ docker build –f Dockerfile-alpine-2 –t client-alpine-2 . $ docker images | grep client client latest 772483ec773e time 568MB client-slim latest a72f5121de6e time 388MB client-alpine latest 02bbb5c5a079 time 300MB client-alpine-2 latest cd3e206844b6 time 275MB
  • 47. Combine RUN statements $ docker build –f Dockerfile-alpine-3 –t client-alpine-3 . $ docker images | grep client client latest 772483ec773e time 568MB client-slim latest a72f5121de6e time 388MB client-alpine latest 02bbb5c5a079 time 300MB client-alpine-2 latest cd3e206844b6 time 275MB client-alpine-3 latest 83fffeb85684 time 162MB
  • 48. Combine RUN statements $ docker build –f Dockerfile-alpine-3 –t client-alpine-3 . $ docker images | grep client client latest 772483ec773e time 568MB client-slim latest a72f5121de6e time 388MB client-alpine latest 02bbb5c5a079 time 300MB client-alpine-2 latest cd3e206844b6 time 275MB client-alpine-3 latest 83fffeb85684 time 162MB
  • 49. v But it’s not just about image sizes
  • 50. Minimal images only matter so much Docker image prune: $ docker image prune –a Or go even further with Docker system prune: $ docker system prune -a
  • 51. Safety first! Lean on tools to check your images for vulnerabilities. A couple of options, both paid and open source: • Aqua MicroScanner (community edition) • Aqua continuous image assurance • Docker Security Scan with Trusted Registry • Clair from CoreOS
  • 52. The importance of garbage collection Clean up after your containers! Beyond image and system prune: • Make sure your orchestration platform (like ECS or K8s) is garbage collecting: • ECS • Kubernetes • 3rd party tools like spotify-gc
  • 53. So what did we learn? One takeaway: less layers is more. Share layers where possible Choose or build your base wisely Not all languages should build the same Keep it simple, avoid extras Tools are here to help