Skip to main content

Cheatsheet | Docker

Docker provides the ability to package and run an application in a loosely isolated environment called a container.
The isolation and security allows you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can easily share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.

Ressources

Lab : Kodekloud 

Documentation

commandes générales

start the docker daemon

docker -d

help / manuel docker --help 
display system wide info docker info

list images available docker images
delete one image docker rmi imageName
delete all images (if a running cont is using image, conflict = need to remove cont) docker rmi -f $(docker images -aq)
run image ( -d to detach —name to name it) docker run imageName
stop container docker stop contName
remove all stopped containers docker container prune
remove all containers docker rm -f $(docker ps -aq)
delete image not used without a name docker image prune
pull one image docker pull imageName
delete all image cont network cache docker system prune -af --volumes

gestion de containers

runtime instance of an image

démarrer un conteneur arrêté
docker start <containerName>
redémarrer un conteneur
docker restart <containerName>
arrêter un conteneur
docker stop <containerName>
mettre en pause un conteneur
docker pause <containerName>
reprendre un conteneur
docker unpause <containerName>
afficher les logs pour debug


docker logs -f <containerName>
details sur le conteneur démarré

 

docker inspect <containerName> 
# ou
docker inspect <contName> | grep APP_COLOR #ajout de couleur pour lisibilité 
# ou
docker inspect <contName> | grep APP_COLOR -B1 -A1 #ajout 1 ligne avant & après le résultat
ressource utilisé par le conteneur

docker container stats

  • detail d'un container docker inspect <id>  
    • possible de filtrer docker inspect <id> | grep APP_COLOR
    • possible d'ajouter -B1 -A1 après le grep pour ajouter une ligne avant et après le résultat pour plus de clarté et trouver le résultat facilement
  • create & run container from image with a custom name docker run --name containerName imageName
  • run container & publiser its port(s) to the host docker run -p <hostPort>:<container:port> <imageName>
  • run in background docker run -d <imageName>

  • remove a container
    • a stopped one docker rm <containerName>
    • all of them containers, including running ones docker rm -f $(docker ps -aq)
  • open shell in running container docker exec -it <containerName> sh

afficher les conteneurs démarrés docker ps
afficher tous les conteneurs  docker ps -a (a pour all)
afficher les logs pour debug

docker logs -f containerName

afficher l'ID des conteneurs utilisant une image X (not running)

docker container ls -a -q --filter "ancestor=x"

 

possible d'ajouter d'autres filtres 

docker container ls -a -q --filter "ancestor=x" --filter "status=exited"




image 

  • build an image from a dockerfile docker build -t <imageName> .
  • build an image from a dockerfile without the cache docker build -t <imageName> . -no-cache
  • list local images docker images
  • delete an image docker rmi <imageName> → rmi for remove image 
  • remove all unused images docker image prune
  • find base OS for x image docker run --rm x cat /etc/os-release (exemple x = python:3.6)
  • find image size docker image inspect <imageNAme> --format '{{.Size}}'

hub

service provided by Docker to find and share images

log in docker login -u <username>

publish docker push <username>/<imageName>

get docker pull <imageName>

nettoyage

Les commandes prune ne suppriment jamais les conteneurs en cours d’exécution, seulement ceux arrêtés ou les objets « non utilisés ». 

Utiliser sans -f pour voir les warning mais besoin de l'ajouter dans les scripts car non interractifs. 

Supprimer tous les conteneurs arrêtés
exclut les conteneurs qui tournent

docker container prune
docker container prune --filter "until=24h"   # plus vieux que 24h

Supprimer les images « dangling »
(sans tag, sans référence)

docker image prune

⚠ Supprimer toutes les images non utilisées par un conteneur ⚠

docker image prune -a
docker image prune -a --filter "until=2024-01-01T00:00:00"   # plus anciennes qu’une date

Volumes inutilisés (non attachés à un conteneur)
⚠ à la perte de données (DB, etc.) 

 

 

docker volume prune

 

Réseaux non utilisés 

docker network prune

Nettoyage global (conteneurs arrêtés, images inutilisées, réseaux inutilisés, build cache)

docker system prune
docker system prune -a              # inclut toutes les images non utilisées
docker system prune -a --volumes    # inclut aussi les volumes inutilisés (danger)