Files
DEV/DEV.DOCKER/TP1/COMMANDS.md
T
EmmanuelTiamzon d12a1adb6d ajout dev docker
2026-06-04 18:57:15 +02:00

213 lines
8.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Commandes utilisées - TP1
## 2. Installation
### 2.2 Vérification de l'installation :
```shell
sudo docker --version
# Docker version 28.3.3, build 980b856816
sudo docker run hello-world
# Unable to find image 'hello-world:latest' locally
# latest: Pulling from library/hello-world
# 4f55086f7dd0: Pull complete
# Digest: sha256:452a468a4bf985040037cb6d5392410206e47db9bf5b7278d281f94d1c2d0931
# Status: Downloaded newer image for hello-world:latest
# Hello from Docker!
```
## 3 Premiers pas avec Docker
### 3.1 Images Docker
### 3.1.1 Lister les images disponibles
```shell
sudo docker images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# hello-world latest e2ac70e7319a 2 weeks ago 10.1kB
# prog_python latest 3dabe55bd71c 7 weeks ago 1.12GB
# <none> <none> 21d22a499c76 7 weeks ago 1.12GB
# python 3.15-rc-trixie 3bf0ec7eb030 2 months ago 1.12GB
# imunes/template latest 58009592de73 2 years ago 387MB
```
### 3.1.2 Télécharger une image
```shell
sudo docker pull nginx
# Using default tag: latest
# latest: Pulling from library/nginx
# 5435b2dcdf5c: Pull complete
# 054715a6bffa: Pull complete
# 88d1d984b765: Pull complete
# 4a038fd18db1: Pull complete
# 84e114c2bb36: Pull complete
# 7b5d674621c2: Pull complete
# 448ea5cac5d5: Pull complete
# Digest: sha256:7f0adca1fc6c29c8dc49a2e90037a10ba20dc266baaed0988e9fb4d0d8b85ba0
# Status: Downloaded newer image for nginx:latest
# docker.io/library/nginx:latest
```
### 3.1.3 Rechercher une image
```shell
sudo docker search ubuntu
# NAME DESCRIPTION STARS OFFICIAL
# ubuntu Ubuntu is a Debian-based Linux operating sys… 17808 [OK]
# ubuntu/squid Squid is a caching proxy for the Web. Long-t… 125
# ...
```
## 3.2 Gestion basique des conteneurs
1. Créer un premier conteneur :
```shell
docker run nginx
# Il tente de start le process/conteneur nginx et reste en stand by. On peut donc l'arrêter avec <CTRL>+C
```
2.
```shell
docker run -d nginx
# 30dfd1b500397ea6b1845ba194f583dd8bae8e55ba828f2ddc2819ede8d7ed00
```
## 4 Cycle de vie des conteneurs
### 4.1.1 Commandes d'observation
```shell
docker ps # conteneurs en cours d'exécution
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 30dfd1b50039 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp admiring_hermann
docker ps -a # tous les conteneurs
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 30dfd1b50039 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp admiring_hermann
# 5a9d4e300f22 nginx "/docker-entrypoint.…" 4 minutes ago Exited (0) 4 minutes ago infallible_banach
# Pour supprimer toutes les images il faut faire :
docker system prune
```
### 4.1.2 Exercice pratique
1. Créer trois conteneurs nginx avec des noms différents :
```shell
docker run -d --name web1 nginx
# 9de9a334b5001da00fb27ae33e3fc09f4b29a78e9b3606d9cdf61acebaf9941e
docker run -d --name web2 nginx
# db98128c8820faa3720643f7a83d9126a11bf27522a24db7218539023f680a6c
docker run -d --name web3 nginx
# a0d91897890b1eb028571846e5aced4a76bbae8c8c970222a95185b0342e697e
```
2. Les états de chaque conteneurs se remarquent en faisant
3.
```shell
docker ps # Après avoir lancé les conteneurs précédents
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# a0d91897890b nginx "/docker-entrypoint.…" 39 seconds ago Up 38 seconds 80/tcp web3
# db98128c8820 nginx "/docker-entrypoint.…" 52 seconds ago Up 51 seconds 80/tcp web2
# 9de9a334b500 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp web1
```
## 4.2 Manipulation des états
```shell
docker stop web1
# ça renvoie : web1 et en faisant docker ps, on voit qu'il n'apparait plus.
docker start web1
# ça renvoie : web1
docker restart web2
# Renvoie web2 et en faisant ps on voit que son timer a recommencé
docker pause web3
# renvoie web3 et affiche ceci dans ps : a0d91897890b nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes (Paused) 80/tcp web3
docker unpause web3 # le remet à la normal
docker kill web1 # n'apparait plus dans le ps. Et dans ps -a : 9de9a334b500 nginx "/docker-entrypoint.…" 12 minutes ago Exited (137) 28 seconds ago web1
```
# 5 Inspection et Debug
## 5.1 Logs et monitoring
```shell
docker logs [container-name]
# montre les logs de l'image actuelle du container
docker logs -f [container-name]
# montre les logs du container en temps réel
docker stats
# CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
# a0d91897890b web3 0.00% 10.43MiB / 15.42GiB 0.07% 3.15kB / 126B 0B / 12.3kB 13
# db98128c8820 web2 0.00% 10.41MiB / 15.42GiB 0.07% 2.83kB / 126B 0B / 4.1kB 13
# 30dfd1b50039 admiring_hermann 0.00% 10.43MiB / 15.42GiB 0.07% 6.26kB / 126B 0B / 12.3kB 13
```
## 5.2 Inspection détaillée
```shell
docker inspect [container-name]
# "Id": "db98128c8820faa3720643f7a83d9126a11bf27522a24db7218539023f680a6c",
# "Created": "2026-04-13T09:08:18.800415878Z",
# "Path": "/docker-entrypoint.sh",
docker exec -it [container-name] /bin/bash
# renvoie : root@db98128c8820:/# Il permet d'exécuter des commandes dans un container en tant que root
```
# 6 Exercice final
```shell
# 1. Créer un conteneur nginx en mode détaché avec le nom ”web-test”
docker run -d --name web-test nginx
fac3038fbd6c11d119683f146c9be888158e3a145b1ba7050caad8283bb5ac78
docker docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fac3038fbd6c nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp web-test
# 2. Pratiquer toutes les commandes du cycle de vie vues précédemment
```shell
[srivasta@salle234-09 TP1]$ docker stop web-test
web-test
[srivasta@salle234-09 TP1]$ docker start web-test
web-test
[srivasta@salle234-09 TP1]$ docker restart web-test
web-test
[srivasta@salle234-09 TP1]$ docker pause web-test
web-test
[srivasta@salle234-09 TP1]$ docker unpause web-test
web-test
[srivasta@salle234-09 TP1]$ docker kill web-test
web-test
```
# 3. Analyser les logs et les différents états
```shell
[srivasta@salle234-09 TP1]$ docker logs web-test
# Le log est TRES long car l'enchainement de start, restart, stop, kill, chaque démarrage ou arrêt, Nginx lance ou coupe des dizaines de sous-processus ("workers"), ce qui génère instantanément une cascade de lignes pour chaque action.
docker inspect web-test
# On voit que ça renvoie un JSON avec l'état actuel (le conteneur est éteint "Status": "exited", "Running": false). Le code d'erreur "ExitCode": 137 indique qu'il a été arrêté brutalement avec le kill que j'ai utilisé. Il est basé sur l'image "nginx", est configuré pour exposer le port "80/tcp".
# 4. Nettoyer lenvironnement (suppression des conteneurs)
docker stop web-test # Il n'apparait donc plus lorsque l'on fait docker ps
```
# 7 Travaux Pratiques Supplémentaires
## 7.1 Rapatrier l'image officielle du serveur web apache (httpd)
```shell
docker pull httpd:alpine3.17
# Digest: sha256:03c154f29d68648c335a19c8bfca1562251bc8af534e10c6f87361551c381d21
# Status: Downloaded newer image for httpd:alpine3.17
# docker.io/library/httpd:alpine3.17
```
## 7.2 Créer un volume
```shell
docker volume create volume_serveur_web #on crée le volume
docker run --rm -v volume_serveur_web:/data alpine sh -c 'echo "<!DOCTYPE html><html><title>C MOI</title><body><h1>Emmanuel</h1></br><p>SRIVASTAVA-TIAMZON</p></body></html>" >> /data/index.html' # On crée le volume qui contient le fichier html demandé.
```
## 7.3 Démarrer un conteneur
```shell
docker run -d --name tp21 httpd # Démarrer un conteneur en mode détaché nommé tp21
docker run -d --name tp22 -v volume_serveur_web:/usr/local/apache2/htdocs/ httpd:alpine3.17 # Démarrer un conteneur en mode détaché en montant le volume précédemment créé nommé tp22
docker run -d --name tp23 -v volume_serveur_web:/usr/local/apache2/htdocs/ -p 8080:80 httpd:alpine3.17 # Démarrer un conteneur en mode détaché en montant le volume précédemment créé et en exposant le port 80 nommé tp23
```