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
| Flag | Purpose | Example |
|---|---|---|
-d | Detached (background) | -d |
-it | Interactive + TTY | -it bash |
--rm | Remove on exit | --rm |
--name | Container name | --name api |
-p host:ctr | Publish port | -p 3000:3000 |
-P | Publish all exposed ports | -P |
-v host:ctr | Bind-mount | -v $PWD:/app |
-e KEY=VAL | Environment variable | -e NODE_ENV=prod |
--env-file | Load vars from file | --env-file .env |
--network | Attach to network | --network mynet |
--restart | Restart policy | --restart unless-stopped |
--cpus | CPU limit | --cpus 1.5 |
--memory | Memory limit | --memory 512m |
-u | Run as user | -u 1000:1000 |
-w | Working directory | -w /app |
--entrypoint | Override 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
| Policy | Behavior |
|---|---|
no | Never restart (default) |
always | Always restart; starts on daemon boot |
unless-stopped | Always restart unless manually stopped |
on-failure[:N] | Restart on non-zero exit, up to N times |