Docker & Docker Compose — The course

Guide 02 / 3

Docker & Docker Compose commands

Find the command suited to a situation and interpret its options.

Expected outcome :

Build, run, diagnose and stop services without losing data.

Reading
0%

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.

docker version

docker info

Presents the Engine's configuration, resources and features.

docker info

docker ps

Lists the running containers. With -a, includes the stopped ones.

docker ps -a

docker inspect

Displays the structured details of an object: IP address, mounts, state, configuration, etc.

docker inspect web

docker logs

Displays a container's standard output and errors. -f follows new lines in real time.

docker logs -f web

docker stats

Shows the containers' CPU and memory usage in real time.

docker stats

docker exec

Runs a command in an already started container. The -it option opens an interactive session if the tool is present.

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.

docker pull nginx:alpine

docker image ls

Shows the images present locally and their tags.

docker image ls

docker build

Builds an image from a Dockerfile. -t sets its name/tag; the dot indicates the current context.

docker build -t monsite:1.0 .

docker image rm

Removes an unused local image, without deleting anything from a remote registry.

docker image rm monsite:1.0
Remember :

Can fail if the image is used by a container.

docker tag

Adds a new reference (name/tag) to an existing image.

docker tag monsite:1.0 monsite:stable

docker push

Sends an image to a registry to make it available elsewhere; requires the appropriate rights.

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.

docker run -d --name web -p 8080:80 nginx:alpine

docker create

Creates a container without starting it.

docker create --name test nginx:alpine

docker start

Starts an existing stopped container, without creating a new one.

docker start web

docker stop

Requests a clean stop of a container then forces the stop after the timeout if necessary.

docker stop web

docker restart

Restarts an existing container.

docker restart web

docker rm

Removes a stopped container. Named volumes are normally not removed with it.

docker rm web
Remember :

Check the data before deleting.

docker cp

Copies files between the host and a container.

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.

docker volume ls

docker volume create

Creates a named volume.

docker volume create donnees

docker volume inspect

Notably shows a volume's mount point and driver.

docker volume inspect donnees

docker network ls

Lists the Docker networks.

docker network ls

docker network create

Creates a network that can be used to link containers.

docker network create atelier

docker network inspect

Shows a network's characteristics and its connected containers.

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.

docker compose version

docker compose config

Checks, merges and displays the resolved Compose configuration. Use it before starting.

docker compose config

docker compose up

Creates and starts the services. -d detaches the terminal; --build asks to rebuild the images concerned.

docker compose up -d --build

docker compose ps

Lists the current Compose project's containers and their state.

docker compose ps

docker compose logs

Displays the services' logs. -f follows the output; a service name targets a specific service.

docker compose logs -f web

docker compose exec

Runs a command in a service's already active container.

docker compose exec web sh

docker compose run

Creates a one-off container to run a service's command.

docker compose run --rm web npm test

docker compose build

Builds or rebuilds the images of the services that have build.

docker compose build web

docker compose pull

Downloads the images referenced in the configuration.

docker compose pull

docker compose stop

Stops the services without removing the project's containers.

docker compose stop

docker compose start

Restarts existing containers that were previously stopped.

docker compose start

docker compose down

Stops and removes the containers and networks created for the project; named volumes are not removed by default.

docker compose down
Remember :

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.

docker run --rm -it alpine sh

-p / -v / --name

-p publishes a port; -v mounts a volume or folder; --name assigns a name.

docker run --name demo -p 8080:80 nginx:alpine

--env / --env-file

Passes environment variables to the container; files may contain sensitive information.

docker run --env NODE_ENV=production monsite:1.0

docker system df

Shows the space used by images, containers and volumes.

docker system df

docker system prune

Removes certain unused resources (notably stopped containers, unused networks and caches depending on options).

docker system prune
Remember :

Take stock before using it; -a widens the cleanup and --volumes can delete unused volumes.