Docker Cookbook


Optimizing Your Dockerfile by Following Best Practices



Download 6,31 Mb.
Pdf ko'rish
bet47/260
Sana21.04.2022
Hajmi6,31 Mb.
#570749
1   ...   43   44   45   46   47   48   49   50   ...   260
Bog'liq
Docker Cookbook

2.5 Optimizing Your Dockerfile by Following Best Practices
Problem
You want to follow best practices to write your Dockerfiles and optimize your Docker
images.
2.5 Optimizing Your Dockerfile by Following Best Practices | 45


Solution
The Docker documentation has published 
best practices
 to write Dockerfiles. This
recipe highlights a few of them to put you on your way to building good images:
1. Run a single process per container. Although you can run multiple processes per
container (e.g., 
Recipe 1.15
), building images that will run only one process or at
least one functional service per container will help you build decoupled applica‐
tions that can scale. Take advantage of container linking (see 
Recipe 3.3
) or other
container-networking techniques (see 
Chapter 3
) to have the containers commu‐
nicate with each other.
2. Do not assume that your containers will live on; they are ephemeral and will be
stopped and restarted. You should treat them as immutable entities, which means
that you should not modify them but instead restart them from your base image.
Therefore, manage runtime configuration and data outside the containers and
hence the image. Use Docker volumes (see 
Recipe 1.18
 and 
Recipe 1.19
) for this.
3. Use a 
.dockerignore
file. When building images, Docker will copy the content of
the working directory where the Dockerfile exists (i.e., the 
build context
) inside
the image. Using 
.dockerignore
, you can exclude files and directories from being
copied during the build process. If you do not use a 
.dockerignore
file, make sure
to build your image in a directory that has only the minimum required. Check
the 
syntax
 for the 
.dockerignore
file.
4. Use 
official
images from Docker Hub instead of writing your own from scratch.
These images are maintained and blessed by the projects authoring the software.
You can also use 
ONBUILD
images (see 
Recipe 2.10
) to simplify even further your
images.
5. Finally, minimize the number of layers of your images and take advantage of the
image cache. Docker uses union filesystems to store images. This means that
each image is made of a base image plus a collection of 
diffs
that adds the
required changes. Each 
diff
represents an additional layer of an image. This has a
direct impact on how your write your Dockerfile and use the various directives.
The following section illustrates this point further.
Discussion
In 
Recipe 2.4
, you saw your first Dockerfile, which starts with the following direc‐
tives:
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y python
RUN apt-get install -y python-pip

Download 6,31 Mb.

Do'stlaringiz bilan baham:
1   ...   43   44   45   46   47   48   49   50   ...   260




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish