Docker CLI Commands Cheat Sheet: The Commands You Use Every Day
How the Docker CLI is organized #
Modern Docker commands follow the pattern docker <object> <action>: docker container ls, docker image rm, docker volume create. Many have older short forms (docker ps, docker rm, docker images) that still work. Every command accepts --help.
If you use Rancher Desktop with containerd, replace docker with nerdctl: almost all the commands in this page are identical.
Run containers #
| Task | Command |
|---|---|
| Run in the foreground and remove at exit | docker run --rm alpine:3.21 echo hello |
| Run in the background with a name | docker run -d --name web nginx:1.27 |
| Publish a port (host:container) | docker run -d -p 8080:80 nginx:1.27 |
| Interactive shell | docker run -it --rm ubuntu:24.04 bash |
| Set an environment variable | docker run -e POSTGRES_PASSWORD=secret postgres:17 |
| Limit memory and CPU | docker run -m 256m --cpus 0.5 nginx:1.27 |
| Restart automatically | docker run -d --restart unless-stopped nginx:1.27 |
| Mount a host directory | docker run -v "$PWD":/work -w /work node:22 npm test |
Inspect what is running #
| Task | Command |
|---|---|
| Running containers | docker ps |
| All containers, also stopped | docker ps -a |
| Logs, last 100 lines, follow | docker logs -f --tail 100 web |
| Run a command in a running container | docker exec -it web sh |
| Details in JSON | docker inspect web |
| One field of the details | docker inspect -f '{{.State.Status}}' web |
| Live CPU and memory | docker stats |
| Processes in a container | docker top web |
| Copy a file out | docker cp web:/etc/nginx/nginx.conf . |
| Copy a file in | docker cp ./index.html web:/usr/share/nginx/html/ |
Start, stop and remove #
$ docker stop web # sends SIGTERM, then SIGKILL after 10 seconds
$ docker start web
$ docker restart web
$ docker rm web # remove a stopped container
$ docker rm -f web # stop and remove
$ docker rm -f $(docker ps -aq) # remove all containers
Images #
| Task | Command |
|---|---|
| Download an image | docker pull nginx:1.27 |
| List local images | docker image ls |
| Build from the current directory | docker build -t myapp:1.0 . |
| Build a specific Dockerfile and stage | docker build -f Dockerfile.prod --target runtime -t myapp:prod . |
| Add another name (tag) | docker tag myapp:1.0 registry.example.com/team/myapp:1.0 |
| Upload to a registry | docker push registry.example.com/team/myapp:1.0 |
| See the layers and their size | docker history myapp:1.0 |
| Remove an image | docker image rm myapp:1.0 |
| Save and load as a tar file | docker save myapp:1.0 -o myapp.tar and docker load -i myapp.tar |
| Log in to a registry | docker login registry.example.com |
Volumes and networks #
$ docker volume create db-data
$ docker volume ls
$ docker volume inspect db-data
$ docker volume rm db-data
$ docker network create app-net
$ docker network ls
$ docker network inspect app-net
$ docker network connect app-net web
Both topics have their own tutorial: volumes and bind mounts and networking.
Docker Compose #
$ docker compose up -d # create and start in the background
$ docker compose ps
$ docker compose logs -f app
$ docker compose exec app sh
$ docker compose down # stop and remove containers and networks
$ docker compose down -v # ...and the named volumes
See Docker Compose.
Clean up disk space #
$ docker system df # how much space images, containers and volumes use
$ docker container prune # remove stopped containers
$ docker image prune # remove dangling images
$ docker image prune -a # remove all images not used by a container
$ docker builder prune # remove the build cache
$ docker system prune --volumes # everything unused, including volumes
Contexts: talk to another engine #
A context tells the CLI which daemon to use. Rancher Desktop with dockerd creates its own context.
$ docker context ls
$ docker context use rancher-desktop
$ docker --context default ps # a single command against another context
Output formatting #
$ docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
$ docker image ls --format '{{.Repository}}:{{.Tag}} {{.Size}}'
Next steps #
Put the commands to use by writing a Dockerfile.