Skip to main content

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
PurposeCommand
List local imagesdocker image ls
Pull an imagedocker pull nginx:stable
View image detailsdocker image inspect nginx:stable
Tag an imagedocker tag nginx:stable my-nginx:demo
View image build historydocker history nginx:stable
Remove an imagedocker image rm my-nginx:demo

Common aliases:

  • docker images is equivalent to docker image ls
PurposeCommand
List running containersdocker container ls
List all containersdocker container ls -a
Create and run a containerdocker run -d --name web -p 8080:80 nginx:stable
Start an existing containerdocker start web
Stop a containerdocker stop web
Force kill a containerdocker kill web
Restart a containerdocker restart web
Remove a containerdocker rm web
Force remove a running containerdocker rm -f web
Rename a containerdocker rename old-name new-name

Common aliases:

  • docker ps is equivalent to docker container ls
  • docker ps -a is equivalent to docker 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

PurposeCommand
View container logsdocker logs web
Follow logsdocker logs -f --tail 100 web
View container detailsdocker inspect web
View exit code onlydocker inspect --format '{{.State.ExitCode}}' web
View container processesdocker top web
View resource usagedocker stats
View port mappingsdocker port web

If you don't know why a container exited, check in this order:

  1. docker ps -a
  2. docker logs <container>
  3. 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