Building Smaller Container Images
...and more™
Adam Samalik
Senior Software Engineer
Scott McCarty
Principal Product Manager
1
CONFIDENTIAL Designator
Analysis
SERIOUS ANALYSIS
We started off with complaints that container images were too large, but analyzed the entire problem set from end to end.
2
What does a user really want when they ask for smaller container images?
ANALYSIS
3
Less Attack Surface
They want less total software in their environment which can be attacked.
Smaller Images
They believe this gives them less attack surface, but it’s a trap! Having 1000 different 5MB images is still 5GB of attack surface.
No Thinking
The user doesn’t want to have to think about attack surface. They want it built into the tools they are using.
Cool Tweets
They want to post on twitter how small their container images are. This will get earn them overt prestige among their peers!
Twitter: @adsamalik @fatherlinux
Things we looked at and tried
ANALYSIS
4
Scratch Container Builds
In the Container world, this is different than in the packaging world. Started with an empty directory and a package manager:
yum install --installroot /mnt/rootfs
Distroless
The claim is that smaller is better for security, but you’re pulling random binaries from The Internet
Minimizing dependencies
This innovation happens in Fedora, and will be inherited in RHEL.
Compile from scratch
Instead of leveraging packages from distributions
Less container images
A bunch of small images can still add to be larger than a few mid-sized images
Twitter: @adsamalik @fatherlinux
Scratch Container Builds
ANALYSIS
5
Twitter: @adsamalik @fatherlinux
Minimizing Dependencies
ANALYSIS
6
Twitter: @adsamalik @fatherlinux
Distroless
ANALYSIS
7
Cletis Hykes
Unemployed
Twitter: @adsamalik @fatherlinux
Compile From Scratch
ANALYSIS
8
Twitter: @adsamalik @fatherlinux
Less container images
BACKGROUND
9
Twitter: @adsamalik @fatherlinux
What if we could have the best?
BACKGROUND
10
High Quality Dependency Tree
Leverage existing linux distributions like Fedora/RHEL
Use Existing Tools
Use RPM and YUM, but not in the container
Minimize Dependencies Within Distro
Minimize hard dependencies within the Linux distribution
Single Linux Distribution
Minimize total attack surface within your organization
Smaller Images
Launched Red Hat Universal Base Image for RHEL 8 and RHEL 7
Twitter: @adsamalik @fatherlinux
Building
Smaller Images
11
12
examples are using Fedora
that's where the innovation happens
Twitter: @adsamalik @fatherlinux
13
Multi-stage builds
Main idea:
When you buy a car, you only get the car.
They won't ship the assembly line with it.
Our application:
Package manager is the assembly line.
Twitter: @adsamalik @fatherlinux
The package manager in Fedora (DNF) can:
14
Twitter: @adsamalik @fatherlinux
Fedora package ecosystem
has alternatives, such as:
glibc-minimal-langpack coreutils-single
15
Twitter: @adsamalik @fatherlinux
Building out a Dockerfile
16
Twitter: @adsamalik @fatherlinux
Building out a Dockerfile: nginx
17
FROM fedora:33
RUN dnf -y install nginx && \
dnf -y clean all
# ...
224 MB
Twitter: @adsamalik @fatherlinux
Building out a Dockerfile: nginx
18
FROM fedora:33 as build-env
RUN dnf -y --installroot /output --releasever 33 install nginx && \
dnf -y --installroot /output --releasever 33 clean all
FROM scratch
COPY --from=build-env /output /
# ...
450 MB (oh no!)
Twitter: @adsamalik @fatherlinux
Building out a Dockerfile: nginx
19
FROM fedora:33 as build-env
RUN dnf -y --setopt=install_weak_deps=false --nodocs \
--installroot /output --releasever 33 \
install glibc-minimal-langpack coreutils-single nginx && \
dnf -y --installroot /output --releasever 33 clean all
FROM scratch
COPY --from=build-env /output /
# ...
116 MB (oh yes!)
Twitter: @adsamalik @fatherlinux
20
FROM fedora:33
RUN dnf -y install nginx && \
dnf -y clean all
# ...
FROM fedora:33 as build-env
RUN mkdir /output
RUN dnf -y --installroot /output --releasever 33 \
--setopt=install_weak_deps=false --nodocs \
install glibc-minimal-langpack coreutils-single nginx && \
dnf -y --installroot /output --releasever 33 clean all
FROM scratch
COPY --from=build-env /output /
# ...
224 MB
116 MB
With a package manager
Without a package manager + a few other tricks
Twitter: @adsamalik @fatherlinux
Other example: "distroles" base
21
FROM fedora:33 as build-env
RUN dnf -y --setopt=install_weak_deps=false --nodocs \
--installroot /output --releasever 33 \
install glibc bash glibc-minimal-langpack coreutils-single && \
dnf -y --installroot /output --releasever 33 clean all
FROM scratch
COPY --from=build-env /output /
ENTRYPOINT /bin/bash
38MB
Twitter: @adsamalik @fatherlinux
Other example: busybox "distroless" base
22
FROM fedora:33 as build-env
RUN dnf -y --setopt=install_weak_deps=false --nodocs \
--installroot /output --releasever 33 \
install busybox && \
dnf -y --installroot /output --releasever 33 clean all
RUN mkdir /output/bin
RUN for i in $(/output/sbin/busybox --list); do ln -s /sbin/busybox \
/output/bin/$i; done
FROM scratch
COPY --from=build-env /output /
ENTRYPOINT /bin/sh
1.8MB
Twitter: @adsamalik @fatherlinux
Minimizing
Dependencies
23
24
Dependencies are complex!
Image shows Fedora repository
Minimization
is one of Fedora's main objectives.
25
26
f32
f33
Twitter: @adsamalik @fatherlinux
From https://tiny.distro.builders
27
From https://tiny.distro.builders
28
From https://tiny.distro.builders
29
From https://tiny.distro.builders
30
https://tiny.distro.builders
Conclusions
OPTIONAL SECTION MARKER OR TITLE
It’s important to use a distro and a package manager when using distroless distros :-P
32
Conclusions
OPTIONAL SECTION MARKER OR TITLE
33
Red Hat Universal Base Image (RHEL 7, 8, and 9)
Launched Red Hat Universal Base Image for RHEL 8 and RHEL 7. For production, we think standardizing on UBI makes sense to reduce total attack surface in an environment.
Distroless Images
Yes and no. We still think it makes sense to use the packages with the distro, but not include the package manager (RPM) and dependency manager (DNF) in the resultant container image
Minimal Dependency Tree
The work Adam and many teams are doing in Fedora will land in RHEL 8 and RHEL 9
Smaller Attack Surface
Combining some tricky build techniques (distroless/scratch), a minimized dependency tree, and high quality package content with rich security metadata is what it truly takes
Twitter: @adsamalik @fatherlinux
Thank you
Red Hat is the world’s leading provider of enterprise open source software solutions. Award-winning support, training, and consulting services make �Red Hat a trusted adviser to the Fortune 500.
34
CONFIDENTIAL Designator
linkedin.com/company/red-hat
youtube.com/user/RedHatVideos
facebook.com/redhatinc
twitter.com/RedHat