sehaj/learn
Docker/01 Foundations

01.06 · with code · 12 min

The Docker CLI, mapped

The Docker CLI is one pattern repeated: docker, a noun, a verb. Learn the pattern and the twenty commands you'll actually use.

The core idea

Nearly every Docker command has the same shape:

The shape of a command
docker
containerobject
lsverb
--allflags
Objects: container, image, volume, network, builder, system. Verbs: ls, inspect, rm, prune, and a few more.

The most common commands also have older short forms. docker ps is docker container ls, docker images is docker image ls, docker rmi is docker image rm. Both work; you'll see both in the wild.

The commands you'll actually use

TaskCommand
Run a containerdocker run [flags] image [command]
List running containersdocker ps (add -a for stopped ones too)
Stop, start, restartdocker stop web, docker start web, docker restart web
Remove a containerdocker rm web (add -f to stop it first)
See logsdocker logs -f --tail 50 web
Run a command insidedocker exec -it web sh
Live resource usedocker stats
Everything about itdocker inspect web
List imagesdocker images
Download, uploaddocker pull nginx:alpine, docker push ghcr.io/you/api:1.0
Build an imagedocker build -t my-api .
Remove an imagedocker rmi my-api
Disk usagedocker system df
Clean updocker system prune

docker run, flag by flag

docker run has dozens of flags, but these cover most days:

run
docker run \
-d \
--name api \
-p 8080:3000 \
-e NODE_ENV=production \
-v api-data:/data \
--rm \
my-api:1.0
FlagMeans
-ddetached: run in the background and print the container ID
--name apia name to use instead of the random one Docker picks
-p 8080:3000host port 8080 forwards to container port 3000
-e KEY=valueset an environment variable
-v api-data:/datamount the volume api-data at /data (04.02 Volumes)
--rmdelete the container when it exits
-itinteractive with a terminal; use it for shells, not servers

Anything after the image name replaces the image's default command: docker run --rm -it ubuntu bash.

run, start and exec

These three trip people up because they all sound like "make it go":

  • docker run creates a new container from an image and starts it.
  • docker start starts an existing, stopped container again, with the same writable layer.
  • docker exec runs an extra process inside a container that's already running.
Predict first

You run docker run -it ubuntu bash, create a file, then type exit. What state is that container in?

Commit to an answer before you read on.

Finding things out

Two habits save a lot of guessing. Read logs before anything else, and use inspect with --format to pull out one field instead of scrolling through JSON:

❯terminal
$ docker logs --tail 20 web
$ docker inspect --format '{{.State.Status}} {{.State.ExitCode}}' web
exited 137
$ docker inspect --format '{{.NetworkSettings.IPAddress}}' web

An exit code of 137 means the container was killed (01.03 Under the hood: namespaces and cgroups).

Cleaning up

Docker never deletes anything on its own, so disk usage grows. See where it went, then prune:

❯terminal
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 14 3 4.2GB 3.1GB (73%)
Containers 9 2 180MB 160MB (88%)
Local Volumes 5 2 1.1GB 600MB (54%)
Build Cache 61 0 2.3GB 2.3GB
$ docker system prune

Example output. docker system prune removes stopped containers, unused networks, dangling images and build cache. It does not remove volumes unless you add --volumes, and only removes images without a tag unless you add -a. Both of those flags can delete things you want, so read the prompt before saying yes.

CheckpointRun, start, exec and clean up

Do each of these and predict the result before you press enter:

❯terminal
$ docker run -it --name box ubuntu bash
$ touch /keep-me && exit
$ docker ps -a --filter name=box
$ docker start -ai box
$ ls /keep-me
$ exit
$ docker rm box
$ docker system df

The file survives the stop and start because it's the same container. It's gone only after docker rm.

STUCK? ▸

If docker start -ai box says the container doesn't exist, you probably ran the first command with --rm. Leave it off for this exercise.

Recap

Interview questions

JuniorfreqMust-knowConcept

What's the difference between docker run, docker start and docker exec?

MidfreqGood to knowDebugging

A CI server running Docker is out of disk. How do you find what's using space and reclaim it safely?

Report a mistakeEdit this page