🛠️ ITBlogs.pro

Docker Cheat Sheet: Essential Commands and Troubleshooting

Oct 16, 2026 · Docker · 3 min read

The commands you'll use every day, grouped by task. Everything here works with current Docker Engine.

Run containers

docker run -d --name web -p 8080:80 nginx          # detached, publish host:container port
docker run -it --rm ubuntu:24.04 bash              # interactive shell, deleted on exit
docker run -d --restart unless-stopped --name db \
  -e MARIADB_ROOT_PASSWORD=secret -v dbdata:/var/lib/mysql mariadb:11
Flag Meaning
-d run in the background
-it interactive + terminal
--rm remove the container when it exits
--name give it a readable name
-p 8080:80 host port 8080 → container port 80
-v name:/path mount a volume (data survives the container)
-e KEY=value set an environment variable
--restart unless-stopped restart after crashes and reboots

Inspect and manage

docker ps                      # running containers
docker ps -a                   # including stopped ones
docker logs -f --tail 100 web  # follow the last 100 lines
docker exec -it web sh         # shell inside a running container
docker stop web && docker start web
docker restart web
docker rm web                  # remove a stopped container
docker rm -f web               # force remove a running one
docker inspect web             # full JSON details
docker stats                   # live CPU/RAM per container
docker top web                 # processes inside

Copy files in and out:

docker cp web:/etc/nginx/nginx.conf ./nginx.conf
docker cp ./index.html web:/usr/share/nginx/html/

Images

docker pull nginx:1.27
docker images
docker rmi nginx:1.27
docker build -t myapp:1.0 .
docker tag myapp:1.0 registry.example.com/myapp:1.0
docker push registry.example.com/myapp:1.0

A minimal Dockerfile:

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Copy requirements.txt first so the dependency layer stays cached when only your code changes.

Volumes and networks

docker volume ls
docker volume inspect dbdata
docker network ls
docker network create mynet
docker run -d --network mynet --name api myapp   # containers on the same network reach each other by name

Clean up (and reclaim disk)

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 unused images
docker system prune              # containers + networks + dangling images + cache

docker system prune --volumes also deletes unused volumes, and that includes database data you might still need. Check docker volume ls first.

When a container won't start or keeps dying

docker ps -a                       # status and exit code
docker logs --tail 50 CONTAINER    # almost always says why
docker inspect -f '{{.State.ExitCode}} OOM={{.State.OOMKilled}}' CONTAINER
Exit code Usually means
0 The main process finished normally, so the container had nothing to keep running
1 Application error, read the logs
126 / 127 Command not executable / not found (check CMD/ENTRYPOINT)
137 Killed, often out of memory (OOMKilled=true) or docker kill
139 Segmentation fault

Get a shell in the image to look around, bypassing its normal command:

docker run -it --rm --entrypoint sh IMAGE

"I published the port but can't reach it"

  1. The app inside must listen on 0.0.0.0, not 127.0.0.1. Otherwise it's unreachable from outside the container.
  2. Check the mapping: docker port CONTAINER.
  3. Check the host firewall or your provider's firewall.
  4. Remember that Docker bypasses UFW, so if it's too reachable, that's the post to read.

Handy one-liners

docker rm -f $(docker ps -aq)                        # remove ALL containers
docker stop $(docker ps -q)                          # stop all running containers
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
docker logs CONTAINER 2>&1 | grep -i error