Contents

Docker Commands Cheat Sheet: Essential Commands for Containers, Images, and More

This page collects the Docker commands you use most in day-to-day work, based on Docker 29. For detailed explanations and actual output for each command, follow the links in each section. The final section covers common pitfalls found while testing this tutorial.

Containers

CommandDescription
docker run -d --name web -p 8080:80 nginx:1.30Run a container in the background and map a port
docker run -it --rm ubuntu:26.04 bashRun interactively and remove the container when it exits
docker psList running containers
docker ps -aList all containers
docker stop webStop a container
docker start webStart a stopped container
docker restart webRestart a container
docker rm webRemove a stopped container
docker rm -f webForce-remove a running container
docker logs -f webFollow a container's logs
docker logs --tail 100 webShow the last 100 log lines
docker exec -it web shOpen a shell in a container
docker cp web:/path/file .Copy a file from a container to the host
docker inspect webView detailed container information
docker statsView resource usage in real time
docker top webShow processes inside a container
docker port webView port mappings
docker update --restart unless-stopped webChange the restart policy
docker run --init ...Use a small init process as PID 1 to forward stop signals correctly

Further reading: docker run options, Container lifecycle, View logs and debug

Images

CommandDescription
docker imagesList local images
docker images --treeShow the platforms included in multi-platform images
docker pull nginx:1.30Pull an image
docker tag myapp user/myapp:1.0Add a new name or tag
docker push user/myapp:1.0Push to a registry
docker rmi nginx:1.30Remove an image
docker history nginx:1.30View image layers
docker buildx imagetools inspect nginx:1.30-alpineView the platforms supported by a remote image
docker save -o img.tar myappExport an image to a file
docker load -i img.tarLoad an image from a file
docker login / docker logoutLog in to / out of a registry

Further reading: Images, Push to Docker Hub

Builds

CommandDescription
docker build -t myapp .Build with the Dockerfile in the current directory
docker build -f Dockerfile.prod -t myapp .Specify a Dockerfile
docker build --no-cache -t myapp .Build without using the cache
docker build --pull -t myapp .Pull the base image again
docker build --target dev -t myapp:dev .Build only up to a specific stage
docker build --build-arg VERSION=1.2 .Pass a build argument (do not use it to pass passwords)
docker build --secret id=token,env=TOKEN .Pass a build secret without leaving it in the image
docker build --progress plain .Show the full build output
docker buildx build --platform linux/amd64,linux/arm64 -t user/app --push .Build for multiple platforms and push

Further reading: Dockerfile instructions, Build cache and optimization, Multi-stage builds, Multi-platform images

Volumes

CommandDescription
docker volume create dataCreate a volume
docker volume lsList volumes
docker volume inspect dataView detailed information
docker volume rm dataRemove a volume
docker run -v data:/app/data myappMount a named volume
docker run -v "$(pwd):/app" myappMount the current directory

Further reading: Volumes and data persistence

Networks

CommandDescription
docker network create appnetCreate a custom network
docker network lsList networks
docker network inspect appnetView the containers on a network
docker run --network appnet --name db postgres:18Connect a container to a network
docker network connect appnet webConnect a running container to a network
docker network rm appnetRemove a network

Further reading: Networking and container communication

Docker Compose

CommandDescription
docker compose up -dStart all services in the background
docker compose up -d --buildRebuild and start
docker compose up -d --waitWait until all services are healthy before returning
docker compose up -d appUpdate or rebuild only one service
docker compose downStop and remove containers and networks
docker compose down -vAlso remove volumes
docker compose psShow service status
docker compose logs -f appFollow a service's logs
docker compose exec app shOpen a shell in a service container
docker compose run --rm app npm testRun a one-off command
docker compose pullPull the latest images
docker compose restart appRestart a service (does not apply changed configuration)
docker compose watchAutomatically sync or rebuild when files change (for development)
docker compose --profile tools up -dAlso start services in the profile
docker compose configCheck the final configuration

Further reading: Docker Compose introduction, docker compose commands

Cleanup

CommandDescription
docker system dfView disk usage
docker container pruneRemove stopped containers
docker image prune -aRemove unused images
docker builder pruneClear the build cache
docker builder prune --filter until=168hClear only build cache unused for more than 7 days
docker volume pruneRemove unused anonymous volumes
docker volume prune -aRemove all unused volumes, including named database volumes
docker system pruneGeneral cleanup (does not include volumes)
docker system prune --volumesGeneral cleanup plus anonymous volumes (does not include named volumes as of Docker 23)

Further reading: Free up disk space

System information

CommandDescription
docker versionClient and server versions
docker infoDocker system information
docker eventsView events in real time
docker context lsList Docker environments you can connect to

Common options at a glance

OptionMeaning
-dRun in the background
-itInteractive mode + terminal
-p host:containerPort mapping
-v source:container-pathMount
-e KEY=VALUEEnvironment variable
--nameContainer name
--rmRemove automatically when it exits
--networkSpecify a network
--restartRestart policy
-fForce (rm, rmi) or follow (logs)

Common pitfalls (found while testing this tutorial)

SymptomCause and fixChapter
docker stop takes 10 secondsThe program is PID 1 and does not handle SIGTERM. Handle it in the program or add --initNode.js application
Connecting to 127.0.53.53 when the container is named web.web is a top-level domain, so the name is resolved as an external domain. Choose another namedocker run options
postgres:18 exits as soon as it startsVersion 18 requires mounting /var/lib/postgresql, not /var/lib/postgresql/dataPostgreSQL
Database reports healthy before it is readyAdd -h 127.0.0.1 to pg_isready and mysqladmin pingMySQL, PostgreSQL
Health check passes even when the Redis password is wrongredis-cli ping still exits with code 0 on error; add | grep -q PONGRedis
Changing MYSQL_ROOT_PASSWORD has no effectThe password variable is used only during the first initializationMySQL
The container still has the old config file after you edit itWhen you mount a single file, saving in the editor replaces the file. Restart the container or mount the directory insteadNginx
Values from --env-file include quotesdocker run --env-file does not strip quotes; Compose's env_file doesEnvironment variables
A secret removed with rm is still in the imageEvery layer retains its contents; ARG and ENV are visible too. Use --secretSecurity best practices
exec format errorThe CPU architectures differ; use --platform or build for multiple platformsMulti-platform images

FAQ

What if I can't remember all the commands?

Start by remembering run, ps, logs, exec, stop, rm, build, and compose up/down. Look up the others here or run docker command --help when you need them.

Where can I find the full options for each command?

See the Command reference chapters for complete options and examples of common commands, such as [docker build](/docker/docker-build/) and [docker ps](/docker/docker-ps/).

Updated October 14, 2026