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
docker imagesList all local images
docker pull nginx:latestDownload image from registry
docker build -t myapp:1.0 .Build image from Dockerfile in current directory
docker build -f Dockerfile.prod -t myapp:prod .Build with specific Dockerfile
docker build --no-cache -t myapp .Build without using cache
docker rmi nginxRemove 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 tag myapp:1.0 registry.io/myapp:1.0Tag an image for a registry
docker image inspect nginxShow image details as JSON
docker history nginxShow image layer history
DOCKER // REFERENCE
Container Lifecycle
docker run nginxRun container (foreground, auto-pulled)
docker run -d nginxRun container in detached (background) mode
docker run -p 8080:80 nginxMap host port 8080 to container port 80
docker run -e NODE_ENV=production myappSet environment variable
docker run --name mycontainer nginxAssign a name to the container
docker run --rm nginxAuto-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 run -it ubuntu bashStart interactive terminal session
docker run -v /host/path:/container/path nginxBind-mount a host directory
docker psList running containers
docker ps -aList all containers (including stopped)
docker stop container_idGracefully stop container (SIGTERM)
docker kill container_idForce stop container (SIGKILL)
docker start container_idStart a stopped container
docker restart container_idRestart a container
docker rm container_idRemove 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_idForce 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
docker exec -it container bashOpen interactive shell in running container
docker exec container ls /appRun one-off command in container
docker logs containerPrint container log output
docker logs -f containerFollow log output in real-time
docker logs --tail 100 containerShow last 100 log lines
docker inspect containerShow full container config as JSON
docker statsLive CPU, memory, and network usage
docker top containerShow processes running inside container
docker cp file.txt container:/app/Copy file from host to container
docker cp container:/app/log.txt .Copy file from container to host
DOCKER // REFERENCE
Docker Compose
docker compose upCreate and start all services
docker compose up -dStart in detached mode
docker compose up --buildRebuild images before starting
docker compose downStop and remove containers and networks
docker compose down -vAlso 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 compose psList service containers
docker compose logs -fFollow all service logs
docker compose logs -f service_nameFollow logs for one service
docker compose exec service bashOpen shell in a running service
docker compose buildBuild or rebuild service images
docker compose pullPull latest images for all services
docker compose restart serviceRestart a specific service
DOCKER // REFERENCE
Volumes
docker volume create myvolumeCreate a named volume
docker volume lsList all volumes
docker volume inspect myvolumeShow volume details
docker volume rm myvolumeRemove 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 pruneRemove 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 run -v myvolume:/app nginxMount named volume at /app
docker run -v $(pwd):/app nginxBind-mount current directory
DOCKER // REFERENCE
Networking
docker network lsList all networks
docker network create mynetworkCreate a user-defined bridge network
docker network create --driver overlay mynetCreate overlay network (Swarm)
docker network inspect mynetworkShow network details
docker network rm mynetworkRemove 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 run --network mynetwork nginxConnect container to a network
docker network connect mynetwork containerConnect running container to network
docker network disconnect mynetwork containerDisconnect container from network
DOCKER // REFERENCE
Registry
docker loginLog in to Docker Hub
docker login registry.example.comLog in to private registry
docker logoutLog out from registry
docker push myuser/myapp:1.0Push image to registry
docker pull myuser/myapp:1.0Pull image from registry
docker save myapp -o myapp.tarExport image to tar archive
docker load -i myapp.tarImport image from tar archive
DOCKER // REFERENCE
System & Cleanup
docker system dfShow disk usage by images, containers, volumes
docker system pruneRemove 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 -aAlso 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 --volumesAlso 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 pruneRemove 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 pruneRemove 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 -aRemove 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.
docker infoDisplay system-wide Docker info
docker versionShow Docker client and server versions
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.