Sachin Chaurasiya

Toolbox · Containers

Docker Commands

Build, run, inspect and clean up containers and images, with the flags that matter for security.

Last reviewed
2026-09-18
On this page

Build

CommandPurpose
docker build --pull -t app:dev .Build, refreshing the base image
docker build --no-cache -t app:dev .Ignore layer cache
docker build --target build -t app:build .Build a specific multi-stage target
docker buildx build --platform linux/amd64,linux/arm64 -t reg/app:1.0 --push .Multi-arch build and push
docker buildx build --sbom=true --provenance=true Attach SBOM and provenance attestations
RUN --mount=type=secret,id=npmrc Use a build secret without baking it into a layer

Run

CommandPurpose
docker run --rm -it app:dev shInteractive shell, remove on exit
docker run --rm --user 10001 --read-only --cap-drop ALL app:devHardened runtime flags
docker run --rm -p 8080:8080 --memory 512m --cpus 1 app:devPort and resource limits
docker run --rm --network none app:devNo network
docker exec -it <ctr> shShell into a running container

Inspect

CommandPurpose
docker ps -aAll containers incl. exited
docker logs -f --tail 100 <ctr>Follow logs
docker inspect <ctr> | jq '.[0].State'State, exit code, OOM
docker history --no-trunc app:devLayer commands (spot leaked args)
docker image inspect app:dev --format '{{.Config.User}}'Verify non-root user
docker stats --no-streamCPU/memory snapshot
docker diff <ctr>Files changed in the container FS

Clean up

CommandPurpose
docker system dfDisk usage by type
docker image prune -a --filter "until=168h"Caution remove unused images older than 7 days
docker container pruneCaution remove stopped containers and their filesystems
docker builder pruneClear build cache
docker system prune -a --volumesDestructive stopped containers, unused images and networks, build cache, anonymous volumes