I Mise en œuvre à partir de Centos 7
Installation de docker :
- centos : yum install docker
- ubuntu : apt install docker.io
Démarrer puis enregistrer le service :
- systemctl start docker
- systemctl enable docker
Vérifier le status d’exécution du service :
- systemctl status docker
Connaitre la version installée :
- docker -v
Pour vérifier que cela fonctionne :
- docker container run hello-world
Unable to find image 'hello-world:latest' locally Trying to pull repository docker.io/library/hello-world … latest: Pulling from docker.io/library/hello-world 1b930d010525: Pull complete Digest: sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe Status: Downloaded newer image for docker.io/hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly.
II La gestion des images
Récupérer une image existante
- docker search guacamole
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/guacamole/guacamole Apache Guacamole is a clientless remote de… 109 docker.io docker.io/guacamole/guacd The native server-side proxy used by Apach… 58 docker.io docker.io/glyptodon/guacamole Guacamole is a clientless remote desktop g… 31 [OK] docker.io docker.io/oznu/guacamole A self-contained guacamole docker containe… 24 [OK] docker.io docker.io/mattgruter/guacamole-webserver Guacamole web application 11 [OK] ...
Il est proposé un tableau de 6 colonnes. les dernières sont :
- Stars
- officials : développée par docker et ses partenaires
- automated
Téléchargement d’une image :
Recherche d’une image déjà fabriquée :
- docker search mailtrap
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/eaudeweb/mailtrap Mail Trap 7 [OK] docker.io docker.io/ipunktbs/docker-mailtrap Mailtrap in docker with roundcube as mail … 2 [OK] docker.io docker.io/michielbdejong/mailtrap Mailtrap - pipes all incoming emails to th… 1 [OK] docker.io docker.io/greatagent/mailtrap 0 docker.io docker.io/koyaan/mailtrap Pipe SMTP to stdout with TLS support 0 docker.io docker.io/motork/exim4 Image used to catchall email to mailtrap 0 docker.io docker.io/webgriffe/exim4-mailtrap Exim4 Docker container with Mailtrap.io su… 0 [OK]
Téléchargement de l’image qui nous intéresse :
- docker image pull eaudeweb/mailtrap
latest: Pulling from docker.io/eaudeweb/mailtrap
e833e65f6ad9: Downloading [=================================> ] 15.18 MB/22.49 MB
5b1102297860: Downloading [============> ] 16.62 MB/68.79 MB
9874d2a72f1c: Download complete
cf2e7dee04c6: Download complete
b81c55fd877e: Download complete
77601fa2c800: Download complete
b33371112eac: Downloading [===============================================> ] 9.943 MB/10.51 MB
a0a51b8ceb15: Waiting
f9a413de82e6: Waiting
f5b73ded68f4: Waiting
Attendre la fin de l’installation
Une fois fini, vérification de la présence de l’image en local :
- docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest fce289e99eb9 5 months ago 1.84 kB
docker.io/eaudeweb/mailtrap latest adfbd16b7ba0 6 months ago 269 MB
Ici, sont présentés les 2 images téléchargées.
Supprimer une image locale :
- docker image rm hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container d573bf139bcd is using its referenced image fce289e99eb9
L’image est déjà instanciée, d’ou l’erreur de suppression. Il faut forcer la suppression avec l’option -f :
- docker image rm -f hello-world
Untagged: hello-world:latest
Untagged: docker.io/hello-world@sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
L’image est effacée. Vérification :
- docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/eaudeweb/mailtrap latest adfbd16b7ba0 6 months ago 269 MB
Il ne reste bien qu’une image sur le disque en local.
III Container : une instance d’une image
Un container est une instance d’une image stockée en local. Pour la lancer il exécuter la commande suivante :
- docker container run eaudeweb/mailtrap
Set the ‘ServerName’ directive globally to suppress this message
.
==> /var/log/apache2/error.log <==
[Mon Jun 10 15:14:47.935923 2019] [mpm_prefork:notice] [pid 187] AH00163: Apache/2.4.25 (Debian) configured — resuming normal operations
[Mon Jun 10 15:14:47.936009 2019] [core:notice] [pid 187] AH00094: Command line: ‘/usr/sbin/apache2’
==> /var/log/mail.log <==
Jun 10 15:14:47 5c4ff61f45c7 postfix[108]: Postfix is running with backwards-compatible default settings
Jun 10 15:14:47 5c4ff61f45c7 postfix[108]: See http://www.postfix.org/COMPATIBILITY_README.html for details
Jun 10 15:14:47 5c4ff61f45c7 postfix[108]: To disable backwards compatibility use "postconf compatibility_level=2" and "postfix reload"
Jun 10 15:14:47 5c4ff61f45c7 postfix/master[155]: daemon started -- version 3.1.8, configuration /etc/postfix
Jun 10 15:14:47 5c4ff61f45c7 dovecot: master: Dovecot v2.2.27 (c0f36b0) starting up for imap
Jun 10 15:14:47 5c4ff61f45c7 dovecot: ssl-params: Generating SSL parameters
Jun 10 15:14:53 5c4ff61f45c7 dovecot: ssl-params: SSL parameters regeneration completed
Une fois la commande lancée, le shell ne rend pas la main… L’instance est bien en cours d’éxecution … mais il va falloir ouvrir un autre terminal pour reprendre la main.
Pour voir les containers (instances) en cours d’exécution :
- docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c4ff61f45c7 eaudeweb/mailtrap « /var/local/docker… » 5 minutes ago Up 5 minutes 25/tcp, 80/tcp lucid_goldberg
Commande plus rapide :
- docker ps
Pour tuer le processus :
- docker container kill 5c4ff61f45c7
Pour relancer le container mais de manière interactive :
- docker container run -it eaudeweb/mailtrap
Le CTRL C fonctionnera ….
Le container est lancé. Pour savoir quels sont les ports d’écoutes :
- docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf77df238d6d adfbd16b7ba0 "/var/local/docker…" About a minute ago Up About a minute 25/tcp, 80/tcp loving_booth
A noter que les instances ne sont pas persistantes. Au prochain démarage à partir de l’image, toutes les modifications seront perdues.
IV Enregistrer ses modifications
Lancer un contenair centos
- docker run -ti centos
L’image n’existant pas, elle automatiquement téléchargée. Une fois le container exécuté en interactif (option -ti), nous nous retrouvons dans l’instance :
[root@localhost ~]# docker run -it centos [root@1b57648a34da /]# ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
Création d’un fichier dans ce container :
- touch /test.txt
A partir d’un autre terminal :
- docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6ea6139f3f7 centos "/bin/bash" 12 minutes ago Up 12 minutes naughty_darwin
Pour voir les différences apportées :
- docker diff c6ea6139f3f7
C /run D /run/secrets A /test.txt
Nous voyons bien notre nouveau fichier. Pour enregistre les modifications, il faut utiliser l’option commit :
- docker commit c6ea6139f3f7 centos-test
[root@localhost ~]# docker commit c6ea6139f3f7 centos-test sha256:1b5c255408dd8b7c99803dd6456aea629a84be385344f61a900a3ede1530db23
Ainsi une nouvelle image est créée :
- docker images
REPOSITORY TAG IMAGE ID CREATED SIZE centos-test latest 1b5c255408dd About a minute ago 202 MB docker.io/centos latest 9f38484d220f 2 months ago 202 MB docker.io/eaudeweb/mailtrap latest adfbd16b7ba0 6 months ago 269 MB
Maintenant si on lance la nouvelle image (docker run -ti centos-test) nous aurons le fichier test à la racine de l’OS.
V Sauvegarder l’image
Création d’un répertoire :
- mkdir ~/lab
Sauvegarde :
- docker save centos-test > ~/lab/centos-test.tar
[root@localhost lab]# ls -als total 204596 0 drwxr-xr-x. 2 root root 29 10 juin 19:01 . 0 dr-xr-x---. 3 root root 146 10 juin 19:00 .. 204596 -rw-r--r--. 1 root root 209502720 10 juin 19:01 centos-test.tar