Introduction à Docker et ses outils
Introduction à Docker et ses outils
Introduction à Docker
Contenu original :
Johan Moreau <[Link]@[Link]>
1 / 71
Introduction Docker engine Docker Compose Docker Swarm
Plan
1 Introduction
Un conteneur : kezako ?
Docker, la petite histoire
2 Docker engine
3 Docker Compose
4 Docker Swarm
2 / 71
Introduction Docker engine Docker Compose Docker Swarm
Un conteneur : kezako ?
3 / 71
Introduction Docker engine Docker Compose Docker Swarm
5 / 71
Introduction Docker engine Docker Compose Docker Swarm
2
Liaison avec le noyau Linux et autres OS
Une belle évolution de Linux :
Mount namespace (Linux 2.4.19)
PID namespace (Linux 2.6.24) - x PID/process
Net namespace (Linux 2.6.19-2.6.24)
User namespace (Linux 2.6.23-3.8)
IPC namespace (Linux 2.6.19-2.6.30)
UTS namespace (Linux 2.6.19) - host et domain
cgroups (Linux 2.6.24) - gérer la limitation de ressource
AUFS/BTRFS: FS/couche, mode union, copy on write
8 / 71
Introduction Docker engine Docker Compose Docker Swarm
De dotcloud à docker
La petite histoire :
DotCloud 3 équivalent FR à Heroku (PAAS)
Construit sur LXC et AUFS + dev. kernel linux
Développement interne d’un cli en python (dc)
Pas mal de soucis sur la gestion des conteneurs:
coucurrence ...
10 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker
Terminologie :
client/server : outil utilisant l’API du serveur/Daemon
index : répertoire public ([Link]
image : conteneur en lecture seule (couches = snapshot)
conteneur : élément manipulable
11 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker
6
[Link]
7
[Link]
12 / 71
Introduction Docker engine Docker Compose Docker Swarm
8
RBAC LDAP/AD, gestion des images avancées, scan d’images, etc ...
13 / 71
Introduction Docker engine Docker Compose Docker Swarm
Version en [Link]
14 / 71
Introduction Docker engine Docker Compose Docker Swarm
Plan
1 Introduction
2 Docker engine
Les commandes de bases
Les images
La persistance des données
Le réseau
La sécurité
Des exemples
3 Docker Compose
4 Docker Swarm
15 / 71
Introduction Docker engine Docker Compose Docker Swarm
16 / 71
Introduction Docker engine Docker Compose Docker Swarm
Préambule
Utilisation de docker
Sudo ou non ? Groupe docker
Syntaxe de la commande
A l’ancienne:
$ d o c k e r <command> <o p t i o n s > <image> <commande>
Exemple:
$ d o c k e r r u n h e l l o −w o r l d
Exemple:
$ d o c k e r c o n t a i n e r r u n h e l l o −w o r l d
9
[Link]
17 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker 10110
$ d o c k e r c o n t a i n e r r u n − i −t u b un t u / b i n / b a s h
10
Les images de base sont très légères pas de ifconfig (net-tools)/ping(iputils-ping)
18 / 71
Introduction Docker engine Docker Compose Docker Swarm
19 / 71
Introduction Docker engine Docker Compose Docker Swarm
1 seul processus
20 / 71
Introduction Docker engine Docker Compose Docker Swarm
# R e c h e r c h e une image d a n s l a r e g i s t r y ( I m p o r t a n c e du ” / ” )
d o c k e r s e a r c h u bu n t u
# R e c u p e r a t i o n de l a d e r n i e r e image s t a c k b r e w / u bu n t u i m a g e s
d o c k e r image p u l l s t a c k b r e w / u bu n t u
# A f f i c h e l ’ h i s t o r i q u e d ’ une image
d o c k e r image h i s t o r y s t a c k b r e w / u b un t u
# A f f i c h e l a l i s t e des images l o c a l e s
docker images
21 / 71
Introduction Docker engine Docker Compose Docker Swarm
# L i e n de 2 c o n t e n e u r s
d o c k e r c o n t a i n e r r u n − t i −− l i n k r e d i s : db −−name webapp u bu n t u b a s h
# L a n c e un c o n t e n e u r en a r r i e r e p l a n
d o c k e r c o n t a i n e r r u n −d −p 8 8 8 8 : 8 0 u b un t u # e x p o r t 8888 on m a s t e r
# A f f i c h e l e s c o n t e n e u r s a c t i f s (−a p o u r l e s a f f i c h e r s tous )
d o c k e r c o n t a i n e r ps
d o c k e r c o n t a i n e r l o g s myUbuntu
d o c k e r c o n t a i n e r e x e c myUbuntu / b i n / b a s h
docker c o n t a i n e r s t a r t myUbuntu # R e l a n c e un c o n t e n e u r
docker c o n t a i n e r a t t a c h myUbuntu # R e p r e n d r e l a main
docker c o n t a i n e r s t o p myUbuntu # SIGTERM s u i v i d ’ un SIGKILL
docker c o n t a i n e r k i l l myUbuntu # SIGKILL d i r e c t e m e n t
22 / 71
Introduction Docker engine Docker Compose Docker Swarm
Les images
23 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker hub
Le hub public :
Dépôt public (push/pull gratuit)
Dépôt d’images officielles (sans ”/”), et d’images tiers,
Systèmes d’exploitation:
debian, ubuntu, centos ...
Mention spéciale: alpine: Micro-distribution
alpine:3.5 : 36.5 Mo
ubuntu:16:04 : 184 Mo
Services conteneurisés:
php, nginx, mariadb, ...
Collection de services supplémentaires :
Builds automatisés (lier des dépôts github/bitbucket pour
lancer un build suite à un commit)
[Link]: Référentiel d’image version entreprise
24 / 71
Introduction Docker engine Docker Compose Docker Swarm
Performance (*5)
Ré-utilisabilité
Lecture seule donc :
diff et versioning
25 / 71
Introduction Docker engine Docker Compose Docker Swarm
26 / 71
Introduction Docker engine Docker Compose Docker Swarm
Commande RUN
29 / 71
Introduction Docker engine Docker Compose Docker Swarm
Combinaisons:
ENTRYPOINT ou CMD:
Spécifie la commande à lancer au démarrage
ENTRYPOINT et CMD:
La commande prend alors la forme:
${ENTRYPOINT} ${CMD}
CMD est, dans ce cas, surchargeable au lancement avec run
Formes de lancement (valable aussi pour RUN):
Forme shell (mod. SHELL) : RUN echo hello
⇒ /bin/sh -c echo hello
Forme exec : RUN [”echo”, ”hello”]
⇒ echo hello
Forme shell :
l’application ne recevra pas les signaux envoyés par stop et kill
30 / 71
Introduction Docker engine Docker Compose Docker Swarm
31 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exercice
32 / 71
Introduction Docker engine Docker Compose Docker Swarm
13 14
Multi-stage build
13
[Link] & Builder
pattern
14
Présent que sur les dernières versions (edge ou 17.05), sinon voir Builder Pattern avec un shell script
33 / 71
Introduction Docker engine Docker Compose Docker Swarm
FROM a l p i n e : l a t e s t
FROM a l p i n e : l a t e s t
COPY −−from=0 . / h e l l o .
COPY h e l l o .
CMD [ ” . / h e l l o ” ]
CMD [ ” . / h e l l o ” ]
d o c k e r image b u i l d \
−−t a g hgo : l a t e s t \
−− f i l e D o c k e r f i l e 2 .
15
[Link] 34 / 71
Introduction Docker engine Docker Compose Docker Swarm
35 / 71
Introduction Docker engine Docker Compose Docker Swarm
Volume
Interêt:
Conserver des données quand un conteneur est supprimé
Partager des fichiers/dossiers entre conteneurs
Partager des fichiers/dossiers entre hôte et conteneurs
Options:
Utilisation du -v (–volume)
Utilisation du –volumes-from (Masquage possible dans le
conteneur)
36 / 71
Introduction Docker engine Docker Compose Docker Swarm
Volume
Volumes nommés :
$ d o c k e r volume c r e a t e −−name dataVolume
$ d o c k e r r u n −t −i −v dataVolume : / d a t a ubuntu / b i n / b a s h
Montage inter-conteneur:
$ d o c k e r volume c r e a t e −−name dataVolume
$ d o c k e r r u n −t −i −v dataVolume : / d a t a : rw −−name myUbuntu ubuntu / b i n / b a s h
$ d o c k e r r u n −t −i −−v o l u m e s−from myUbuntu ubuntu / b i n / b a s h
37 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exercice: Volumes
38 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exercice: Volumes
d o c k e r c o n t a i n e r c r e a t e − t i −v myEtc : / e t c \
−−name myUbuntu u b u nt u
d o c k e r c o n t a i n e r r u n −d −−rm \
−v myEtc : / d a t a \
−v $ {PWD} : / backup u b u nt u \
b a s h −c ’ cd / d a t a && t a r c z v f / backup / backup . t a r . gz . ’
39 / 71
Introduction Docker engine Docker Compose Docker Swarm
Le réseau
40 / 71
Introduction Docker engine Docker Compose Docker Swarm
Network - au démarrage
Au démarrage du daemon docker:
création du bridge ”docker0”
une adresse IP privé ainsi qu’une adresse MAC sont assignées
au bridge
configuration des tables de routage (route et iptables)
Toute création de container entraı̂ne la création de deux
paires d’interfaces:
une dans le container : eth*
une autre dans la machine hôte : veth*
toutes les deux reliées au bridge et fonctionne comme un pipe
génération d’une adresse IP ainsi qu’une adresse MAC pour le
container
configuration de la route par défaut dans le container
41 / 71
Introduction Docker engine Docker Compose Docker Swarm
Network
42 / 71
Introduction Docker engine Docker Compose Docker Swarm
43 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exercice
44 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exercice : Netcat
Coté client:
<cmd> | nc <IP ou hostname> 1234
45 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exercice
d o c k e r r u n −d −−rm −−name a l p i n e 1 a l p i n e \
/ b i n / s h −c ’ w h i l e t r u e ; do nc −v − l −p 1234 ; done ’
# Does n o t work
d o c k e r r u n − t i −−rm −−name a l p i n e 2 a l p i n e \
/ b i n / s h −c ’ c a t / e t c / i s s u e | nc a l p i n e 1 1 2 3 4 ’
# Works ( w i t h I P o f a l p i n e 1 )
d o c k e r r u n − t i −−rm −−name a l p i n e 2 a l p i n e \
/ b i n / s h −c ’ c a t / e t c / i s s u e | nc 1 7 2 . 1 7 . 0 . 3 1 2 3 4 ’
# Works
d o c k e r r u n − t i −−rm −−name a l p i n e 2 −− l i n k a l p i n e 1 a l p i n e \
/ b i n / s h −c ’ c a t / e t c / i s s u e | nc a l p i n e 1 1 2 3 4 ’
# With n e t w o r k s
docker network c r e a t e n e t n c s r v
d o c k e r r u n −d −−rm −−name a l p i n e 1 −−n e t w o r k n e t n c s r v a l p i n e \
/ b i n / s h −c ’ w h i l e t r u e ; do nc −v − l −p 1234 ; done ’
d o c k e r r u n −d −−rm −−name a l p i n e 2 −−n e t w o r k n e t n c s r v a l p i n e \
/ b i n / s h −c ’ c a t / e t c / i s s u e | nc a l p i n e 1 1 2 3 4 ’ 46 / 71
Introduction Docker engine Docker Compose Docker Swarm
47 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exercice
# Make m i s s i n g d i r e c t o r i e s
RUN m k d i r −p / u s r / s r c / app / t e m p l a t e s
# c o p y f i l e s r e q u i r e d f o r t h e app t o r u n
COPY . / app . py / u s r / s r c / app /
COPY . / t e m p l a t e s / i n d e x . h t m l / u s r / s r c / app / t e m p l a t e s
# t e l l t h e p o r t number t h e c o n t a i n e r s h o u l d e x p o s e
EXPOSE 5000
# run the a p p l i c a t i o n
CMD [ ” p y t h o n ” , ” / u s r / s r c / app / app . py ” ]
48 / 71
Introduction Docker engine Docker Compose Docker Swarm
Network
Reverse-proxy :
nginx ([Link]
traefik16 ([Link] HAProxy,
...
16
[Link] image sous [Link]
49 / 71
Introduction Docker engine Docker Compose Docker Swarm
La sécurité
50 / 71
Introduction Docker engine Docker Compose Docker Swarm
Sécurité
17
[Link]
18
[Link]
51 / 71
Introduction Docker engine Docker Compose Docker Swarm
Problème:
Par défaut, root dans un conteneur
on peut monter n’importe quel volume
$ d o c k e r r u n −t −i −v / e t c : / d a t a a l p i n e
19
[Link]
52 / 71
Introduction Docker engine Docker Compose Docker Swarm
Private Registry :
Stocke et distribue les images Docker
De nombreux Registry hébergés disponibles:
Docker Hub, AWS ECR, ...
Peut être self-hosted avec plusieurs intégrations pour le
stockage:
Local, AWS S3, Ceph, OpenStack Swift, ...
53 / 71
Introduction Docker engine Docker Compose Docker Swarm
54 / 71
Introduction Docker engine Docker Compose Docker Swarm
d o c k e r r u n −d −p 5 0 0 0 : 5 0 0 0 −−name r e g i s t r y \
−v $ ( pwd ) / r e g i s t r y −d a t a : / v a r / l i b / r e g i s t r y r e g i s t r y : 2
d o c k e r t a g h e l l o −w o r l d 1 2 7 . 0 . 0 . 1 : 5 0 0 0 / h e l l o −w o r l d
d o c k e r p us h 1 2 7 . 0 . 0 . 1 : 5 0 0 0 / h e l l o −w o r l d
55 / 71
Introduction Docker engine Docker Compose Docker Swarm
Haute-disponibilité vs Migration
Stratégie :
Les conteneurs ne sont pas migrables à chaud
Choix d’une architecture scalable permettant la
haute-disponibilité, tout en négligeant la perte d’un noeud
L’architecture logiciel doit tenir compte sur de ce design
56 / 71
Introduction Docker engine Docker Compose Docker Swarm
Des exemples
57 / 71
Introduction Docker engine Docker Compose Docker Swarm
Exemples DevOps
pour ”le Dev” :
Multiples environnements (tests, dev, branches, ...)
Compilation/Exécution multi-[os—jvm—tools—...]
Utilisation de conteneurs pré-chargés avec des data pour les
tests
Outils spécifiques disponibles : plugins IntelliJ, Eclipse 20 , ...
pour ”l’Ops” :
Rapidité de déploiement
Force les bonnes pratiques (microservice, description donc
documentation, ...)
Déploiement récurrent de serveur
Gestion des vulnérabilités : mise à jour d’une des couches,
test, ...
20
[Link]
58 / 71
Introduction Docker engine Docker Compose Docker Swarm
Quoi :
Les évangélistes Docker font quasiment tout tourner en
conteneur
Permet de limiter la contagion virale depuis un logiciel
Moyen de tester des applications en gardant un système clean
Permet de garder propre l’OS sous jacent :
Latex, environnement lourd, nombreuses dépendances
Exemple 21 : Latex, Irssi, Mutt, Spotify, Skype
21
[Link]
59 / 71
Introduction Docker engine Docker Compose Docker Swarm
60 / 71
Introduction Docker engine Docker Compose Docker Swarm
Plan
1 Introduction
2 Docker engine
3 Docker Compose
4 Docker Swarm
61 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker Compose
62 / 71
Introduction Docker engine Docker Compose Docker Swarm
v e r s i o n : ”2”
services :
service name 1 :
image : <image name 1 >
ports :
− ” 80:80 ”
volumes :
− ./ s e r v i c e . conf :/ etc / s e r v i c e / s e r v i c e . conf
command : −−v e r b o s e
whoami :
image : <image name 1 >
b u i l d : . / path / to / d o c k e r f i l e
depends on :
− service name 1
environment :
− MY ENV VAR=e n v i r o n e m e n t
63 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker Compose
docker run −d −−name db mysql
db :
image : m y s q l
environment :
MYSQL ROOT PASSWORD : p a s s w o r d
docker run −d −−name phpfpm −−link db:mysql jprjr/php-fpm
phpfpm :
image : j p r j r / php−fpm
volumes :
− . : / srv / http
− t i m e z o n e . i n i : / e t c / php / c o n f . d/ t i m e z o n e . i n i
links :
− db : m y s q l
docker run −d −−link phpfpm:phpfpm −p 80:80 my-nginx
nginx :
build : .
links :
− phpfpm : phpfpm
ports :
− 80:80
64 / 71
Introduction Docker engine Docker Compose Docker Swarm
65 / 71
Introduction Docker engine Docker Compose Docker Swarm
v e r s i o n : ”2”
services :
my−mysql−s e r v e r :
image : m y s q l
environment :
− MYSQL ROOT PASSWORD=r o o t
my−phpmyadmin−s e r v e r :
image : phpmyadmin / phpmyadmin
depends on :
− my−mysql−s e r v e r
environment :
− MYSQL ROOT PASSWORD=r o o t
− PMA HOST=my−mysql−s e r v e r
ports :
− ” 8080:80 ”
66 / 71
Introduction Docker engine Docker Compose Docker Swarm
Plan
1 Introduction
2 Docker engine
3 Docker Compose
4 Docker Swarm
67 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker Swarm22
Docker en multihost
Possiblement windows/linux
Faire passer des services à l’échelle
Tagger des machines: lieux (France, US
...), type (production, dev ...)
2 versions : docker swarm et docker-swarm
(ancien)
docker swarm (depuis 1.12) :
docker swarm init – join – ps
22
[Link] [Link]
68 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker Machine23
23
Remplace boot2docker même si celui est toujours disponible
69 / 71
Introduction Docker engine Docker Compose Docker Swarm
Docker Machine24
d o c k e r −m a c h i n e c r e a t e −d v i r t u a l b o x manager1
d o c k e r −m a c h i n e s s h manager1
d o c k e r swarm i n i t −−a d v e r t i s e −a d d r = . . .
d o c k e r −m a c h i n e c r e a t e −d v i r t u a l b o x s l a v e 1
d o c k e r −m a c h i n e s s h s l a v e 1
d o c k e r swarm j o i n . . .
d o c k e r −m a c h i n e c r e a t e −d v i r t u a l b o x s l a v e 2
d o c k e r −m a c h i n e s s h s l a v e 2
d o c k e r swarm j o i n . . .
Depuis le manager:
d o c k e r node l s
d o c k e r s e r v i c e c r e a t e −−name whoami −− r e p l i c a s 1 \
−p 8 0 : 8 0 0 0 j w i l d e r / whoami
docker s e r v i c e l s
24
[Link]
70 / 71
Introduction Docker engine Docker Compose Docker Swarm
Le document :
Cette présentation a été faite avec des outils opensource et
libre dans le but de présenter des outils de construction
d’applications eux-aussi opensource.
N’hésitez pas à nous envoyer vos remarques ou corrections à:
[Link] sur [Link] ou
[Link] sur [Link]
Ce document est distribué sous licence Creative Commons
Attribution-ShareAlike 2.0
71 / 71