Docker: essential commands

updated 2026-07-01#docker#cli#cheatsheet

Lifecycle

Run, list, stop, remove: 90% of your day-to-day Docker fits in these four commands. Key detail: docker ps alone only shows running containers — add -a to also see stopped ones.

shell
docker run -d --name web -p 8080:80 nginx:1.27   # lancer en arrière-plan / run detached
docker ps -a                                     # tous les conteneurs / all containers
docker stop web                                  # arrêt propre (SIGTERM) / graceful stop
docker start web                                 # relancer un conteneur arrêté / restart a stopped one
docker rm web                                    # supprimer (doit être arrêté) / remove (must be stopped)

⚠️ docker rm -f web force-removes a running container (SIGKILL, no graceful shutdown). Handy in dev, avoid it on a production database.

Inspect & debug

Container not responding? In order: the logs, then a shell inside, then the full metadata. stats shows you live who's eating your RAM.

shell
docker logs -f --tail 100 web        # suivre les 100 dernières lignes / follow last 100 lines
docker exec -it web sh               # shell dans le conteneur / shell inside the container
docker inspect web                   # tout le JSON : IP, mounts, env / full JSON: IP, mounts, env
docker inspect -f '{{.State.Status}}' web   # un champ précis / one specific field
docker stats                         # CPU/RAM en temps réel / live CPU/RAM

Tip: docker exec requires a running container. If it crashes at startup, read docker logs web first, then check the exit code with docker inspect -f '{{.State.ExitCode}}' web.

Images

An image is the template, a container is an instance of it. Always pin a version (nginx:1.27), never latest in production: latest shifts under your feet on every pull.

shell
docker pull nginx:1.27                     # télécharger / download
docker images                              # lister les images locales / list local images
docker build -t monapp:1.0 .               # construire depuis un Dockerfile / build from Dockerfile
docker tag monapp:1.0 registry.example.com/monapp:1.0
docker push registry.example.com/monapp:1.0
docker rmi monapp:1.0                      # supprimer une image / remove an image

Volumes & networks

Data lives in volumes, not in the container: removing a container leaves its named volumes intact. Networks let containers reach each other by name (built-in DNS).

shell
docker volume create data
docker run -d --name db -v data:/var/lib/postgresql/data postgres:16
docker volume ls
docker volume inspect data                 # où c'est stocké sur l'hôte / where it lives on the host

docker network create backend
docker network connect backend web         # web peut joindre db par son nom / web can reach db by name
docker network ls

⚠️ docker volume rm data destroys the data permanently. Make sure no container uses it and you have a backup.

Pitfalls

  • The container "stops by itself": a container lives as long as its main process (PID 1) runs. If your command exits (or daemonizes), the container stops. That's by design.
  • -p 8080:80 the wrong way round: it's always host:container. The left-hand port is the one you open in your browser.
  • Editing a file inside the container: gone after the next rm + run. Anything worth keeping goes in a volume or bind mount.

Going further

These commands are the surface; to understand what happens underneath (images, layers, isolation, Compose), follow the Docker fundamentals guide in the learning path.