Docker Commands Cheat Sheet
Everyday Docker work comes down to roughly twenty commands: docker run starts containers, docker ps lists them, docker logs and docker exec help you debug them, and a few more look after images, volumes and disk space. This Docker commands cheat sheet sorts them by task, with the flags worth knowing and examples ready to paste. They behave identically on any Linux server with Docker Engine.
Haven't installed Docker yet? Our guide to installing Docker on Ubuntu gets you there first.
Syntax note. Most Docker commands exist in two forms, a short one (docker ps, docker rmi) and a grouped one (docker container ls, docker image rm), and both do exactly the same thing. This sheet sticks to the short forms, since those are what most documentation and scripts use.
Docker commands to run containers
| Command | What it does |
|---|---|
docker run nginx |
Start a container from an image, in the foreground |
docker run -d nginx |
Start it in the background (detached) |
docker run -d --name web nginx |
Give it a name instead of a random one |
docker run -d -p 8080:80 nginx |
Publish container port 80 on host port 8080 |
docker run -d -p 127.0.0.1:8080:80 nginx |
Publish only on localhost |
docker run -e KEY=value nginx |
Set an environment variable |
docker run --env-file .env nginx |
Load variables from a file |
docker run -v data:/var/lib/app nginx |
Mount a named volume |
docker run -v /srv/site:/usr/share/nginx/html:ro nginx |
Bind-mount a host folder, read-only |
docker run --rm -it alpine sh |
Interactive shell, container deleted on exit |
docker run -d --restart unless-stopped nginx |
Restart automatically unless you stop it |
docker run --memory 512m --cpus 1.5 nginx |
Limit memory and CPU |
A typical long-running service on a server puts several of these together:
docker run -d --name web \
--restart unless-stopped \
-p 127.0.0.1:8080:80 \
-v /srv/site:/usr/share/nginx/html:ro \
nginx:1.27
Binding to 127.0.0.1 matters on a server. A port published with a plain -p 8080:80 is opened in iptables by Docker itself, and that rule bypasses ufw.
List and inspect containers
| Command | What it does |
|---|---|
docker ps |
Running containers |
docker ps -a |
All containers, including stopped |
docker ps -q |
Only container IDs (handy in scripts) |
docker ps --filter status=exited |
Only stopped containers |
docker inspect web |
Full configuration as JSON |
docker inspect -f '{{.NetworkSettings.IPAddress}}' web |
One field from the JSON |
docker port web |
Which ports are published |
docker stats |
Live CPU, memory and network use per container |
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" |
Choose which columns docker ps shows |
docker top web |
Processes inside a container |
Logs and debugging
| Command | What it does |
|---|---|
docker logs web |
All output from the container |
docker logs -f web |
Follow new output, like tail -f |
docker logs --tail 100 web |
Last 100 lines |
docker logs --since 30m web |
Output from the last 30 minutes |
docker exec -it web sh |
Open a shell inside a running container |
docker exec -it web bash |
Same, if the image has bash |
docker exec web ls /etc/nginx |
Run one command inside the container |
docker exec -u root -it web sh |
Shell as root, if the image runs as another user |
docker cp web:/etc/nginx/nginx.conf . |
Copy a file out of a container |
docker cp ./nginx.conf web:/etc/nginx/ |
Copy a file into a container |
Small images like alpine ship sh but not bash. When docker exec -it web bash fails with "executable file not found", switch to sh.
Start, stop and restart
| Command | What it does |
|---|---|
docker stop web |
Graceful stop: SIGTERM, then SIGKILL after 10 seconds |
docker stop -t 60 web |
Allow 60 seconds before the kill |
docker start web |
Start a stopped container again |
docker restart web |
Stop and start |
docker kill web |
Stop immediately with SIGKILL |
docker pause web / docker unpause web |
Freeze and resume all processes |
docker update --restart unless-stopped web |
Change the restart policy of an existing container |
docker rename web web-old |
Rename a container |
Restart policies and restarting the Docker service itself are covered in how to restart Docker.
Remove containers
| Command | What it does |
|---|---|
docker rm web |
Remove a stopped container |
docker rm -f web |
Stop and remove a running container |
docker rm -v web |
Also remove its anonymous volumes |
docker container prune |
Remove all stopped containers |
docker rm -f $(docker ps -aq) |
Remove every container |
For safe patterns and the details, see how to stop and remove Docker containers.
Images
| Command | What it does |
|---|---|
docker images |
List local images |
docker search nginx |
Search Docker Hub for images |
docker pull nginx:1.27 |
Download an image |
docker build -t myapp:1.0 . |
Build an image from the Dockerfile in this folder |
docker build --no-cache -t myapp:1.0 . |
Build without using cached layers |
docker buildx build --platform linux/amd64,linux/arm64 -t myapp:1.0 . |
Build one image for several CPU architectures |
docker scout cves myapp:1.0 |
List known vulnerabilities in an image (needs the Docker Scout plugin) |
docker tag myapp:1.0 registry.example.com/myapp:1.0 |
Add another name to an image |
docker push registry.example.com/myapp:1.0 |
Upload to a registry |
docker login registry.example.com |
Log in to a registry |
docker history myapp:1.0 |
Show the layers of an image |
docker save -o myapp.tar myapp:1.0 |
Export an image to a tar file |
docker load -i myapp.tar |
Import an image from a tar file |
docker rmi myapp:1.0 |
Remove an image |
docker image prune -a |
Remove all images no container uses |
docker save and docker load help when a server can't reach a registry: export the image to a tar file, copy it over with scp or rsync, and load it there. Deleting images safely is covered in Docker remove image.
Volumes
| Command | What it does |
|---|---|
docker volume ls |
List volumes |
docker volume create pgdata |
Create a named volume |
docker volume inspect pgdata |
Show where the volume lives on the host |
docker volume rm pgdata |
Remove a volume (its data is deleted) |
docker volume prune |
Remove unused volumes |
A named volume outlives docker rm, which makes it the right home for databases and uploads. To back one up, mount it into a disposable container and archive its contents:
docker run --rm -v pgdata:/data -v "$PWD":/backup alpine \
tar czf /backup/pgdata.tar.gz -C /data .
The archive flags are explained in our guide to tar and gzip in Linux.
Networks
| Command | What it does |
|---|---|
docker network ls |
List networks |
docker network create appnet |
Create a user-defined bridge network |
docker run -d --network appnet --name api myapi |
Attach a container to it |
docker network connect appnet web |
Attach a running container |
docker network inspect appnet |
Show connected containers and IPs |
docker network rm appnet |
Remove a network |
On a user-defined network, containers find each other by name, so web can reach http://api:3000 without knowing any IP address. The default bridge network has no such name lookup.
Docker Compose
| Command | What it does |
|---|---|
docker compose up -d |
Create and start every service in the background |
docker compose up -d --build |
Rebuild images first |
docker compose ps |
Status of the project's containers |
docker compose logs -f api |
Follow one service's logs |
docker compose exec api sh |
Shell in a service container |
docker compose restart api |
Restart one service |
docker compose pull |
Pull newer images for all services |
docker compose down |
Stop and remove the project's containers and networks |
docker compose down -v |
Also remove its volumes (data is deleted) |
With Compose v2 the command is docker compose, space included. The full walkthrough is in Docker Compose up and down.
Disk space and clean-up
| Command | What it does |
|---|---|
docker system df |
Disk used by images, containers, volumes and build cache |
docker system df -v |
The same, per object |
docker system prune |
Remove stopped containers, unused networks, dangling images and build cache |
docker system prune -a |
Also remove every unused image |
docker builder prune |
Remove only the build cache |
docker system prune won't touch volumes unless --volumes is present. For the rest of the server's storage, see how to check disk usage in Linux.
System and daemon
These run on any Docker host, whether that's a small OrbitServers VPS or a dedicated server.
| Command | What it does |
|---|---|
docker version |
Client and server versions |
docker info |
Storage driver, number of containers, root directory |
docker events |
Live stream of what the daemon is doing |
sudo systemctl status docker |
Is the daemon running? |
sudo journalctl -u docker |
Daemon logs |
docker context ls |
Saved connections to other Docker hosts |
Frequently asked questions
What are the most important Docker commands for beginners?
docker run, docker ps, docker logs, docker exec, docker stop, docker rm and docker images. Those seven are enough to start, examine, debug and tidy up containers.
What is the difference between docker run and docker start?
docker run builds a brand-new container from an image and starts it. docker start restarts an existing, stopped container with the configuration it was created with.
What is the difference between docker exec and docker attach?
docker exec launches an extra process, normally a shell, inside a container that is already running. docker attach hooks your terminal up to the container's main process, so a Ctrl+C there can bring the container down. For troubleshooting, reach for exec.
How do I see all Docker commands?
docker --help prints the complete list, and docker <command> --help shows every flag of one command, for example docker run --help.
Do I need sudo for Docker commands?
Only while your user is outside the docker group. Adding it with sudo usermod -aG docker $USER does away with sudo, but membership amounts to root access on the host, so add trusted users only.
How do I free up Docker disk space?
docker system df shows what is taking up space. docker system prune then clears stopped containers, unused networks, dangling images and build cache, and adding -a takes unused images too. Volumes go only when you ask for them, so look at docker volume ls before pruning those.
Was this article helpful?
Written by Julius · Product Manager