Docker Cheatsheet

Containers

Use this Docker reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Running Containers

docker run nginx                                  # foreground, blocks terminal
docker run -d nginx                               # detached (background)
docker run -it ubuntu bash                        # interactive + pseudo-TTY
docker run --rm alpine echo "hello"               # auto-remove on exit
docker run --name web nginx                       # named container
docker run -d -p 8080:80 nginx                    # host:container port mapping
docker run -d -p 127.0.0.1:8080:80 nginx          # bind to localhost only
docker run -d -P nginx                            # map ALL exposed ports to random host ports

Common docker run Flags

FlagPurposeExample
-dDetached (background)-d
-itInteractive + TTY-it bash
--rmRemove on exit--rm
--nameContainer name--name api
-p host:ctrPublish port-p 3000:3000
-PPublish all exposed ports-P
-v host:ctrBind-mount-v $PWD:/app
-e KEY=VALEnvironment variable-e NODE_ENV=prod
--env-fileLoad vars from file--env-file .env
--networkAttach to network--network mynet
--restartRestart policy--restart unless-stopped
--cpusCPU limit--cpus 1.5
--memoryMemory limit--memory 512m
-uRun as user-u 1000:1000
-wWorking directory-w /app
--entrypointOverride entrypoint--entrypoint sh

Listing and Status

docker container ls               # running containers (alias: docker ps)
docker container ls -a            # all containers including stopped
docker container ls -q            # IDs only
docker container ls --filter status=exited
docker container ls --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

Inspecting Containers

docker inspect mycontainer                    # full JSON
docker inspect -f '{{.State.Status}}' mycontainer
docker inspect -f '{{.NetworkSettings.IPAddress}}' mycontainer
docker logs mycontainer                       # stdout/stderr
docker logs -f mycontainer                    # follow (tail -f)
docker logs --tail 100 mycontainer            # last 100 lines
docker logs --since 10m mycontainer           # last 10 minutes
docker top mycontainer                        # running processes
docker stats                                  # live resource usage (all containers)
docker stats mycontainer                      # single container
docker port mycontainer                       # published port mappings

Executing Commands in a Running Container

docker exec -it mycontainer bash             # interactive shell
docker exec mycontainer ls /app              # one-off command
docker exec -u root mycontainer whoami       # run as specific user
docker exec -e DEBUG=1 mycontainer ./run.sh  # inject env var

Lifecycle Management

docker stop mycontainer          # SIGTERM, then SIGKILL after grace period
docker stop -t 30 mycontainer    # 30 s grace period
docker kill mycontainer          # immediate SIGKILL
docker start mycontainer         # restart a stopped container
docker restart mycontainer       # stop + start
docker pause mycontainer         # freeze (SIGSTOP all processes)
docker unpause mycontainer

Removing Containers

docker rm mycontainer                        # remove stopped container
docker rm -f mycontainer                     # force-remove running container
docker rm $(docker ps -aq)                   # remove all stopped containers
docker container prune                       # remove all stopped containers
docker container prune --filter "until=24h"  # older than 24 hours

Copying Files

docker cp mycontainer:/app/log.txt ./log.txt    # container → host
docker cp ./config.json mycontainer:/etc/app/   # host → container

Restart Policies

PolicyBehavior
noNever restart (default)
alwaysAlways restart; starts on daemon boot
unless-stoppedAlways restart unless manually stopped
on-failure[:N]Restart on non-zero exit, up to N times