Installation et première utilisation sous Docker |
Article original Publié le : 11 octobre 2020 Mise a jour le : – |
Un petit tuto pense-bête sur les commandes docker
Pour rappel, pour la même image, on peut avoir plusieurs conteneur
Testé sous Ubuntu 20.04
Préparation de l’environnement
- Mise a jour du système
$ sudo apt update
- Installation des paquets nécessaires
$ sudo apt install ca-certificates software-properties-common apt-transport-https curl
- Installation de paquet utile
$ sudo apt install vim
Installation de Docker
Ajoutez la clé GPG du dépôt officiel de Docker au trousseau de clé d’apt, puis ajouter le dépôt au source.list
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Mettre à jour la base de données des paquets et installer le paquet Docker
$ sudo apt update
$ sudo apt install docker-ce
L’installation terminé, le service est démarré implicitement, une interface réseau virtuelle est crée ainsi que le client Docker
systemctl status/stop/start docker
Si on fait un id sur son utilisateur on constate qu’il nous manque le groupe docker
Il faut donc donner les droits a l’utilisateur au groupe docker pour hériter des droits sudo,
$ sudo usermod -aG docker ${USER}
ou
$ sudo usermod -aG docker bef
Fermer la session ou faire un su sur son user pour appliquer les droits au groupe, refaire un id pour vérifier
$ su - ${USER}
ou
$ su - bef
Les commandes Docker
Elle sont nombreuses mais sur ce tuto on en verra que quelques-unes
docker attach – docker diff – docker kill – docker port – docker search – docker top
docker build – docker events – docker load – docker ps – docker secret – docker trust
docker builder – docker exec – docker login – docker pull – docker service – docker unpause
docker checkpoint – docker export – docker logout – docker push – docker stack – docker update
docker commit – docker history – docker logs – docker rename – docker start – docker version
docker config – docker image – docker manifest – docker restart – docker stats – docker volume
docker container – docker images – docker network – docker rm – docker stop – docker wait
docker context – docker import – docker node – docker rmi – docker swarm –
docker cp – docker info – docker pause – docker run – docker system –
docker create – docker inspect – docker plugin – docker save – docker tag
Pour lister les images présentes sur le système (par défaut il n’y en a pas)
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
La première image que l’on peut tester est une image test ( https://hub.docker.com/_/hello-world )
Si elle n’est pas présente localement, docker ira vérifier son référentiel puis téléchargera l’image
$ docker run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
Si on relance la commande “docker images” on trouve l’image dans son repository local
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 9 months ago 13.3kB
Pour rechercher une images dans le référentiel par défaut, pour l’exemple je cherche une image centos
$ docker search centos
Les résultats retournent l’image officiel et les images alternatives faites par la communauté.
Les stars sont la popularité des images.
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6225 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 122 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 115 [OK]
centos/systemd systemd enabled base container. 86 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 83
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 47
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 46
kinogmt/centos-ssh CentOS with SSH 29 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13
guyton/centos6 From official centos6 container with full up… 10 [OK]
centos/tools Docker image that has systems administration… 6 [OK]
drecom/centos-ruby centos ruby 6 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 5
pivotaldata/centos-gcc-toolchainCentOS with a toolchain, but unaffiliated wi… 3
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
mcnaughton/centos-base centos base image 1 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
Pour récupérer l’image, comme pour un git faire un pull
$ docker pull centos
Using default tag: latest latest: Pulling from library/centos 3c72a8ed6814: Pull complete Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd Status: Downloaded newer image for centos:latest docker.io/library/centos:latest
Nous avons maintenant 2 images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB hello-world latest bf756fb1ae65 9 months ago 13.3kB
Pour créer un conteneur depuis l’image centos, utiliser les paramètres “i” pour interactif et “t” pour allouer a une sortie standard tty. Un nouveau prompt apparaît avec l’id de l’image
/!\ a chaque exécution de cette commande vers cette image il créera un nouveau conteneur et donc un nouvel id associé a ce conteneur.
$ docker run -it centos
[root@244b296b019b /]#
La vm est identifié avec son id “244b296b019b”, exemple avec la commande ps
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
244b296b019b centos "/bin/bash" 8 minutes ago Up 8 minutes recursing_robinson
Pour arrêter un conteneur en cours d’exécution, utilisez “docker stop” avec l’id ou le nom, (ou faire exit dans le conteneur)
$ docker stop recursing_robinson
ou
$ docker stop 244b296b019b
Pour (re)démarrer son conteneur on utilisera la commande “docker start” avec le paramètre “a” pour attacher l’image et “i” pour interactif suivi de l’id ou du nom du conteneur
$ docker start -ai recursing_robinson
ou
$ docker start -ai 244b296b019b
Il est possible de renommer l’image pour avoir un nom plus explicite, stopper le conteneur avant et utiliser la commande rename suivie de l’ancien et du nouveau nom puis démarrer le conteneur et vérifier avec docker ps
$ docker rename recursing_robinson centos8_original
/!\ Il est possible de créer un conteneur directement avec son nom avec l’argument –name
$ docker run -d --name nouveau_conteneur_centos8 centos
Dans le bundle de commande, il y en a aussi une pour supprimer les images et les conteneurs
Pour voir tous les conteneurs, actifs et inactifs, exécutez docker ps avec le commutateur -a
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 244b296b019b centos "/bin/bash" About an hour ago Up 8 minutes centos8_original 235d6267c213 centos "/bin/bash" About an hour ago Exited (0) About an hour ago practical_satoshi fbe630a60cfb hello-world "/hello" 19 hours ago Exited (0) 19 hours ago musing_nightingale
Pour supprimer le conteneur hello-world
$ docker rm fbe630a60cfb
On re-liste les images présentes, et on récupérer le nom ou l’id pour suppression de l’image hello-world
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB hello-world latest bf756fb1ae65 9 months ago 13.3kB
$ docker rmi hello-world
/!\ Rappel, les images et les conteneurs dispose chacun d’un nom et d’un id propre a eux
On peux créer autant de conteneur depuis une image et modifier a volonté ces conteneurs.
On peux aussi cloner un conteneur avec de nouvelles fonctionnalités pré-installés comme l’installation de programme et en faire une nouvelle image.
Dans l’exemple on va installer une application, cloner le conteneur en image pour pouvoir spawner plus tard des conteneurs avec cette application déjà installé
On peut installer n’importe quel programme sur un conteneur docker, tout comme une machine virtuelle
Pour le test j’installe juste l’éditeur de texte “vim”
[root@244b296b019b /]# yum install vim
Last metadata expiration check: 0:18:20 ago on Sat Oct 10 13:47:06 2020.
Dependencies resolved.
===================================================================================================================================
Package Architecture Version Repository Size
===================================================================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-15.el8 AppStream 39 k
vim-common x86_64 2:8.0.1763-13.el8 AppStream 6.3 M
vim-filesystem noarch 2:8.0.1763-13.el8 AppStream 48 k
which x86_64 2.21-12.el8 BaseOS 49 k
Transaction Summary
===================================================================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 31 M
Is this ok [y/N]: y
Puis faire un commit du conteneur pour en faire une nouvelle image
exemple: docker commit -m “commentaire” -a “auteur” conteneur_id repository/nouvelle_image
$ docker commit -m "Installation du paquet vim" -a "BEF" 244b296b019b img_centos8_avec_vim
Le -m est l’attribut commentaire
Le -a est l’attribut pour mettre le nom de l’auteur
244b296b019b est l’id du conteneur
img_centos8_avec_vim est le nom de la nouvelle image
Puis on re-liste les images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE img_centos8_avec_vim latest ea6cd224c331 5 minutes ago 278MB centos latest 0d120b6ccaa8 2 months ago 215MB
Cette image peut ensuite être partagé avec la communauté sur Docker Hub
Il faut avoir un compte sur la plateforme et un repository pour pouvoir pousser son image
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: user_id Password: WARNING! Your password will be stored unencrypted in /home/bef/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
Commencer par taggé l’image a pousser, exemple docker tag id_image user_id/image/tag
$ docker tag ea6cd224c331 toaster/img_centos8:latest
Puis pousser la avec la commande push
$ docker push toaster/img_centos8:latest
he push refers to repository [docker.io/toaster/img_centos8] 9539f41bd068: Pushed 291f6e44771a: Pushed latest: digest: sha256:dc03d7b603c08e2cdabdfedd08631fbdca2ed5a42ac6e2fec6679a07b65abb98 size: 741
Recuperer l’image avec la commande pull
$ docker pull toaster/img_centos8:latest
atest: Pulling from toaster/img_centos8 3c72a8ed6814: Already exists ea139865789e: Pull complete Digest: sha256:dc03d7b603c08e2cdabdfedd08631fbdca2ed5a42ac6e2fec6679a07b65abb98 Status: Downloaded newer image for toaster/img_centos8:latest docker.io/toaster/img_centos8:latest
Pour plus d’info sur les commandes
$ docker info
Quelques liens
https://docs.docker.com/engine/reference/commandline/docker/
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04