You are on page 1of 36

Type your text

Docker Basics Type your text

Type your text


Type your text

ype your text

1 / 36
Outline
VMs, Containers, Docker
Getting Started - Docker Engine
Custom Images
Docker Compose

2 / 36
VMs, Containers & Docker
Introduction

3 / 36
Virtual Machines Virtual Machines
Each virtual machine includes the application, the necessary
vs. binaries and libraries and an entire guest operating system - all
of which may be tens of GBs in size.
Containers Containers
 
(Docker) Containers include the application and all of its
Containers have similar resource isolation dependencies, but share the kernel with other containers. They
run as an isolated process in userspace on the host operating
and allocation bene ts as virtual machines system. They're also not tied to any speci c infrastructure.
but a di erent architectural approach
(Docker) Containers running on a single machine all share the
allows them to be much more portable and same operating system kernel so they start instantly and make
e cient. more e cient use of RAM. Images are constructed from layered
lesystems so they can share common les, making disk usage
Ref: docker.com and image downloads much more e cient.

4 / 36
5 / 36
LXC

Containers
LXC owes its origin to the development of cgroups and
namespaces in the Linux kernel to support lightweight
virtualized OS environments (containers) and some early work
Container (lightweight process virtualization) technology is by Daniel Lezcano and Serge Hallyn dating from 2009 at IBM.
not new, mainstream support in the vanilla kernel however
is, paving the way for widespread adoption (Linux Kernel 3.8 The LXC Project provides tools to manage containers, advanced
- released in February 2013 - cf. Rami Rosen). networking and storage support and a wide choice of minimal
container OS templates. It is currently led by a 2 member team,
Stephane Graber and Serge Hallyn from Ubuntu. The LXC
FreeBSD has Jails, Solaris has Zones and
project is supported by Ubuntu.
there are other (Linux) container
technologies: OpenVZ, VServer, Google Docker
Containers, LXC/LXD, Docker, etc. Docker is a project by dotCloud now Docker Inc released in
March 2013, initially based on the LXC project to build single
Ref: Flockport
application containers. Docker has now developed their own
implementation libcontainer that uses kernel namespaces and
cgroups directly.

6 / 36
Both LXC and Docker are userland container
managers that use kernel namespaces to
provide end user containers. We also now
have Systemd-Nspawn that does the same
LXC vs. Docker thing.
Ref: Flockport
The only di erence is LXC containers have an
an init and can thus run multiple processes
and Docker containers do not have an init
and can only run single processes.

7 / 36
Type your text

8 / 36
Docker
Docker allows you to package an application with all of its
dependencies into a standardized unit for software
development.

Docker containers wrap up a piece of software in a complete


lesystem that contains everything it needs to run: code,
runtime, system tools, system libraries - anything you can
install on a server. This guarantees that it will always run the
same, regardless of the environment it is running in.

Docker containers run on any computer, on any


infrastructure and in any cloud.

Ref: docker.com

9 / 36
Containers isolate individual applications and use operating system resources that have been abstracted by Docker.
Containers can be built by "layering", with multiple containers sharing underlying layers, decreasing resource usage.
Ref: Docker Ecosystem - DO

10 / 36
Docker Advantages
Typically, when designing an application or service to use
Lightweight resource utilization: instead of virtualizing an
Docker, it works best to break out functionality into individual
entire operating system, containers isolate at the process
containers, a design recently known as micro-service
level and use the host's kernel.
architecture.
Portability: all of the dependencies for a containerized
This gives you the ability to easily scale or update application are bundled inside of the container, allowing
components independently in the future. it to run on any Docker host.
Predictability: The host does not care about what is
Having this exibility is one of the many reasons that people running inside of the container and the container does
are interested in Docker for development and deployment. not care about which host it is running on. The interfaces
are standardized and the interactions are predictable.
Ref: Docker Ecosystem - DO

11 / 36
Docker Engine
Getting Started

12 / 36
Docker Engine
When people say "Docker" they typically mean Docker Engine,
the client-server application made up of the Docker daemon, a
REST API that speci es interfaces for interacting with the
daemon, and a command line interface (CLI) client that talks to
the daemon (through the REST API wrapper).

Docker Engine accepts docker commands from the CLI, such as


do cke rr un<i mage >, do ck erpsto list running
containers, dock erim ag e sto list images, and so on.

Engine is the core of Docker and nothing else will run without it.

Ref: docker.com

13 / 36
Docker Architecture

14 / 36
Docker daemon
Docker Architecture The Docker daemon runs on a host machine. The user does not
directly interact with the daemon, but instead through the
Docker uses a client-server architecture. The Docker client talks Docker client.
to the Docker daemon, which does the heavy lifting of building,
running, and distributing your Docker containers.
Docker client
Both the Docker client and the daemon can run on the same
The Docker client, in the form of the docker binary, is the
system, or you can connect a Docker client to a remote Docker
primary user interface to Docker.
daemon.
It accepts commands from the user and communicates back and
The Docker client and daemon communicate via sockets or
forth with a Docker daemon.
through a RESTful API.
Ref: docker.com

15 / 36
16 / 36
Let's Try It ...
My Case: amd64 Machine, Ubuntu 16.04

17 / 36
First Step
$cur
l- f
sSLhttps://get.docker.com/|sh
$doc
kerinf
o
C
onta
iner
s:1
.
..
I
mage
s:15
S
erve
rV e
rsi
on:1 .11.
1
S
tora
geDriv
er:auf s $do
cke
rrunh
ello
-wo
rld
.
..
L
oggi
ngDriv
er:jso n-file H
ellof
romDoc
ker.
C
grou
pD r
ive
r:cgro upfs T
hisme
ssages
howsth
aty
ourin
stal
lat
ionap
pea
rst
obewo
rki
n
P
lugi
ns:
.
.. T
ogenera
teth i
sm essa
ge,Do ckertookt hefo llowingsteps:
K
erne
lV e
rsi
on:4 .4.0
-21 -
g eneric 1.T
h eDock
erc lie
n tcon
ta ctedtheD ockerda emo
n.
O
pera
tingSy
stem:U buntu1 6.04L TS 2.T
h eDock
erd aem
o npul
le dthe"hell o
- world"imagefromthe
.
.. 3.T
h eDock
erd aem
o ncre
at edane wc ontainerfromt hatimage
$doc
kerver
sion execu
tablethatp r
oducesth eoutputy oua recu r
rentlyread
4.T
h eDock
erd aem
o nstr
ea medthato utputtoth eD ocke
rc lie
n
C
lien
t: toy o
urte r
minal.
Vers
ion: 1.11
. 1
APIvers
ion
: 1.23 T
otr
ys o
met
hin
gmo
reamb
iti
ous
,yo
ucanrunanUbu
ntucon
tai
n
Goversi
on: go1.5.4 $do
ckerru
n-itu
bun
tubas
h
Gitcomm
it: 5604
c be
Buil
t: TueAp r2 623:
43:
492
016 S
har
eimages
,auto
mat
eworkf
low
s,a
ndmor
ewi
thaf
reeDoc
ker
OS/A
rch: linux/amd64 htt
ps:
//hub
.do
cke
r.c
om

S
erve
r: F
ormor
ee xa
mpl
esandide
as,vi
sit:
Vers
ion: 1.11
. 1 htt
ps:
//doc
s.d
ock
er.c
om/
use
rgu
ide/
APIvers
ion
: 1.23
Goversi
on: go1.5.4
Gitcomm
it: 5604
c be
Buil
t: TueAp r2 623:
43:
492
016 Ref: Quickstart, Install Docker
OS/A
rch: linux/amd64

18 / 36
Try Some Commands $do
ckerrun- -
rmf irecyb
erice/whal
esa
yHell
oDo
cke
r
___
_____
______
<He
lloDocker>
---
-----
------
$do
ckerima
ges \
R
EPO
SITOR
Y TAG SIZE \
e
m/n
otebo
ok v1 864.9MB \
u
bun
tu 16.04 120
.1MB ## .
a
lpi
ne 3.3 4.7
98MB ##### # ==
b
usy
box latest 1.113MB ##### ## ### = ==
f
ire
cyber
ice
/wh
ales
ay latest 47.25MB /
""""
""""""
"" ""
"""\___/===
h
ell
o-wor
ld latest 960B ~~
~{ ~~~~~~~~~~~ ~~~~~~/ == =-~~~
d
ock
er/wh
ale
say latest 247MB \_
_____o __/
\ \ _
_/
\____
\_______/

$dockerp s-a
$JO
B=$(d
ockerrun- dubuntu/ bin
/sh- c"whilet ru
e;doech
oHe
llC
O
oNT
wA
o
rI
lN
dE
;RsI
lD
eep1;doI
M
nA
eG
"E C
OMM
AND
2
f6f337530d5 h
ello-wo
rld "
/he
llo
"
$do
ckerstop$ JOB e
71dbedafb57 e
m/noteb
ook
:v1 "
tin
i--j u
pyte
rnote
$do
ckerstart$ JOB
$do
ckerrestart$ JOB $dockerr m-f2 f6
f
2
f6f
$do
ckerkill$JO B $dockerp s-a
C
ONTAINERID I
MAGE C
OMM
AND
$do
ckerstop$ JOB #C ontaine
rm ustbes to
p pe
dt ore
moveit e71dbedafb57 e
m/noteb
ook
:v1 "
tin
i--j u
pyte
rnote
$do
ckerrm$ JOB
$do
ckerrm- f$JO B #R unningcontai
ner $dockerr un-it--en
trypoin
t/bin/
shfir
ecy
ber
ice/
whal
esa
y

19 / 36
nginx
$dockerrun- d-P--namewebngi
nx
2
24a
61e a
84cfbf
468bd09
0aebbd
0ba
534e9
b07
bb8e
7e00
68bf
aec
a1b
a72f
754
b

$dockerps
C
ONTAINERID IMAGE COMMAND C
REA
TED STA
TUS POR
TS
2
24a
61e a
84c f n
gin x "
ng i
nx- g'd
aemono
f f
"
e
71dbedafb57 em/note
book:
v1 "tini--jupyt
ernote"

$dockerportw eb
4
43/tcp->0 .0.
0. 0
:3276
8
8
0/tcp->0 .0.
0. 0
:3276
9

$do
ckersto
pweb
$do
ckerrmweb

20 / 36
Mount a Volume on the Container

$mk
dirmysite&&cdmysite
m
ysi
te$echo"mynewsite
">i n de
x .
h tm
l
m
ysi
te$dockerrun-d-P-v$( pwd ):/usr/s
ha re
/ ng
i nx
/h tm
l- -na
mem yw
ebn
ginx
d
a01
817c
28bbdb2
f3b7
127
5ba7b9560da4e65f8716329c16787c83181
760534c

m
ysite$dockerportm y
web
4
43/tcp->0 .0.
0.0:3
2770
8
0/tcp->0 .0.
0.0:3
2771

m
ysi
te$echo"thisisc
ool
">co
ol.h
tml
$do
ckerstopmyweb
$do
ckerrmmyweb

Ref: docker.com

21 / 36
Custom Images

22 / 36
Dockerfile
Build Custom Image F
R
R
U
OMdo
Na
c
pt
-
k
g
e
e
r/
wha
l
t-yu
e
p
s
a
d
a
y:l
a
te&
te
s
&a
t
pt
-ge
tinsta
ll-yf
ort
une
s
C
MD/us
r/gam
es/
for
tune-a|cow
say

$mkdirm ydockerbuild& &c dmy dockerbuild/& &touc


hDockerfile
$dockerbui ld-td ocker-whale.
S
endingb uildc ontextt oDo ckerd aemon2 .048k B
$doc
kerrund o
cker-whale
S
tep1:F ROMdo cker/whalesay:la
tes t
____
____
______
_______________________
___
--->6 b
362 a9f73eb
/Ontheotherhand,l ifeca nb ean \
S
tep2:R UNapt -get-yu pdate&&a pt-geti ns
tall-yf
or tun
es
|end
lessparad
eo fTR ANSS
E XUALQ UIL
TING|
--->Ru nningi n737 5f2
759 7d7
|BEE
Sa b
oardac ruises hipto |
.
..
\DIS
NEYW
ORLDifon lyw eleti t!! /
S
tep3:C MD/us r/games/fortune-a|c owsay
----
----
------
-----------------------
---
--->Ru nningi n09 c5
7e3 ebb83
\
--->4 28cbace4310
\
R
emovingint ermediatec ontainer0 9c57e3ebb83
\
S
uccessfullybu ilt4 28cbace4310
## .
#
## ## # ==
##### ## # ===
Ref: Build your own image /
""""
""""""
"" ""
""___/== =
~~~{~~~~~~~~~~~ ~~~ ~~/ = ==-~~~
\_
_____o __/
\ \ __/
\____
\______/

23 / 36
firecyberice/whalesay
Example Dockerfile
docker/whalesay F
ROMal
pine:
3.2

RUNapku pdate\
&
&a pka ddg itpe rl\
&
&c d/ tmp/\
F
ROMubuntu:14.04 &
&g itc lonehtt ps://github.com/jasonm23/c
owsay.git\
&
&c dc owsay&&. /install.sh/ usr
/ local\
R
UNapt-getupd ate\ &
&c d. .\
&
&a pt-getinst all-yc owsay--
no-
ins
tall
-re
com
mend
s\ &
&r m-r fc owsay\
&
&r m-r f/ var/lib/apt/
lists
/*\ &
&a pkd elg it
&
&m v/u sr/share/cowsay
/cows
/de
fau
lt.
cow/us
r/s
hare
/co
wsa
y/c
ows/orig
-default.cow
ENVPATH$ PATH
#"co
wsay"i nstallst o/u sr
/game
s COPYdocker.cow/ usr/local/share/cows/
E
NVPATH$ PATH:/usr/g
ame s
#M ov
et he" default.cow"o utoft hewa ys ow ec a
no verwritei t
C
OPYdocker.cow/us r/share/
cowsa
y/c
ows
/ RUN\
R
UNln- sv/ usr/share/cowsa
y/cow
s/d
ock
er.
cow/us
r/s
hare
/co
wsa
y/c
ows/de
m
vfa
u
/l
ut
s.
rc
/o
lw
ocal/
sh are/cows/default.cow/ usr/
local/sha
r e/cows
&
&l n-s v/ usr/local/share/cows/docker.cow/usr/local/shar
C
MD["co
ws ay"]
ENTRY
POINT[ "cowsay"]

24 / 36
Manual Process
$do
ckerrun--
namemy
red
is-itubu
ntu
:16.
04b
ash

r
oot
@ac6
002b
2a9
8b:/
#apt
-ge
tupda
te test/myredis:v1
r
oot
@ac6
002b
2a9
8b:/
#apt
-ge
tinst
allwge
t
r
oot
@ac6
002b
2a9
8b:/
#apt
-ge
tinst
allbui
ld-
esse
nti
altc
l8.
5

r
oot@
ac60
02b
2a98b:/#wge th ttp://download.redis.io
/redi
s -s
tabl
e.t
ar.gz
root
@ac6002b2a98b:
/redis-stable#psax|g repred is
r
oot@
ac60
02b
2a98b:/#tarxz fr edis-stable.tar.gz
root
@ac6002b2a98b:/
redis-stable#src/redi
s-cli
r
oot@
ac60
02b
2a98b:/#cdr edis-stable&&ma ke&&ma kei n
s ta
ll
127.
0.0.1:6379
>s etf oobar
r
oot@
ac60
02b
2a98b:/#./r edis-stable/utils/install_
serve
r .s
h
OK
.
..
127.
0.0.1:6379
>g etf oo
S
elec
tedcon
fig:
"bar
"
P
ort :6 379
127.
0.0.1:6379
>e xit
C
onfi
gf i
le :/e tc/redis/6379.conf
L
ogfile :/v ar/l
og/ redis_
63 79.log root
@ac6002b2a98b:
/redis-stable#exit
D
atadir :/v ar/lib/redis/6379
E
xecu
tabl
e :/u sr/l
oc al/bin/redis-server $d o
ckerp s-a
C
liExecu
tab
le:/u sr/l
oc al/bin/redis-cli $d o
ckerc ommit- m"addr edis"- a"em"myredist est/myredis:v1
I
st h
isok?Thenpr essEN TERt og oono rC trl-Ctoa bort. sha2
56:9b
75 a94f
67cb47b012
f4 45ed
65fb13f
c67d05a00a
d1bb26 2d20
b a4
d
C
opie
d/ t
mp/
6379.conf=>/ etc/
init. d/redis_6
37 9
I
nsta
llin
gservice... $d o
ckeri mages|g repr edis
S
ucce
ss! test
/myredis v1 9b75a94f67cb 39s econdsago 4 08.3MB
S
tart
ingRed
isserv er...
I
nsta
llat
ionsuccessful

Ref: Getting Started with Docker

25 / 36
Dockerfile
test/myredis:df

$do
ckerbui
ld-tt
est
/my
redi
s:d
f. F
ROMub
untu:
16.0
4

$do
ckerimages|g
repre dis R
UNa
pt-g
etup
date
t
est
/myr
edis df 9 a45
0a e
418d
8 Abou
tam inu
teag
o 408.
6 R
UNa
pt-g
etin
stal
l-ywg
et
t
est
/myr
edis v1 9 b75
a9 4
f67c
b 13minu
tesago 4
08.3 R
UNa
pt-g
etin
stal
l-ybu
ild
-es
sent
ialtc
l8.
5

$do
ckerrun- d-p6 379:6379te s
t/ m
y re
dis:d f R
U Nwgeth tt
p://
downloa
d .
red is.
io/redi
s-st abl
e.t
ar.
gz
1
240
a12b56e4a87dfe89e4ca4400eb1cafde802ac
018 7a54776f9ea5
4bb7f
74fR
U Ntarx zfredis-
stable.tar.gz
R
U Ncdr edis
-stabl
e& &m ake&&m a
kein sta
ll
$do
ckerps R
U N./redis-
stable
/utils/install_
server.s
h
C
ONT
AINERID IMAGE COMMAND CREATE
D STATUS PORTS
1
240
a12b56e4 test/myred
is :df "r
ed i
s-s e
rver" E
X PO
SE6 37
9
E
N TR
YPOINT ["re
dis-ser
v e
r" ]
$su
doa pt-getinst allre dis-tools
$re
dis-cli
1
27.
0.0.1
: 63
79 >s etba tm an
O
K
1
27.
0.0.1
: 63
79 >getba t
"
man
"
1
27.
0.0.1
: 63
79 >qui t

26 / 36
Docker Compose

27 / 36
Docker Compose
Compose is a tool for de ning and running multi-container
Docker applications. With Compose, you use a Compose le to
con gure your application's services. Then, using a single
command, you create and start all the services from your
con guration.

Using Compose is basically a three-step process:

1. De ne your app's environment with a Docker le so it can


be reproduced anywhere.
2. De ne the services that make up your app in docker-
compose.yml so they can be run together in an isolated
environment.
3. Lastly, run docker-compose up and Compose will start
and run your entire app.

Ref: Overview of Docker Compose

28 / 36
$cu
rl-Lht
tps
://github.
com/docker
/compo
se/r
ele
ase
s/do
wnl
oad
/
$ch
mod+
x/usr
/local
/bin/do
cke r
-compo
se

Docker Compose
Getting Started
$do
cker
-com
pos
eve rsion
d
ock
er-c
ompo
seversion1 .
7.0,build0
d7bf
73
d
ock
er-p
yversi
on:1 .8.
0
C
Pyt
honvers
ion
:2 .7.
9
O
pen
SSLvers
ion
:Op enSSL1 .0
.1e1
1Feb2013

29 / 36
app.py

f
romfl
aski
mpo
rtF
lask
f
romre
disi
mpo
rtR
edis

a
pp=Fla
sk(
__n
ame
__)
r
edi
s=Redi
s(h
ost
='re
dis'
,po
rt=
637
9)

@
app
.route('/')
d
efhello
() :

Getting Started
redis.incr('
hit
s')
return' He
lloWorld
!Ih
avebe
ensee
n%sti
mes
.'%re
dis.
g

i
f__
n a
me__=="__m
ai n
_ _"
:

Step #1
app.r
un(
hos
t="0.0.0.0",de
bug
=Tru
e)

requirements.txt

f
las
k
r
edi
s

Ref: Getting Started

30 / 36
Dockerfile

F
ROMpyt
hon:
2.7

Getting Started
A
DD./code
W
ORKD
IR/cod
e
R
UNpipinst
all-rreq
uir
eme
nts.
txt
C
MDpyth
onapp.py

Step #2
$do
ckerbui
ld-tweb.
$do
ckerima
ges|grepwe
b
w
eb la t
est d6f2
5a9
bf6
32 2m
inute
sag
o 667
.7M
B

31 / 36
docker-compose.yml

v
ers
ion:'2'

Getting Started
s
erv
ices:
we
b:
build
:.
ports
:

Step #3
-"5000
: 50
00"
volum
es:
-. :
/code
depen
ds_on:
-r e
dis
re
dis:
image
:r edis

32 / 36
$do
cke
r-com
pos
eup
C
rea
tin
gn et
wor
k"c
omp
ose
tes
t_d
efau
lt"wi
tht
hedefa
ultdr
ive
r
B
uil
din
gw eb
.
..

$do
cke
r-com
pos
eup-
d

Getting Started
Step #4

33 / 36
Refs

34 / 36
Refs
1. Docker Introduction
2. Docker - Documentation
3. Docker Ecosystem - Digital Ocean
4. LXC vs. Docker - Flockport
5. CLIs Reference docker ps
6. Open Container Initiative
7. Getting Started with Docker
8. Docker Compose - Getting Started

35 / 36
END Type your text

Type your text

36 / 36

You might also like