Docker Cookbook


| Chapter 2: Image Creation and Sharing



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

46 | Chapter 2: Image Creation and Sharing


RUN apt-get clean
RUN pip install flask
ADD hello.py /tmp/hello.py
...
It contains several faux-pas that illustrate a couple of best practices to follow instead.
The fact that it uses the 
ubuntu:14.04
official image is good. However, you then pro‐
ceeded to install a few packages using multiple 
RUN
commands. This is bad practice,
as it will add unnecessary layers to the image. You also used the 
ADD
command to
copy a simple file. Instead in this example, you should use the 
COPY
command (
ADD
allows more-complex file copy scenarios).
Therefore, the Dockerfile should instead be written like so:
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y \
python
python-pip
RUN pip install flask
COPY hello.py /tmp/hello.py
...
It could even be made better with the use of a Python official image:
FROM python:2.7.10
RUN pip install flask
COPY hello.py /tmp/hello.py
...
This is not meant to be exhaustive, but gives you a taste of how to optimize your
Dockerfile. For more detailed information, see the recommended best 
practices
.
2.6 Versioning an Image with Tags
Problem
You are creating multiple images and multiple versions of the same image. You would
like to keep track of each image and its versions easily, instead of using an image ID.
Solution
Tag the image with the 
docker tag
command. This allows you to rename an existing
image, or create a new tag for the same name.
2.6 Versioning an Image with Tags | 47


When you committed an image (see 
Recipe 2.1
) you already used tags. The naming
convention for images is that everything after a colon is a tag.
A tag is optional. If you do not specify a tag, Docker will implicitly
try to use a tag called 
latest
. If such a tag for the image being ref‐
erenced does not exist in the repository, Docker will fail to down‐
load the image.
For example, let’s rename the 
ubuntu:14.04
image to 
foobar
. You will not specify a tag,
just change the name; hence Docker will use the 
latest
tag automatically:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 9bd07e480c5b 12 days ago 192.7 MB
$ docker tag ubuntu foobar
2014/12/17 09:57:48 Error response from daemon: No such id: ubuntu
$ docker tag ubuntu:14.04 foobar
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
foobar latest 9bd07e480c5b 12 days ago 192.7 MB
ubuntu 14.04 9bd07e480c5b 12 days ago 192.7 MB
The first thing that you see in the preceding example is that when you try to tag the
ubuntu
image, Docker throws an error. That is because the 
ubuntu
image has only a
14.04
tag and no 
latest
tag. In your second attempt you specify the existing tag by
using a colon, and the tagging is successful. Docker creates a new 
foobar
image and
automatically adds the 
latest
tag. If you specify a tag by using a colon after the new
name for the image, you get this:
$ docker tag ubuntu:14.04 foobar:cookbook
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
foobar cookbook 9bd07e480c5b 12 days ago 192.7 MB
foobar latest 9bd07e480c5b 12 days ago 192.7 MB
ubuntu 14.04 9bd07e480c5b 12 days ago 192.7 MB
All the images you used so far are local to your Docker host. But when you want to
share these images through registries, you need to name them appropriately. Specifi‐
cally, you need to follow the 
USERNAME/NAME
convention when preparing an
image for 
Docker Hub
. When using a private registry, you need to specify the registry
host, an optional username and the name of the image (i.e., 
REGISTRYHOST/USER‐
NAME/NAME
). And, of course, you can still use a tag (i.e., 
:TAG
).

Download 6,31 Mb.

Do'stlaringiz bilan baham:
1   ...   44   45   46   47   48   49   50   51   ...   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