Ravindra BagaleCourses & study guides मराठी

Chapter 56: Docker CI/CD with Jenkins and ECR, Deploy to Amazon ECS, Troubleshooting, Best Practices and Interview Questions

56.6 The complete Docker CLI reference, grouped and tried once each

Friends, Docker has more than a hundred commands and flags. Nobody remembers all of them, and you do not need to. You need to know the groups, the ten or fifteen commands you use every day, and how to find the rest with --help. Today we walk through every group of the Docker CLI: system, images, containers, exec and logs, files, networks, volumes, build, Compose, registry, contexts and cleanup. For each group we run three commands so your fingers remember them.

1. Start from a story

At TCS, a new joiner named Pooja was given a printed sheet of fifty Docker commands. She tried to memorise it and forgot most of it in a week.

Her mentor threw the sheet away and drew twelve boxes on a whiteboard, one per group. Inside each box he wrote only three commands. Then he showed her docker COMMAND --help and the --format flag.

Pooja practised one box per day. In two weeks she could find any command she needed, because she knew which box it lived in.

Today we fill those twelve boxes.

dockerthe clientmanagement groupimage, container, network, volume...actionls, inspect, rm, prune...flags--format, --filter, -f--help at every levelfind anything
Picture 1: how the CLI is organised

2. The twelve groups

Step 1: System

Why: Is Docker healthy?

What: System commands show the version, the daemon details and the disk usage. Start here on any new machine.

Example 1: Version

Type this:

docker version --format 'client {{.Client.Version}} server {{.Server.Version}}'

You will see:

client 27.3.1 server 27.3.1

What it means: Client and daemon both answer.

Example 2: Info

Type this:

docker info --format '{{.Containers}} containers, {{.Images}} images, driver {{.Driver}}'

You will see:

7 containers, 23 images, driver overlay2

What it means: A quick health summary.

Example 3: Disk usage

Type this:

docker system df

You will see:

TYPE           TOTAL  ACTIVE  SIZE    RECLAIMABLE
Images         23     6       8.1GB   5.2GB (64%)
Containers     7      4       42MB    10MB
Local Volumes  5      3       1.3GB   300MB
Build Cache    41     0       2.2GB   2.2GB

What it means: What uses the disk.

Step 2: Images

Why: Get, list, tag, remove.

What: docker image has pull, ls, inspect, history, tag, rm, save, load and prune. The short forms docker pull and docker images still work.

Example 4: Pull and list

Type this:

docker pull -q nginx:1.27-alpine && docker image ls nginx

You will see:

nginx  1.27-alpine  a5967740120f  2 weeks ago  47.9MB

What it means: Downloaded and listed.

Example 5: Tag

Type this:

docker tag nginx:1.27-alpine registry.example.com/web:1.0 && docker image ls registry.example.com/web --format '{{.Repository}}:{{.Tag}}'

You will see:

registry.example.com/web:1.0

What it means: A second name for the same image.

Example 6: Save and load

Type this:

docker save -o web.tar nginx:1.27-alpine && docker load -i web.tar

You will see:

Loaded image: nginx:1.27-alpine

What it means: Move images without a registry.

Step 3: Containers: run and lifecycle

Why: Start, stop, remove.

What: run creates and starts. Then ps, stop, start, restart, pause, unpause, kill and rm manage the lifecycle.

Example 7: Run

Type this:

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

You will see:

3f9a1c...

What it means: Running in the background.

Example 8: Lifecycle

Type this:

docker stop web && docker start web && docker restart web && docker ps --filter name=web --format '{{.Status}}'

You will see:

web
web
web
Up 1 second

What it means: Stop, start and restart.

Example 9: Remove

Type this:

docker rm -f web

You will see:

web

What it means: Stops and deletes in one step.

Step 4: Containers: look inside

Why: Logs, exec, top, stats.

What: logs shows output, exec runs a command inside, top lists processes, stats shows usage, inspect shows every detail.

Example 10: Logs

Type this:

docker run -d --name web nginx:1.27-alpine >/dev/null && docker logs --tail 2 web

You will see:

... start worker processes

What it means: The app output.

Example 11: Exec and top

Type this:

docker exec web nginx -v && docker top web -o pid,comm

You will see:

nginx version: nginx/1.27.2
PID   COMMAND
2210  nginx
2251  nginx

What it means: Inside and process list.

Example 12: Inspect one field

Type this:

docker inspect --format '{{.NetworkSettings.IPAddress}}' web

You will see:

172.17.0.2

What it means: Pick any field with --format.

Step 5: Files

Why: Copy and compare.

What: cp copies files between host and container. diff shows what changed inside the container since it started. export dumps the filesystem.

Example 13: Copy out

Type this:

docker cp web:/etc/nginx/nginx.conf ./nginx.conf && wc -l nginx.conf

You will see:

32 nginx.conf

What it means: A file from the container.

Example 14: Copy in

Type this:

printf '%s\n' 'hello' > hi.html && docker cp hi.html web:/usr/share/nginx/html/ && docker exec web cat /usr/share/nginx/html/hi.html

You will see:

hello

What it means: A file into the container.

Example 15: Diff

Type this:

docker diff web | head -3

You will see:

C /usr
A /usr/share/nginx/html/hi.html
C /var/cache/nginx

What it means: A added, C changed, D deleted.

Step 6: Networks

Why: Connect containers.

What: network create, ls, inspect, connect, disconnect and rm. Containers on the same user network find each other by name.

Example 16: Create and use

Type this:

docker network create shopnet && docker run -d --name db --network shopnet -e POSTGRES_PASSWORD=example-only postgres:16-alpine

You will see:

shopnet
8b2e...

What it means: A private network with a database.

Example 17: Connect

Type this:

docker network connect shopnet web && docker exec web getent hosts db

You will see:

172.19.0.2  db

What it means: web finds db by name.

Example 18: Inspect

Type this:

docker network inspect shopnet --format '{{range .Containers}}{{.Name}} {{end}}'

You will see:

db web

What it means: Who is on the network.

Step 7: Volumes

Why: Keep data.

What: volume create, ls, inspect and rm, and -v or --mount on run to attach them.

Example 19: Create

Type this:

docker volume create pgdata && docker volume ls --format '{{.Name}}' | grep pgdata

You will see:

pgdata

What it means: A named volume.

Example 20: Use with --mount

Type this:

docker run -d --name db2 --mount source=pgdata,target=/var/lib/postgresql/data -e POSTGRES_PASSWORD=example-only postgres:16-alpine && docker inspect --format '{{range .Mounts}}{{.Name}}{{end}}' db2

You will see:

pgdata

What it means: Data lives outside the container.

Example 21: Where is it

Type this:

docker volume inspect --format '{{.Mountpoint}}' pgdata

You will see:

/var/lib/docker/volumes/pgdata/_data

What it means: The host path.

Step 8: Build

Why: From Dockerfile to image.

What: build with -t, -f, --build-arg, --target, --no-cache and --platform. buildx adds multi-platform builds and cache export.

Example 22: Basic build

Type this:

docker build -t shop-api:1.0 .

You will see:

=> naming to docker.io/library/shop-api:1.0

What it means: Built from the Dockerfile in this folder.

Example 23: Target stage and arg

Type this:

docker build --target build --build-arg NODE_ENV=test -t shop-api:test .

You will see:

=> naming to docker.io/library/shop-api:test

What it means: Stop at the build stage.

Example 24: Multi-platform

Type this:

docker buildx build --platform linux/amd64,linux/arm64 -t registry.example.com/shop-api:1.0 --push .

You will see:

=> pushing manifest list

What it means: One tag for Intel and ARM servers.

Step 9: Compose

Why: Many containers from one file.

What: compose up, down, ps, logs, exec, build, pull and config. Always run config first to check the file.

Example 25: Check the file

Type this:

docker compose config --services

You will see:

web
api
db

What it means: The file is valid.

Example 26: Up and status

Type this:

docker compose up -d && docker compose ps --format '{{.Service}} {{.State}}'

You will see:

web running
api running
db running

What it means: The whole stack.

Example 27: Logs and down

Type this:

docker compose logs --tail 1 api && docker compose down

You will see:

api-1  | listening on 3000
[+] Running 4/4 Removed

What it means: Read and tear down.

Step 10: Registry

Why: Log in, push, pull.

What: login, logout, push, pull and search. Use --password-stdin so passwords never sit in shell history.

Example 28: Log in safely

Type this:

printf '%s' "$REG_TOKEN" | docker login registry.example.com -u ci --password-stdin

You will see:

Login Succeeded

What it means: Token from an environment variable, example only.

Example 29: Push

Type this:

docker push registry.example.com/web:1.0

You will see:

1.0: digest: sha256:7d1b... size: 1568

What it means: Uploaded.

Example 30: Search Docker Hub

Type this:

docker search --limit 3 --filter is-official=true redis --format '{{.Name}}'

You will see:

redis

What it means: Official images only.

Step 11: Contexts and plugins

Why: Other Dockers and extra tools.

What: context switches which daemon the CLI talks to. Plugins like buildx, compose, scout and debug add new commands.

Example 31: Contexts

Type this:

docker context ls --format '{{.Name}} {{.Current}}'

You will see:

default true
staging false

What it means: Local is active.

Example 32: Plugins

Type this:

docker info --format '{{range .ClientInfo.Plugins}}{{.Name}} {{end}}'

You will see:

buildx compose debug scout

What it means: Installed plugins.

Example 33: Help at any level

Type this:

docker container run --help | grep -m2 -- '--memory'

You will see:

-m, --memory bytes   Memory limit

What it means: Find any flag fast.

Step 12: Cleanup

Why: Free space safely.

What: prune exists for containers, images, networks, volumes, builder and system. Read what it will delete. Volume prune deletes data.

Example 34: Stopped containers

Type this:

docker container prune -f

You will see:

Deleted Containers: 5d2c...
Total reclaimed space: 10MB

What it means: Only stopped ones.

Example 35: Unused images and build cache

Type this:

docker image prune -f && docker builder prune -f --filter until=72h

You will see:

Total reclaimed space: 2.9GB

What it means: Dangling images and old cache.

Example 36: Clean the lab

Type this:

docker rm -f web db db2 && docker network rm shopnet && docker volume rm pgdata

You will see:

web
db
db2
shopnet
pgdata

What it means: We remove exactly what we created.

run, ps, logsstart and watchexec, inspect, statslook insidebuild, tag, push, pullimagescompose up, downstackssystem df, prunekeep it clean
Picture 2: the everyday fifteen

3. Short forms and long forms

Docker has two ways to write many commands. The old short form is docker ps, docker images, docker rmi and docker rm. The newer long form puts the object first: docker container ls, docker image ls, docker image rm and docker container rm. Both do exactly the same thing.

Pooja's mentor told her to read the long form in documentation, because the group name tells you which box the command lives in, and to type the short form at the keyboard, because it is faster. In scripts, the team prefers the long form so a new reader understands the script without guessing.

When you meet a new command in a blog post, ask two questions: which group is this, and which action is it. That one habit makes the whole CLI feel small.

4. Common mistakes and how Pooja avoids them

Mistake one: memorising flags. Pooja uses --help and --format.

Mistake two: reading huge inspect output. She picks the field with --format.

Mistake three: system prune with --volumes on a server. She prunes each type on purpose and never volumes without checking.

Mistake four: passwords on the login command line. She always uses --password-stdin.

Check yourself

Q1. Which command shows what uses the disk?

docker system df.

Q2. How do you get one field from inspect?

--format with a Go template, for example '{{.State.Status}}'.

Q3. Which command copies a file out of a container?

docker cp container:path hostpath.

Q4. What should you run before compose up?

docker compose config to check the file.

Friends, learn the twelve groups, practise three commands from each, and use --help and --format for everything else. Did you understand? Let us try it once more. Pick one group a day, run its three commands from memory, and write one new flag you discovered with --help.

Ravindra Bagale's Tip

Learn --format once and you will never scroll through inspect output again.

Lab: try at home

  1. Run the three system commands on your machine.
  2. Pull, tag, save and load one image.
  3. Run a container and walk it through its lifecycle.
  4. Copy a file in and out, then read docker diff.
  5. Create a network and a volume and use both.
  6. Build one image for two platforms with buildx.
  7. Clean up only what you created.

Recap

  • Docker CLI is grouped: system, image, container, network, volume, build, compose, registry, context.
  • --help works at every level.
  • --format and --filter make output readable.
  • Log in with --password-stdin.
  • Prune carefully; volumes hold data.