Docker Cookbook


Writing Your First Dockerfile



Download 6,31 Mb.
Pdf ko'rish
bet42/260
Sana21.04.2022
Hajmi6,31 Mb.
#570749
1   ...   38   39   40   41   42   43   44   45   ...   260
Bog'liq
Docker Cookbook

2.3 Writing Your First Dockerfile
Problem
Running a container in interactive mode, making changes to it, and then committing
these changes to create a new image works well (see 
Recipe 2.1
). However, you want
to automate building your image and share your build steps with others.
Solution
To automate building a Docker image, you decribe the building steps in a Docker
manifesto called the Dockerfile. This text file uses a set of instructions to describe
which base image the new container is based on, what steps need to be taken to install
40 | Chapter 2: Image Creation and Sharing
www.allitebooks.com


various dependencies and applications, what files need to be present in the image,
how they are made available to a container, what ports should be exposed, and what
command should run when a container starts, as well as a few other things.
To illustrate this, let’s write our first Dockerfile. The resulting image will allow you to
create a container that executes the 
/bin/echo
command. Create a text file called
Dockerfile
in your working directory and write the following content in it:
FROM ubuntu:14.04
ENTRYPOINT ["/bin/echo"]
The 
FROM
instruction tells you which image to base the new image off of. Here you
choose the 
ubuntu:14.04
image from the Official Ubuntu 
repository
in Docker Hub. 
The 
ENTRYPOINT
instruction tells you which command to run when a container based
on this image is started. To build the image, issue a 
docker build .
at the prompt
like so:
$ docker build .
Sending build context to Docker daemon 2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
---> 9bd07e480c5b
Step 1 : ENTRYPOINT /bin/echo
---> Running in da3fa01c973a
---> e778362ca7cf
Removing intermediate container da3fa01c973a
Successfully built e778362ca7cf
$ docker images
REPOSITORY TAG IMAGE ID ... VIRTUAL SIZE
e778362ca7cf ... 192.7 MB
ubuntu 14.04 9bd07e480c5b ... 192.7 MB
You are now ready to run this container, specifying the image ID of the freshly built
image and passing an argument to it (i.e., 
Hi Docker !
):
$ docker run e778362ca7cf Hi Docker !
Hi Docker !
Amazing—you ran 
echo
in a container! A container was started using the image that
you built from this two-line Dockerfile. The container ran and executed the com‐
mand defined by the 
ENTRYPOINT
instruction. Once this command was finished, the
container job was done and it exited. If you run it again without passing an argument,
nothing is echoed:
$ docker run e778362ca7cf
You could also use the 
CMD
instruction in a Dockerfile. This has the advantage that
you can overwrite the 
CMD
behavior when you launch a container, by passing a new

Download 6,31 Mb.

Do'stlaringiz bilan baham:
1   ...   38   39   40   41   42   43   44   45   ...   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