A compact reference to the Docker commands that cover 95% of day-to-day work on a server. Every command below works with Docker Engine 24 and newer. Need Docker first? See installing Docker on Ubuntu.
Run containers
docker run -d --name web -p 8080:80 nginx:stable
| Flag | Meaning |
|---|---|
-d |
Detached (background) |
--name web |
Give it a name instead of a random one |
-p 8080:80 |
Publish container port 80 on host port 8080 |
-p 127.0.0.1:8080:80 |
Publish on localhost only |
-e KEY=value |
Set an environment variable |
--env-file .env |
Load variables from a file |
-v /host/dir:/container/dir |
Bind mount a host directory |
-v data:/var/lib/data |
Mount a named volume |
--restart unless-stopped |
Restart after crashes and reboots |
--rm |
Delete the container when it exits |
-it |
Interactive terminal (for shells) |
--memory 512m --cpus 1 |
Resource limits |
Run a throwaway shell in an image:
docker run --rm -it debian:stable bash
List and inspect
docker ps # running containers
docker ps -a # include stopped ones
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
docker inspect web # full JSON details
docker inspect -f '{{.State.Health.Status}}' web
docker stats # live CPU / memory / network per container
docker top web # processes inside a container
docker port web # published ports
Logs
docker logs web # everything so far
docker logs -f web # follow
docker logs --tail 100 web # last 100 lines
docker logs --since 30m web # last 30 minutes
More on this in Docker logs.
Get inside a container
docker exec -it web bash # or sh on Alpine-based images
docker exec web nginx -t # run a single command
docker exec -u root -it web sh # as root
docker cp web:/etc/nginx/nginx.conf ./nginx.conf # copy files out
docker cp ./index.html web:/usr/share/nginx/html/ # and in
Start, stop, remove
docker stop web # graceful stop (SIGTERM, then SIGKILL after 10 s)
docker start web
docker restart web
docker kill web # immediate SIGKILL
docker rm web # remove a stopped container
docker rm -f web # stop and remove
docker update --restart unless-stopped web # change restart policy in place
Images
docker images # list local images
docker pull postgres:17 # download
docker build -t myapp:1.0 . # build from ./Dockerfile
docker tag myapp:1.0 registry.example.com/myapp:1.0
docker push registry.example.com/myapp:1.0
docker rmi myapp:1.0 # remove an image
docker history myapp:1.0 # layers and their sizes
Volumes
docker volume ls
docker volume create data
docker volume inspect data # shows the mountpoint on the host
docker volume rm data
Back up a named volume to a tarball:
docker run --rm -v data:/data -v "$PWD":/backup debian:stable \
tar czf /backup/data-$(date +%F).tar.gz -C /data .
Networks
docker network ls
docker network create appnet
docker run -d --name db --network appnet postgres:17
docker run -d --name app --network appnet myapp:1.0 # reaches the DB at "db"
docker network inspect appnet
docker network connect appnet web # attach a running container
Containers on the same user-defined network resolve each other by name. Details in Docker networking.
Clean up disk space
docker system df # what's using space
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 volume prune # remove unused volumes (careful!)
docker builder prune # build cache
docker system prune # containers + networks + dangling images + build cache
volume prune deletes data that no container currently references — including the database volume of a stack you’ve temporarily brought down. See freeing disk space with docker prune.
Docker Compose
docker compose up -d # start the stack
docker compose ps
docker compose logs -f app
docker compose exec app sh
docker compose pull && docker compose up -d # update images
docker compose down # stop and remove containers (keeps volumes)
docker compose config # print the resolved config
Full walkthrough: Docker Compose tutorial.
System and troubleshooting
docker info # engine settings, storage driver, root dir
docker version # client and server versions
docker events # live stream of container events
sudo journalctl -u docker -n 50 # daemon logs
Handy one-liners
# Stop all running containers
docker stop $(docker ps -q)
# Remove all stopped containers
docker rm $(docker ps -aq -f status=exited)
# Show IP addresses of all containers
docker ps -q | xargs docker inspect -f '{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
# Show container sizes
docker ps -s
Bookmark this page, and when you’re ready to package your own app, read the Dockerfile tutorial. To run containers around the clock, a Docker VPS gives you full root access on KVM.