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
| Command | What 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/Dockerfile | Use a Dockerfile somewhere else. The context is still the last argument. |
-f Dockerfile.naive | Build from a second Dockerfile in the same folder, for example to compare two versions. |
--progress=plain | Print the full build log, so you can grep it: CACHED marks reused steps. |
.dockerignore | Not a command: a file at the root of the context listing paths never sent to the builder, such as target/ and .git. |
docker history IMAGE | The layers, newest on top, with the instruction that made each one |
docker image inspect IMAGE | Recorded settings: CMD, exposed ports, working directory, architecture |
Running
| Command | What 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. |
-d | Run in the background and print the container ID. |
--name N | Give it a name you choose. Names must be unique, even among exited containers. |
-p 8080:8080 | Publish a port: your machine's port:the container's port. Two containers can share a container port but not a host port. |
--rm | Remove 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
| Command | Shows |
|---|---|
docker ps / docker ps -a | Running 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 N | The writable layer: A added, C changed, D deleted |
docker logs N | What the main process printed to stdout and stderr |
docker top N | The processes running inside |
Stopping and cleaning up
| Command | Effect |
|---|---|
docker stop N | SIGTERM, wait 10 seconds, then SIGKILL. The container and its writable layer remain. |
docker start N | Run the same main process again in the same container, with earlier changes intact. |
docker rm N / rm -f N | Delete a stopped container / stop and delete a running one. Its writable layer is gone. |
docker rmi IMAGE | Delete an image. Refused while any container, even an exited one, uses it. |
Predict before you type
| After… | File written in the container | Same container? |
|---|---|---|
| stop, then start | Still there | Yes |
| rm, then run again | Gone | No, a new one |
| A second container from the same image | Never there | No, each has its own layer |
| Kubernetes restarts a crashed container | Gone (clean state) | Same Pod, fresh container filesystem |