PUBLIC REFERENCE // DOCKER

Docker

Docker images are built in layers, and each instruction in a Dockerfile creates a new cached layer — understanding that caching model is the difference between a build that takes seconds and one that reinstalls every dependency from scratch every time.

Commands
79
Sections
8
Source policy
Official links

FIELD NOTES

Decisions worth remembering

  • Order Dockerfile instructions from least to most frequently changing (e.g. copy package.json and install dependencies before copying the rest of the source) so Docker can reuse cached layers.
  • A named volume (docker volume create) is managed by Docker and survives container removal; a bind mount just maps a host path in and is better for local development where you want live file edits.
  • Avoid running processes as root inside containers — set a USER in the Dockerfile — since a container escape as root has far higher impact than as an unprivileged user.
  • docker system prune removes stopped containers, unused networks, and dangling images, but add -a if you also want to remove unused (not just dangling) images when disk space is tight.

DOCKER // REFERENCE

Images

10 commands
docker rmi nginx

Remove an image

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker rmi $(docker images -q)

Remove all images

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

DOCKER // REFERENCE

Container Lifecycle

16 commands
docker run --rm nginx

Auto-remove container when it exits

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker rm container_id

Remove a stopped container

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker rm -f container_id

Force remove a running container

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

DOCKER // REFERENCE

Exec & Logs

10 commands

DOCKER // REFERENCE

Docker Compose

12 commands
docker compose down -v

Also remove named volumes

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

DOCKER // REFERENCE

Volumes

7 commands
docker volume rm myvolume

Remove a volume

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker volume prune

Remove all unused volumes

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

DOCKER // REFERENCE

Networking

8 commands
docker network rm mynetwork

Remove a network

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

DOCKER // REFERENCE

Registry

7 commands

DOCKER // REFERENCE

System & Cleanup

9 commands
docker system prune

Remove stopped containers, unused networks, dangling images

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker system prune -a

Also remove all unused images (not just dangling)

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker system prune --volumes

Also remove unused volumes

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker container prune

Remove all stopped containers

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker image prune

Remove dangling images (untagged)

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

docker image prune -a

Remove all unused images

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

COMMON QUESTIONS

Docker command FAQ

Why is my Docker image so large?

Usually unnecessary build tools or cache left in the final layer — use multi-stage builds to copy only the compiled artifact into a slim final image.

What is the difference between CMD and ENTRYPOINT in a Dockerfile?

ENTRYPOINT defines the fixed executable that always runs; CMD supplies default arguments that can be overridden from the command line — combining both gives you a fixed binary with overridable defaults.

Why can't my container connect to a service running on my host machine?

localhost inside a container refers to the container itself, not the host — use host.docker.internal (Docker Desktop) or the host's actual network IP.

How do I see why a container exited immediately?

docker logs <container> shows its last output, and docker inspect <container> --format='{{.State.ExitCode}}' shows the exit code.