Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Building Smaller Container Images
#1
Building Smaller Container Images

Linux Containers have become a popular topic, making sure that a container image is not bigger than it should be is considered as a good practice. This article give some tips on how to create smaller Fedora container images.

microdnf


Fedora’s DNF is written in Python and and it’s designed to be extensible as it has wide range of plugins. But Fedora has an alternative base container image which uses an smaller package manager called microdnf written in C. To use this minimal image in a Dockerfile the FROM line should look like this:

FROM registry.fedoraproject.org/fedora-minimal:30

This is an important saving if your image does not need typical DNF dependencies like Python. For example, if you are making a NodeJS image.

Install and Clean up in one layer


To save space it’s important to remove repos meta data using dnf clean all or its microdnf equivalent microdnf clean all. But you should not do this in two steps because that would actually store those files in a container image layer then mark them for deletion in another layer. To do it properly you should do the installation and cleanup in one step like this

FROM registry.fedoraproject.org/fedora-minimal:30 
RUN microdnf install nodejs && microdnf clean all

Modularity with microdnf


Modularity is a way to offer you different versions of a stack to choose from. For example you might want non-LTS NodeJS version 11 for a project and old LTS NodeJS version 8 for another and latest LTS NodeJS version 10 for another. You can specify which stream using colon

# dnf module list 
# dnf module install nodejs:8

The dnf module install command implies two commands one that enables the stream and one that install nodejs from it.

# dnf module enable nodejs:8 
# dnf install nodejs

Although microdnf does not offer any command related to modularity, it is possible to enable a module with a configuation file, and libdnf (which microdnf uses) seems to support modularity streams. The file looks like this

/etc/dnf/modules.d/nodejs.module 
[nodejs]
name=nodejs
stream=8
profiles=
state=enabled

A full Dockerfile using modularity with microdnf looks like this:

FROM registry.fedoraproject.org/fedora-minimal:30 
RUN \
echo -e "[nodejs]\nname=nodejs\nstream=8\nprofiles=\nstate=enabled\n" > /etc/dnf/modules.d/nodejs.module && \
microdnf install nodejs zopfli findutils busybox && \
microdnf clean all

Multi-staged builds


In many cases you might have tons of build-time dependencies that are not needed to run the software for example building a Go binary, which statically link dependencies. Multi-stage build are an efficient way to separate the application build and the application runtime.

For example the Dockerfile below builds confd a Go application.

# building container 
FROM registry.fedoraproject.org/fedora-minimal AS build
RUN mkdir /go && microdnf install golang && microdnf clean all
WORKDIR /go
RUN export GOPATH=/go; CGO_ENABLED=0 go get github.com/kelseyhightower/confd

FROM registry.fedoraproject.org/fedora-minimal
WORKDIR /
COPY --from=build /go/bin/confd /usr/local/bin
CMD ["confd"]

The multi-stage build is done by adding AS after the FROM instruction and by having another FROM from a base container image then using COPY –from= instruction to copy content from the build container to the second container.

This Dockerfile can then be built and run using podman

$ podman build -t myconfd .
$ podman run -it myconfd


Forum Jump:


Users browsing this thread: 1 Guest(s)

Forum stats
Latest posts
Topic Date, time  Author Last Sender Forum
  Black Ops 2 Box and Name ... 08-18, 10:36 xSicKx pantsground Modified Gami...
  HPE Buys MapR Assets 08-08, 21:13 xSicKxBot xSicKxBot Linux, FreeBS...
  Fedora - Trace code in Fe... 08-08, 21:13 xSicKxBot xSicKxBot Linux, FreeBS...
  Universal Sprite Sheet Ch... 08-08, 21:13 xSicKxBot xSicKxBot Game Developm...
  News - The Original Ittle... 08-08, 21:13 xSicKxBot xSicKxBot Nintendo Disc...
  Mobile - Tencent expands ... 08-08, 21:13 xSicKxBot xSicKxBot New Releases
  XONE - Streets of Rogue 08-08, 21:13 xSicKxBot xSicKxBot New Releases
  XONE - Solo: Islands of t... 08-08, 21:13 xSicKxBot xSicKxBot New Releases
  PC - Automachef 08-08, 21:13 xSicKxBot xSicKxBot New Releases
  PC - Silver Chains 08-08, 21:13 xSicKxBot xSicKxBot New Releases
  Steam - Pre-Purchase Now ... 08-08, 21:13 xSicKxBot xSicKxBot PC Discussion
Most views
  Rules of Sick... 423374
  (Read This Be... 263935
  exProfileEdit... 33114
  Custom PS3 Fi... 30747
  ULX Commands ... 15675
Most reputations
xSicKx 13
Senpai Shameless 2
SicKdirtySanchez 2
adriankzsr 1
76561198282553414 1
Most replies
  exProfileEdit... 66
  Black Ops 2 B... 45
  (PC) Battlefi... 24
  Custom PS3 Fi... 21
  Grand Theft A... 20
Top posters
xSicKxBot 8859
xSicKx 383
gaosuo1234 26
dengdai 15
SicKdirtySanchez 8
Top referrers
xSicKx 10
SicKdirtySanchez 4
sick1022 4
Tiger 2
ps3d3dri6 1
Newest members
pantsground 08-18
hestiacx 08-18
beardyjuan 08-16
Letitia Baldwin 08-15
zlTraqZz 08-06