Docker for Kubernetes · Reference

Docker CLI cheat sheet

This sheet grows with each lesson. Exact flag meanings are in the Docker CLI reference.

Container lifecycle

            docker run  (= create + start)
image ─────────────────────────────▶ running
                                       │  main process ends, or docker stop
                                       ▼
          docker start / restart ◀── exited   (writable layer kept)
                                       │  docker rm
                                       ▼
                                     gone     (writable layer deleted)

Building

CommandWhat it does
docker build -t NAME:TAG .Build an image from ./Dockerfile. The final . is the build context: the only files COPY can see.
-f path/DockerfileUse a Dockerfile somewhere else. The context is still the last argument.
-f Dockerfile.naiveBuild from a second Dockerfile in the same folder, for example to compare two versions.
--progress=plainPrint the full build log, so you can grep it: CACHED marks reused steps.
.dockerignoreNot a command: a file at the root of the context listing paths never sent to the builder, such as target/ and .git.
docker history IMAGEThe layers, newest on top, with the instruction that made each one
docker image inspect IMAGERecorded settings: CMD, exposed ports, working directory, architecture

Running

CommandWhat it does
docker run IMAGE [CMD…]Pull the image if it is missing, create a container, start its main process. A command after the image replaces the image's default.
-dRun in the background and print the container ID.
--name NGive it a name you choose. Names must be unique, even among exited containers.
-p 8080:8080Publish a port: your machine's port:the container's port. Two containers can share a container port but not a host port.
--rmRemove the container as soon as it exits. Good for one-off commands.
docker exec N CMD…Run an extra command inside a running container. Use sh -c '…' when the command has >, ; or $VAR.

Looking

CommandShows
docker ps / docker ps -aRunning containers / all containers, including exited ones
docker images [REPO]Images stored locally, with their sizes. docker images dk-hello lists every tag of one repository.
docker diff NThe writable layer: A added, C changed, D deleted
docker logs NWhat the main process printed to stdout and stderr
docker top NThe processes running inside

Stopping and cleaning up

CommandEffect
docker stop NSIGTERM, wait 10 seconds, then SIGKILL. The container and its writable layer remain.
docker start NRun the same main process again in the same container, with earlier changes intact.
docker rm N / rm -f NDelete a stopped container / stop and delete a running one. Its writable layer is gone.
docker rmi IMAGEDelete an image. Refused while any container, even an exited one, uses it.

Predict before you type

After…File written in the containerSame container?
stop, then startStill thereYes
rm, then run againGoneNo, a new one
A second container from the same imageNever thereNo, each has its own layer
Kubernetes restarts a crashed containerGone (clean state)Same Pod, fresh container filesystem

Related ideas