Complete Docker CLI reference — image builds, container lifecycle, volumes, networking, Compose, and cleanup commands for daily container work.
Build, tag, and manage the artifacts your containers run from.
docker build -t razorpay/orders-api:v2.3.1 .
docker images ## list local images
docker tag razorpay/orders-api:v2.3.1 razorpay/orders-api:latest
docker rmi razorpay/orders-api:v2.3.0 ## remove an image
docker history razorpay/orders-api:v2.3.1 ## inspect layer-by-layer size
docker pull postgres:16
docker push razorpay/orders-api:v2.3.1
Parameter Breakdown:
-t: Tags the image during build — always use name:version, never rely on latest in productionhistory: Shows the size each layer added — first stop when an image is too largetag: Creates a second reference to the same image, does not duplicate storageRun, stop, and remove containers.
docker run -d --name orders-api \
-p 8080:8080 \
-e DB_HOST=db.internal.razorpay.net \
razorpay/orders-api:v2.3.1
docker ps ## running containers
docker ps -a ## all containers, including stopped
docker stop orders-api
docker start orders-api
docker restart orders-api
docker rm orders-api ## remove a stopped container
docker rm -f orders-api ## force remove a running container
Command Parameter Table:
| Flag | Description |
|---|---|
-d |
Run detached, in the background |
-p |
Map host port to container port, host:container |
-e |
Inject an environment variable into the container |
Debug a running or recently exited container.
docker logs orders-api ## current logs
docker logs -f orders-api ## stream live
docker logs --tail 100 orders-api ## last 100 lines only
docker inspect orders-api ## full JSON config and state
docker exec -it orders-api bash ## interactive shell
docker top orders-api ## processes running inside
docker stats orders-api ## live CPU/memory usage
Parameter Breakdown:
exec -it: Opens an interactive shell inside an already-running containerinspect: Returns full JSON — pipe through --format or jq to extract one fieldstats: Live resource usage, similar to top but container-scopedPersist data outside the container's writable layer.
docker volume create orders-db-data
docker volume ls
docker volume inspect orders-db-data
docker run -d --name orders-db \
-v orders-db-data:/var/lib/postgresql/data \
postgres:16
docker run -d --name orders-api \
-v $(pwd)/config:/app/config:ro \
razorpay/orders-api:v2.3.1
docker volume rm orders-db-data
Parameter Breakdown:
-v name:path: Named volume — Docker manages the storage location-v $(pwd)/dir:path: Bind mount — maps a host directory directly:ro: Mounts the volume read-only inside the containerConnect containers to each other and the outside world.
docker network ls
docker network create payments-net
docker network inspect payments-net
docker run -d --name orders-api \
--network payments-net \
razorpay/orders-api:v2.3.1
docker network connect payments-net orders-db
docker network disconnect payments-net orders-db
Parameter Breakdown:
--network: Containers on the same custom network resolve each other by container namebridge network does not support name-based DNS — always create a custom networknetwork inspect: Shows every container currently attached, with their IPsRun multi-container applications from one file.
docker compose up -d ## start all services, detached
docker compose down ## stop and remove containers + network
docker compose down -v ## also remove named volumes
docker compose ps ## status of services in this project
docker compose logs -f orders-api ## stream logs for one service
docker compose exec orders-api bash ## shell into a running service
docker compose build --no-cache ## force a clean rebuild
docker compose restart orders-api
Parameter Breakdown:
up -d: Starts every service defined in docker-compose.yml in the backgrounddown -v: Use with caution — this deletes named volumes and their data--no-cache: Ignores Docker's build cache, useful when a base image silently changedKeep images small and builds fast.
## Multi-stage build pattern (Dockerfile excerpt)
## FROM golang:1.22 AS builder
## WORKDIR /app
## COPY . .
## RUN go build -o orders-api
##
## FROM alpine:3.19
## COPY --from=builder /app/orders-api /usr/local/bin/
## ENTRYPOINT ["orders-api"]
docker build --no-cache -t orders-api:debug .
docker build --build-arg ENV=staging -t orders-api:staging .
docker buildx build --platform linux/amd64,linux/arm64 \
-t razorpay/orders-api:v2.3.1 --push .
Parameter Breakdown:
--build-arg: Passes a build-time variable into the Dockerfilebuildx --platform: Builds and pushes multi-architecture images in one commandReclaim disk space on build servers and laptops.
docker system df ## see space used by images/containers/volumes
docker container prune ## remove all stopped containers
docker image prune ## remove dangling (untagged) images
docker image prune -a ## remove all unused images
docker volume prune ## remove unused volumes
docker system prune -a --volumes ## remove everything unused, including volumes
Command Parameter Table:
| Flag | Description |
|---|---|
image prune |
Removes only dangling images by default |
image prune -a |
Removes every image not used by a running container |
system prune -a --volumes |
Most aggressive cleanup — confirm before running on shared hosts |
Common failures and the first command to run for each.
## "port is already allocated"
docker ps --filter "publish=8080" ## find what's using the port
## "no space left on device"
docker system df ## check usage
docker system prune -a ## reclaim space
## Container exits immediately
docker logs orders-api ## check the actual error
docker inspect orders-api \
--format='{{.State.ExitCode}}' ## get the exact exit code
## Permission denied inside container
docker exec -it orders-api whoami ## confirm running user
Notes:
docker logsShortcuts worth adding to your dotfiles.
alias dps='docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'
alias dlog='docker logs -f'
alias dex='docker exec -it'
## Stop and remove every running container
docker stop $(docker ps -q) && docker rm $(docker ps -aq)
## Remove all images not associated with a container
docker image prune -a -f
## Follow logs of every Compose service at once
docker compose logs -f