Guide 02 / 3
Docker & Docker Compose commands
Find the command suited to a situation and interpret its options.
Build, run, diagnose and stop services without losing data.
01 · Checking and observing
A command acts on the Docker client; the daemon then performs the operation. Each example assumes Docker is installed and reachable from the terminal.
docker version
Displays the Docker client and server versions. Lets you check communication with Docker Engine.
Example
docker version
docker info
Presents the Engine's configuration, resources and features.
Example
docker info
docker ps
Lists the running containers. With -a, includes the stopped ones.
Example
docker ps -a
docker inspect
Displays the structured details of an object: IP address, mounts, state, configuration, etc.
Example
docker inspect web
docker logs
Displays a container's standard output and errors. -f follows new lines in real time.
Example
docker logs -f web
docker stats
Shows the containers' CPU and memory usage in real time.
Example
docker stats
docker exec
Runs a command in an already started container. The -it option opens an interactive session if the tool is present.
Example
docker exec -it web sh
02 · Handling images
An image is a template: you download it, build it, name it, then start a container from it.
docker pull
Downloads an image from a registry without starting a container.
Example
docker pull nginx:alpine
docker image ls
Shows the images present locally and their tags.
Example
docker image ls
docker build
Builds an image from a Dockerfile. -t sets its name/tag; the dot indicates the current context.
Example
docker build -t monsite:1.0 .
docker image rm
Removes an unused local image, without deleting anything from a remote registry.
Example
docker image rm monsite:1.0
Can fail if the image is used by a container.
docker tag
Adds a new reference (name/tag) to an existing image.
Example
docker tag monsite:1.0 monsite:stable
docker push
Sends an image to a registry to make it available elsewhere; requires the appropriate rights.
Example
docker push registry.example.com/monapp:1.0
03 · Container lifecycle
The usual cycle goes: create → start → observe → stop → recreate if needed.
docker run
Creates then starts a container from an image. -d runs in the background; -p publishes a port; --name names the container.
Example
docker run -d --name web -p 8080:80 nginx:alpine
docker create
Creates a container without starting it.
Example
docker create --name test nginx:alpine
docker start
Starts an existing stopped container, without creating a new one.
Example
docker start web
docker stop
Requests a clean stop of a container then forces the stop after the timeout if necessary.
Example
docker stop web
docker restart
Restarts an existing container.
Example
docker restart web
docker rm
Removes a stopped container. Named volumes are normally not removed with it.
Example
docker rm web
Check the data before deleting.
docker cp
Copies files between the host and a container.
Example
docker cp web:/usr/share/nginx/html/index.html ./index.html
04 · Storage and networks
Volumes survive the recreation of a service; networks allow communication by service names.
docker volume ls
Lists the existing volumes.
Example
docker volume ls
docker volume create
Creates a named volume.
Example
docker volume create donnees
docker volume inspect
Notably shows a volume's mount point and driver.
Example
docker volume inspect donnees
docker network ls
Lists the Docker networks.
Example
docker network ls
docker network create
Creates a network that can be used to link containers.
Example
docker network create atelier
docker network inspect
Shows a network's characteristics and its connected containers.
Example
docker network inspect atelier
05 · Driving a Compose application
These commands must be run in the folder containing compose.yaml, or with -f to specify the file.
docker compose version
Displays the Docker Compose plugin version.
Example
docker compose version
docker compose config
Checks, merges and displays the resolved Compose configuration. Use it before starting.
Example
docker compose config
docker compose up
Creates and starts the services. -d detaches the terminal; --build asks to rebuild the images concerned.
Example
docker compose up -d --build
docker compose ps
Lists the current Compose project's containers and their state.
Example
docker compose ps
docker compose logs
Displays the services' logs. -f follows the output; a service name targets a specific service.
Example
docker compose logs -f web
docker compose exec
Runs a command in a service's already active container.
Example
docker compose exec web sh
docker compose run
Creates a one-off container to run a service's command.
Example
docker compose run --rm web npm test
docker compose build
Builds or rebuilds the images of the services that have build.
Example
docker compose build web
docker compose pull
Downloads the images referenced in the configuration.
Example
docker compose pull
docker compose stop
Stops the services without removing the project's containers.
Example
docker compose stop
docker compose start
Restarts existing containers that were previously stopped.
Example
docker compose start
docker compose down
Stops and removes the containers and networks created for the project; named volumes are not removed by default.
Example
docker compose down
With -v, also removes volumes: risk of data loss.
06 · Risky options and operations
Always identify a command's scope before running it.
-d / -it / --rm
-d detaches the terminal; -it allows interaction; --rm removes a one-off container when it stops.
Example
docker run --rm -it alpine sh
-p / -v / --name
-p publishes a port; -v mounts a volume or folder; --name assigns a name.
Example
docker run --name demo -p 8080:80 nginx:alpine
--env / --env-file
Passes environment variables to the container; files may contain sensitive information.
Example
docker run --env NODE_ENV=production monsite:1.0
docker system df
Shows the space used by images, containers and volumes.
Example
docker system df
docker system prune
Removes certain unused resources (notably stopped containers, unused networks and caches depending on options).
Example
docker system prune
Take stock before using it; -a widens the cleanup and --volumes can delete unused volumes.