Docker Daily Command Reference
This page leans toward a quick reference rather than conceptual completeness. It mainly groups the commands I look up most often. It uses the newer object-style syntax by default, such as docker image ls and docker container ls, but also keeps common aliases in mind so you don't have to mentally translate when looking up commands.
Remember the Minimal Model First
Image image -> docker run -> Container container
- An image is like a template
- A container is like a running instance
- Deleting a container usually does not delete the image
- Data persistence relies on volumes or bind mounts, not the container's writable layer
Image-Related
| Purpose | Command |
|---|---|
| List local images | docker image ls |
| Pull an image | docker pull nginx:stable |
| View image details | docker image inspect nginx:stable |
| Tag an image | docker tag nginx:stable my-nginx:demo |
| View image build history | docker history nginx:stable |
| Remove an image | docker image rm my-nginx:demo |
Common aliases:
docker imagesis equivalent todocker image ls
Container-Related
| Purpose | Command |
|---|---|
| List running containers | docker container ls |
| List all containers | docker container ls -a |
| Create and run a container | docker run -d --name web -p 8080:80 nginx:stable |
| Start an existing container | docker start web |
| Stop a container | docker stop web |
| Force kill a container | docker kill web |
| Restart a container | docker restart web |
| Remove a container | docker rm web |
| Force remove a running container | docker rm -f web |
| Rename a container | docker rename old-name new-name |
Common aliases:
docker psis equivalent todocker container lsdocker ps -ais equivalent todocker container ls -a
Entering Containers and Copying Files
Enter a Running Container
docker exec -it web bash
If the image does not have bash, use sh:
docker exec -it web sh
Enter a Container as root
docker exec -u root -it web bash
Copy Files Between Host and Container
docker cp ./local.txt web:/tmp/local.txt
docker cp web:/var/log/nginx/access.log ./access.log
Logs, Status, and Inspection
| Purpose | Command |
|---|---|
| View container logs | docker logs web |
| Follow logs | docker logs -f --tail 100 web |
| View container details | docker inspect web |
| View exit code only | docker inspect --format '{{.State.ExitCode}}' web |
| View container processes | docker top web |
| View resource usage | docker stats |
| View port mappings | docker port web |
If you don't know why a container exited, check in this order:
docker ps -adocker logs <container>docker inspect <container>
Network and Storage
docker network ls
docker volume ls
docker volume inspect mydata
If you frequently find yourself wondering "where did my data go" or "why is my mount not working," go directly to:
Cleanup Commands
First check disk usage:
docker system df
Then clean up by object type:
docker container prune
docker image prune
docker volume prune
docker network prune
If you are very sure you want to do a full cleanup:
docker system prune -a --volumes
This command deletes unused containers, networks, images, and volumes. It is convenient but can easily remove things that are "not currently running but you still want to keep."
My Most Commonly Used Command Set
docker ps
docker logs -f --tail 100 my-service
docker exec -it my-service bash
docker inspect my-service
docker system df