Docker cheatsheet

Docker CLI commands for images, containers, volumes, networks, logs, build and cleanup.

loading Docker cheatsheet
About this tool

What is a Docker cheatsheet?

Docker runs programs in containers: isolated processes started from an image that carries the program and everything it needs. The docker command line has dozens of subcommands and hundreds of flags, and most of the daily work uses a small part of them. This cheatsheet keeps that part one search away. It is organised by task, because you rarely start from a command name. You start from a situation: the container will not start, the disk is full, I need a shell inside it.

The commands are grouped in nine sections: run containers, manage containers, logs and inspection, images, build and registry, volumes, networks, cleanup and information. Each item has a plain description and one to three commands. Values you replace are written between angle brackets, such as <image>, <container>, <name>, <tag> and <registry>.

Some commands here delete things for good: docker rm -f, docker volume prune, docker system prune and docker image prune -a. The descriptions say what is removed, and the errors section below lists the traps. The page is plain reference material that runs in your browser, with no account and nothing sent anywhere. To run several containers together from one file, see the Docker Compose cheatsheet.

How to use the Docker cheatsheet

  1. Open the page and scroll. The sections follow a normal order: run, manage, inspect, build, store data, clean up.
  2. Press / to focus the search box, or click it. Type what you want to do or part of a command: logs follow, port or prune. Every word has to match. The search ignores accents and capital letters.
  3. Use the section chips under the search box to jump to a section. While you search, only sections with results remain in the chips.
  4. Press the round copy button next to an item to copy its commands, one per line, in the order you would run them.
  5. Press Copy section in a section title to take every command of the section.
  6. Replace the placeholders in angle brackets and paste into your terminal. Read the description first, especially in the cleanup section.
  7. Press Print for a clean sheet on paper or PDF, without the buttons.

A counter at the bottom shows how many items matched. If nothing matches, remove a word or try a simpler one such as run, logs, build or volume.

Examples

Start a web server and check it

Items "Run in the background (detached) with a name", "Publish a port" and "List the running containers":

docker run -d --name web -p 8080:80 nginx:1.27
docker ps

docker run -d prints the full ID of the new container, then docker ps lists it:

CONTAINER ID   IMAGE        COMMAND                  CREATED         STATUS         PORTS                  NAMES
3f2a9c1b7d4e   nginx:1.27   "/docker-entrypoint.…"   5 seconds ago   Up 4 seconds   0.0.0.0:8080->80/tcp   web

8080:80 is host port first, container port second. Open http://localhost:8080 to see the page. Not sure which service uses a port number? See the port numbers list.

Read the logs and open a shell

docker logs --tail 100 -t web
docker exec -it web sh

The first command prints the last 100 lines with timestamps. Add -f to follow them live. The second opens a shell inside the running container; type exit to leave, and the container keeps running. Use bash instead of sh only if the image has it.

Get one field with a format

Item "Get one field, here the IP address":

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
172.17.0.2

docker inspect prints a large JSON. The -f template picks one field. To read the full JSON in a tree, paste it in the JSON viewer.

See what cleanup would free

docker system df
docker container prune
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          12        3         4.2GB     2.9GB (69%)
Containers      5         1         12MB      10MB (83%)
Local Volumes   4         2         1.1GB     600MB (54%)
Build Cache     30        0         800MB     800MB

Sizes are an example. docker system df only reads, and docker container prune asks Are you sure you want to continue? [y/N] before it removes stopped containers.

Common errors and how to fix them

  • Cannot connect to the Docker daemon. Is the docker daemon running?: the client works but the daemon does not answer. Start Docker Desktop, or the service on Linux. docker version shows the client and the server separately, so you can see which one is missing.
  • permission denied while trying to connect to the Docker daemon socket: your user cannot use the socket. Use sudo, or add your user to the docker group and log in again. Know that this group is equivalent to root access on the machine.
  • Bind for 0.0.0.0:8080 failed: port is already allocated: another container or program uses that host port. Choose another host port, such as -p 8081:80, or stop the other one. docker port <container> shows what a container publishes.
  • Conflict. The container name is already in use: a stopped container still holds the name. Remove it with docker rm <container>, or pick another name.
  • docker rm -f $(docker ps -aq) removes every container: including databases that were running. Check docker ps -a first. Volumes survive, but only if the data is in a volume and not inside the container.
  • docker system prune and docker image prune -a delete more than you expect: unused images must be downloaded again, and --volumes also deletes the data of volumes that no container uses. Read the warning it prints, and run docker system df first.
  • -v "$(pwd)":/app does not work in PowerShell or cmd: those shells do not know $(pwd). In PowerShell write -v "${PWD}:/app".
  • Data disappeared after removing a container: files written inside a container are lost with it. Keep data in a volume with -v <volume>:/data.
  • No command matches your search: remove a word, or try run, logs, build or volume.

Frequently asked questions

What is the difference between docker run and docker exec?

docker run creates a new container from an image and starts it. docker exec runs a command in a container that is already running. Use exec to debug, and run to start something new.

What is the difference between an image and a container?

An image is a read-only template with the program and its files. A container is a running, or stopped, instance created from an image. You can start many containers from one image.

How do I stop and remove all containers?

docker rm -f $(docker ps -aq) does it in one line. It works in Bash and zsh, not in PowerShell. It deletes every container, so make sure that is what you want.

How do I free disk space used by Docker?

Start with docker system df to see where the space goes. Then docker container prune for stopped containers, docker image prune for untagged images and docker volume prune for unused volumes.

How do I run a container from a Compose file instead?

Describe the services in a compose.yaml and use docker compose up -d. The docker run to Compose converter turns a docker run command into that file.

Report a problem