· Satrajit Sengupta · Containers · 5 min read
Docker commands reference — everything you need for day-to-day operations
A practical reference for the Docker commands that cover 95% of real-world container operations, with context on when and why to use each one.
Once Docker is installed and you’ve built your first image, you’ll find yourself using the same ~20 commands for almost everything. This is a practical reference — commands are grouped by what they do, with context on behaviour that trips people up.
Container lifecycle
Check the Docker version
docker -v
docker version # more detail: client and server versions separatelyList containers
docker ps # running containers only
docker ps -a # all containers, including stopped ones
docker ps -l # the most recently created container
docker ps -n 3 # the last 3 created containersCreate a container (without starting it)
docker create --name my-container nginx:alpineCreates the container in Created status. Start it separately with docker start.
Run a container
docker run nginx:alpine # foreground, random name
docker run --name web nginx:alpine # named container
docker run -itd --name web nginx:alpine # detached, interactive, tty
docker run -d -p 8080:80 --name web nginx:alpine # detached, port publishedThe -itd flags: -i keeps stdin open, -t allocates a pseudo-TTY, -d detaches. You’ll use -itd for containers you want running in the background that you also want to be able to shell into.
Start and stop
docker start my-container # start a stopped container
docker stop my-container # graceful stop (SIGTERM, then SIGKILL after timeout)
docker kill my-container # immediate stop (SIGKILL)
docker restart my-container # stop + startInteracting with running containers
Execute a command inside a container
docker exec -it my-container bash # open a bash shell
docker exec -it my-container sh # open sh (for minimal images without bash)
docker exec my-container ls /app # run a single command and returndocker exec creates a new process in the container. When you exit, that process ends — but the container’s main process (PID 1) keeps running, so the container stays up.
Attach to a container’s main process
docker attach my-containerUnlike exec, attach connects your terminal to the container’s PID 1 — the main process. When you exit, that process receives the signal and the container stops. Be careful with this in production.
Copy files between host and container
# Host → container
docker cp /host/path/file.txt my-container:/container/path/
# Container → host
docker cp my-container:/container/path/file.txt /host/path/Useful for debugging — copying log files out of a container, or injecting a config change without rebuilding the image.
View container output
docker logs my-container # all output since container started
docker logs -f my-container # follow (live tail)
docker logs --tail 50 my-container # last 50 lines onlyRemoving containers
docker rm my-container # remove a stopped container
docker rm -f my-container # stop and remove (force)
docker container prune # remove all stopped containersYou cannot docker rm a running container without -f. If you get an error, check docker ps first.
To remove a running container, stop it first or use -f:
docker stop my-container && docker rm my-container
# or
docker rm -f my-containerImages
List and inspect images
docker images # list all local images
docker image ls # same thing
docker image inspect nginx:alpine # full metadata as JSON
docker history nginx:alpine # each layer, size, and instructionPull and push
docker pull nginx:alpine # pull from Docker Hub
docker pull registry.example.com/my-app:v1 # pull from private registry
docker tag my-app:latest myuser/my-app:v1 # tag for pushing
docker push myuser/my-app:v1 # push to Docker HubRemove images
docker rmi my-app:latest # remove by name:tag
docker rmi <image-id> # remove by ID
docker image prune # remove all dangling (untagged) images
docker image prune -a # remove all unused imagesYou cannot remove an image that is used by any container — running or stopped. Remove the container first:
docker rm my-container && docker rmi my-imageNetworking (quick reference)
docker network ls # list all networks
docker network inspect bridge # details of the bridge network
docker network create my-net # create a user-defined bridge
docker network connect my-net my-container # attach running container to network
docker network disconnect my-net my-container
docker network rm my-net
docker network prune # remove all unused networksVolumes (quick reference)
docker volume ls
docker volume create my-vol
docker volume inspect my-vol
docker volume rm my-vol
docker volume prune # remove all unused volumesSystem maintenance
docker system df # disk usage by images, containers, volumes
docker system prune # remove all stopped containers, unused networks, dangling images
docker system prune -a # also removes unused images (not just dangling)
docker system prune --volumes # include volumes in cleanupdocker system prune -a --volumes is the nuclear option — it reclaims all Docker disk space on the host. Don’t run it on a production machine without knowing what’s running.
Quick-reference table
| Command | What it does |
|---|---|
docker ps / docker ps -a | List running / all containers |
docker run | Create and start a container |
docker start / docker stop | Start / stop a container |
docker exec -it | Shell into a running container |
docker attach | Connect to container’s PID 1 |
docker logs -f | Follow container output |
docker cp | Copy files host ↔ container |
docker rm / docker rm -f | Remove stopped / force-remove container |
docker container prune | Remove all stopped containers |
docker images | List local images |
docker pull / docker push | Pull from / push to registry |
docker rmi | Remove an image |
docker image prune -a | Remove all unused images |
docker network ls / create / rm | Manage networks |
docker volume ls / create / rm | Manage volumes |
docker system df | Show disk usage |
docker system prune -a | Clean up everything unused |
Part of the Docker series on The Digital Drift. Related: Docker volumes · Docker networking