Date de la version originale : 23 Décembre 2020 - révision 2025-12.10.01
AprĂšs avoir installĂ© Docker, nous aurons accĂšs Ă la commande âdockerâ Ă partir du âshellâ du systĂšme dâexploitation du poste de lâinstallation.
Cette commande nous permettra de gĂ©rer lâensemble des ressources disponibles sous Docker.
Si Docker est installĂ© sur une machine distante, par exemple un serveur Linux, il sera possible dây avoir accĂšs suite Ă une connexion âsshâ Ă partir dâun terminal ou en utilisant lâextension âsshâ de VS Code.
$ ssh username@adresse_ip <mot de passe ou clé tls>
# Sous Code, utiliser l'option: Open a remote window.
docker --help$ docker --help
Usage: docker [OPTIONS] COMMAND
Options:
--config string Location of client config files (default "/Users/alain/.docker")
-c, --context string Name of the context to use to connect to the daemon (...)
-D, --debug Enable debug mode
...
Management Commands:
container Manage containers
image Manage images
...
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
...
đĄNote : Sous Linux, si vous obtenez un message de permission refusĂ©e :
# Exemple d'erreur:
$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
---
Solution:
sudo groupadd docker
sudo usermod -aG docker $USER
sudo chmod 666 /var/run/docker.sock
docker <cmd> --helpExemple : docker ps --help
docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
đ Action â Afficher lâaide de la commande ârunâ
âQuestion â Ă quoi servent les options -i -t et -d ?
-it) sous git-bashhttps://willi.am/blog/2016/08/08/docker-for-windows-interactive-sessions-in-mintty-git-bash/
Solution :
$ winpty docker exec -it alpine sh
Docker Engine propose deux types de commandes :
docker container ls)docker ps)docker cmdGestion --helpExemple : docker container --help
$ docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
exec Run a command in a running container
ls List containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
...
Run 'docker container COMMAND --help' for more information on a command.
** 2.1.1 - Action â ExĂ©cuter les commandes suivantes :**
$ docker run hello-world
$ docker run alpine
$ docker run -it -d alpine
# Attention, -it et -d sont des paramĂštres de 'run', ils ne peuvent pas ĂȘtre placĂ©s ailleurs dans l'expression.
Note : âalpineâ est une image populaire servant de base Ă une image personnalisĂ©e en raison de sa trĂšs petite taille.
docker images$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 10 days ago 5.61MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
docker ps$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8026675b9b7 alpine "/bin/sh" 48 minutes ago Up 48 minutes intelligent_noether
docker ps -a$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8026675b9b7 alpine "/bin/sh" 49 minutes ago Up 49 minutes intelligent_noether
b321afb1ea0f alpine "-it -d" 49 minutes ago Created practical_lamarr
1114bbe1af61 alpine "/bin/sh" 49 minutes ago Exited (0) 49 minutes ago dazzling_zhukovsky
d09c5b00adca hello-world "/hello" 50 minutes ago Exited (0) 50 minutes ago charming_mcnulty
docker search <expression>Exemple : docker search cowsay
$ docker search cowsay
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
lherrera/cowsay 4
grycap/cowsay Alpine-less Cowsay (with Fortune) 2 [OK]
...
$ docker search alainboudreault
NAME DESCRIPTION STARS OFFICIAL
alainboudreault/phpweb Roule un petit script php qui identifie le h⊠0
alainboudreault/superminou 0
alainboudreault/docker-hub-github Lorem ipsum ... 0
alainboudreault/420-4d4-mercredi Premier pas avec hub.docker.com 0
alainboudreault/labo-01 semaine 02 0
alainboudreault/bonjour420 0
alainboudreault/momo-dit Exemple d'utilisation de variables d'environ⊠0
alainboudreault/unserveurweb Mon premier test push avec docker-hub 0
...
docker pull éditeur/imageExemple : docker pull lherrera/cowsay
$ docker pull lherrera/cowsay
Using default tag: latest
latest: Pulling from lherrera/cowsay
...
Status: Downloaded newer image for lherrera/cowsay:latest
# Afficher les images locales
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 10 days ago 5.61MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
lherrera/cowsay latest 47e12946765b 4 years ago 186MB
Note : Lorsque nous téléchargeons une image, nous obtenons toujours la derniÚre version (ex. :
alpine:latest).
docker run IDdocker run 47e12946765b
________________________________________
/ No group of professionals meets except \
| to conspire against the public at |
| large. |
\ -- Mark Twain /
----------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Un autre exemple:
$ docker run -e MESSAGE="Bonjour le monde!" alainboudreault/momo-dit
-----------------------------------------------------
Momo dit: Bonjour le monde!
-----------------------------------------------------
docker rm ID# 1 - Identifier le conteneur
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a27294467a25 47e12946765b "/entrypoint.sh" About a minute ago Exited (0) About a minute ago elegant_gauss
# 2 - Effacer le conteneur
$ docker rm a27294467a25
docker rmi ID# 1 - Obtenir l'image ID
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
...
lherrera/cowsay latest 47e12946765b 4 years ago 186MB
# 2 - Effacer l'image
$ docker rmi 47e12946765b
Untagged: lherrera/cowsay:latest
...
Deleted: sha256:47e12946765b355fb29cdd14f54e78a05d24cb5d68afc1e0e92cd4a0243a1b1a
...
đĄNote : Sâil existe des instances âconteneursâ de cette image, il faudra effacer les conteneurs avant ou bien utiliser lâoption
-fpour forcer la suppression de lâimage.
docker restart ID# Voici un conteneur terminé:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d09c5b00adca hello-world "/hello" 50 minutes ago Exited (0) 50 minutes ago charming_mcnulty
# Il peut-ĂȘtre redĂ©marrĂ© Ă partir de son ID
$docker restart d09c5b00adca$
# Ou, en utilisant le nom du conteneur:
$ docker restart charming_mcnulty
-t) interactif (-i) : docker run -it image$ docker run -it alpine
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ #
đĄNote : la commande
exitva quitter et terminer le conteneur.
docker attach IDPour se connecter à un conteneur démarré en arriÚre-plan (-it -d), utilisez docker attach ID/OU_NOM.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8026675b9b7 alpine "/bin/sh" About an hour ago Up About an hour intelligent_noether
$ docker attach intelligent_noether
/ # pwd
/
/ # mkdir 420-4C4
/ # cd 420-4C4/
/420-4C4 # touch je-suis-passé-par-ici.txt
/420-4C4 # exit
$
đĄNote : La commande
exita provoquĂ© lâarrĂȘt du conteneur.
docker restart ID$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
...
c8026675b9b7 alpine "/bin/sh" 2 hours ago Exited (0) 3 minutes ago intelligent_noether
$ docker restart c8
$ docker attach c8
/ # ls -l
total 60
drwxr-xr-x 2 root root 4096 Jan 25 18:44 420-4C4
...
đĄNote : Il nâest pas nĂ©cessaire de fournir tout le numĂ©ro dâID dans une commande. Seulement un nombre suffisant de caractĂšres pour rendre lâID unique.
# CTRL P + Q - permet de quitter sans arrĂȘter le conteneur
/ # read escape sequence
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8026675b9b7 alpine "/bin/sh" 2 hours ago Up 3 minutes intelligent_noether
$
docker stop ID$ docker stop c8
c8
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$
docker run --name$ docker run -it -d --name monAlpine alpine
ce5bde565bfa21f9b3d4f0cf8dc386cfe193c6dabbcc25a676371b51a32ec7c3
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce5bde565bfa alpine "/bin/sh" 7 seconds ago Up 6 seconds monAlpine
$ docker attach monAlpine
/ # CTRL P+Q
docker rename nom/ID nouveauNom$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce5bde565bfa alpine "/bin/sh" 7 seconds ago Up 6 seconds monAlpine
$ docker rename monAlpine test
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce5bde565bfa alpine "/bin/sh" 4 minutes ago Up 4 minutes test
Voici comment réinitialiser à zéro votre installation de docker :
đ DANGER : CETTE COMMANDE EFFACE AUSSI LES IMAGES DE MINIKUBE***
Commande pour Linux/macOS :
docker container stop $(docker container ls -aq) && docker system prune -af --volumes
Commande pour PowerShell :
docker container stop $(docker container ls -aq) ; docker system prune -af --volumes
Plusieurs images offrent des services via le protocol TCP/IP. Par dĂ©faut, le rĂ©seau du conteneur est isolĂ©, mais il est possible dâĂ©tablir un lien (BIND) entre le rĂ©seau du conteneur et lâordinateur hĂŽte.
TĂ©lĂ©chargement de lâimage :
$ docker pull nginx
...
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
docker run -p P-host:P-conteneurLancer le serveur Web sur le port 8080 de lâordinateur hĂŽte :
$ docker run -it -d -p 8080:80 --name monServeurWeb nginx
eb66bdef2f73caf6ed04e17f132d613a84f9fa15163b074d27fc3a93cbc4c4b3
# Afficher les conteneurs actifs
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb66bdef2f73 nginx "/docker-entrypoint.âŠ" 39 seconds ago Up 38 seconds 0.0.0.0:8080->80/tcp monServeurWeb
localhost:8080
https://hub.docker.com/_/nginx
Le répertoire racine du site web est localisé dans :
/usr/share/nginx/html
docker exec# 1 - Connexion au shell du conteneur nginx
$ docker exec -it monServeurWeb /bin/bash
root@eb66bdef2f73:/# cd /usr/share/nginx/html
root@eb66bdef2f73:/usr/share/nginx/html# ls
50x.html index.html
# 2 - Créer un nouveau document html
$ echo "<center><h1>Mon serveur WEB</h1></center>" > index2.html
# Ou installer nano dans le conteneur:
$ apt update $$ apt install nano -y
# 3 - Tester dans le fureteur
âQuestion : Est-ce que quitter le âshellâ avec exit va terminer le conteneur?
đĄNOTE : la commande
attachva exĂ©cuter le point dâentrĂ©e. Dans le cas de nginx, cela ne correspond pas Ă un âshellâ.

WEBSRV.80 pour la connexion au service web.docker ontainer stat$ docker container stats
---------------------------------------------------------------------------
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
eb66bdef2f73 monServeurWeb 0.00% 2.098MiB / 1.942GiB 0.11% 41.3kB / 24.1kB 65.5kB / 0B 2
CTRL+C pour quitter
docker logs ID|NAME$ docker logs monServeurWeb
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
...
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [25/Jan/2021:20:41:47 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (...)" "-"
...
docker inspect ID|NAME$ docker inspect serveur-web
$ docker inspect serveur-web | grep "IPAddress"
"IPAddress": "172.17.0.3",
$ inspect serveur-web | grep "Image"
"Image": "sha256:325b00a35073d9aa1d3df16da8afbbae1ac7d824c505f7490cd5cdbb79d60f6d",
"Image": "nginx:latest",
docker --filter$ docker container ls --filter "status=exited"
$ docker run -d --name "toto" --label "app=web-service" --label "environment=production" nginx:latest
$ docker container ls --filter "name=toto"
$ docker container ls --filter "label=env=production"
# Afficher les conteneurs d'image de type x
$ docker ps -a --filter ancestor=nginx
# Afficher avec un format personnalisé :
$ docker ps --filter ancestor=nginx --format "table \t\t\t"
Document rédigé par Alain Boudreault © 2021-2026
Version 2025.12.10.1
Site par ve2cuy