Docker CLI cheat sheet: run, exec, logs, compose and cleanup
By Pull This RepoPosted 7 min read
Short answer
The Docker CLI commands you use most are docker run to start a container, docker ps to list them, docker exec -it to open a shell inside one, docker logs -f to watch its output, docker stop and docker rm to shut it down, docker images and docker build for images, docker compose up -d and down for multi-container apps, and docker system prune to clean up.
The Docker CLI is the docker command you type to start, inspect and clean up containers. This cheat sheet covers the commands most people need, grouped by job, with docker exec in the most detail because it is the one people get stuck on. Every flag here was checked against Docker's official CLI reference at docs.docker.com on 22 November 2026. No Docker yet? Start with our guide to installing Docker on Ubuntu.
Examples use a container named web and an image named myapp. Swap in your own names or IDs.
What are the most used Docker commands?
These ten cover most days. The short forms are official aliases: Docker's reference lists docker ps for docker container ls, docker exec for docker container exec, and so on.
| Command | What it does |
|---|---|
docker run -d --name web -p 8080:80 nginx | start a container in the background, port 8080 on your machine to 80 inside |
docker ps | list running containers |
docker ps -a | list all containers, including stopped ones |
docker exec -it web sh | open a shell inside a running container |
docker logs -f web | stream a container's output |
docker stop web | stop it gracefully |
docker rm web | delete a stopped container |
docker images | list images on this machine |
docker compose up -d | start every service in the Compose file, in the background |
docker system prune | remove stopped containers, unused networks, dangling images and build cache |
How does docker exec work?
docker exec runs a new command inside a container that is already running. The usage is docker exec [OPTIONS] CONTAINER COMMAND [ARG...]. The most common form opens an interactive shell:
docker exec -it web sh-i keeps standard input open and -t allocates a pseudo-terminal; together they give you a working prompt. Type exit to leave the shell.
Should you use sh or bash with docker exec?
Use sh unless you know the image has bash. Docker's own example is docker exec -it mycontainer sh, and the reference says the command "should be an executable". If bash is not installed in the image, docker exec -it web bash fails, so try sh first. When you need several commands in one go, wrap them in a shell, as the reference shows:
docker exec web sh -c "echo a && echo b"The reference notes that a chained or quoted command passed straight to exec, without sh -c, will not work.
How do you run docker exec as root or as another user?
Pass -u with a user name or UID, optionally followed by a group:
docker exec -it -u root web sh
docker exec -it -u 1000:1000 web shThe reference gives the format as <name|uid>[:<group|gid>]. There is also --privileged, which Docker describes as giving "extended privileges to the command". Reach for it only when a task fails without it.
What other docker exec flags are useful?
| Flag | What it does | Example |
|---|---|---|
-d, --detach | run the command in the background | docker exec -d web touch /tmp/ok |
-e, --env | set an environment variable | docker exec -e DEBUG=1 web env |
--env-file | read variables from a file | docker exec --env-file .env web env |
-w, --workdir | working directory inside the container | docker exec -it -w /app web sh |
-u, --user | run as a user or UID | docker exec -u 0 web whoami |
Without -w, the command runs in the container's default working directory.
Why does docker exec fail?
Two cases come straight from the reference. A paused container rejects exec with "Container mycontainer is paused, unpause the container before exec"; run docker unpause first. And an exec command only lives as long as the container's main process (PID 1): if the container stops, so does your command, and it is not restarted when the container restarts. If docker ps does not list the container, start it with docker start web before you exec into it.
How do you run containers with docker run?
docker run creates and starts a new container from an image, downloading the image first if needed.
| Flag | What it does | Example |
|---|---|---|
-d | run in the background and print the ID | docker run -d nginx |
-it | interactive terminal | docker run -it ubuntu bash |
--name | give the container a name | docker run -d --name web nginx |
-p | publish a port, host:container | docker run -p 127.0.0.1:80:8080 myapp |
-v | mount a host folder or a volume | docker run -v "$PWD":/app myapp |
-e | set an environment variable | docker run -e TZ=UTC myapp |
--env-file | load variables from a file | docker run --env-file .env myapp |
--rm | delete the container when it exits | docker run --rm -it alpine sh |
--restart | restart policy | docker run -d --restart unless-stopped myapp |
--network | join a network | docker run --network mynet myapp |
-w | working directory | docker run -w /app myapp |
--entrypoint | replace the image's entrypoint | docker run --entrypoint sh myapp |
--platform | pick a platform | docker run --platform linux/amd64 myapp |
Restart policies are no (the default), on-failure[:max-retries], always and unless-stopped. Port publishing has a security catch: the reference notes that -p 80:80 without an IP publishes on all interfaces, so it is reachable from outside. Prefix 127.0.0.1: to keep a port local.
How do you list, inspect and stop containers?
| Command | What it does |
|---|---|
docker ps -q | only container IDs |
docker ps -l | the latest created container |
docker ps -s | include file sizes |
docker logs --tail 100 web | last 100 lines (-n is the short form) |
docker logs --since 10m web | logs from the last 10 minutes |
docker logs -t web | add timestamps |
docker stats | live CPU and memory use of running containers |
docker stats --no-stream | one snapshot instead of a live feed |
docker top web | processes running inside a container |
docker inspect web | low-level details as JSON |
docker cp ./file.txt web:/app | copy a file into a container |
docker cp web:/var/log/ ./logs | copy files out of a container |
docker stop -t 30 web | stop, waiting 30 seconds before killing |
docker start web / docker restart web | start or restart a container |
docker rm -f web | force-remove a running container |
docker stop sends SIGTERM, waits (10 seconds by default on Linux, 30 on Windows), then sends SIGKILL if the container is still running.
How do you build and manage images?
| Command | What it does |
|---|---|
docker build -t myapp . | build an image from the Dockerfile in this folder |
docker build -t myapp -f Dockerfile.dev . | use a different Dockerfile |
docker build --no-cache -t myapp . | build without the cache |
docker build --build-arg VERSION=2 -t myapp . | pass a build-time variable |
docker pull nginx:alpine | download an image |
docker push myname/myapp:1.0 | upload an image to a registry |
docker images | list images (-a includes intermediate ones) |
docker rmi myapp | delete an image (-f forces it) |
docker login / docker logout | sign in to or out of a registry |
docker build is an alias for docker buildx build; the . at the end is the build context, the folder sent to the builder.
What are the docker compose commands?
Compose runs multi-container apps defined in a compose.yaml file. Run these from the folder that holds it.
| Command | What it does |
|---|---|
docker compose up -d | create and start all services in the background |
docker compose up -d --build | rebuild images first |
docker compose up -d --force-recreate | recreate containers even if nothing changed |
docker compose ps | list the project's containers (-a includes stopped) |
docker compose logs -f | follow logs from every service |
docker compose logs -n 50 web | last 50 lines from one service |
docker compose exec web sh | shell into a service (a TTY is allocated by default) |
docker compose pull | download newer images for the services |
docker compose restart / stop | restart or stop services |
docker compose down | stop and remove containers and networks |
docker compose down -v | also remove named and anonymous volumes |
docker compose config | print the resolved Compose file |
docker compose -f other.yaml up -d | use a different Compose file |
down keeps your named volumes unless you add -v, and it never removes external networks or volumes. Treat down -v as "delete the database".
How do you clean up Docker disk space?
| Command | What it removes |
|---|---|
docker container prune | all stopped containers |
docker image prune | dangling images (-a for all unused images) |
docker volume ls / docker volume prune | list volumes / remove unused local volumes |
docker network ls | list networks |
docker system prune | stopped containers, unused networks, dangling images, unused build cache |
docker system prune -a | the same, plus every image no container uses |
docker system prune --volumes | also prune anonymous volumes |
Every prune asks "Are you sure you want to continue? [y/N]" unless you add -f. Volumes hold data such as databases, so read the list before you confirm a volume prune.
What is the catch?
The Docker CLI has far more commands than this page, including swarm, contexts and Buildx builders, and some flags need a newer API version, which the reference marks per option. Two habits prevent most accidents: never publish a database port without 127.0.0.1: on a public server, and never run down -v or a volume prune on data you have not backed up. Inside a container you are usually in a minimal Linux system, so our Linux commands cheat sheet is the next tab to keep open.
For a real multi-container stack to practise on, see how to self-host Supabase with Docker.
Pull it. Stash it.
Questions people ask
How do I get a shell inside a running Docker container?
Run docker exec -it followed by the container name and sh, for example docker exec -it web sh. The -i flag keeps input open and -t allocates a terminal. Use bash in place of sh only if the image includes bash, because docker exec can only run a program that exists in the container.
What is the difference between docker run and docker exec?
docker run creates and starts a new container from an image. docker exec runs an extra command inside a container that is already running. Docker's reference says an exec command only runs while the container's main process is running and is not restarted if the container restarts.
How do I run docker exec as root?
Pass the user with -u, for example docker exec -it -u root web sh. The -u flag takes a user name or UID, with an optional group after a colon. For extra kernel privileges, docker exec also has a --privileged flag, which Docker describes as giving extended privileges to the command.
What does docker system prune delete?
By default it removes stopped containers, networks not used by any container, dangling images and unused build cache. Add -a to also remove every unused image, and --volumes to also prune anonymous volumes. It asks for confirmation unless you add -f.
Is it docker-compose or docker compose?
Current Docker installs ship Compose as a CLI plugin, so the command is docker compose with a space, for example docker compose up -d. The Docker CLI reference documents Compose under docker compose. Compose itself is Apache-2.0 licensed and had 39,067 stars on GitHub on 22 November 2026.
Sources
- 1Docker Docs: docker CLI reference (read 2026-11-22)
- 2Docker Docs: docker container exec (read 2026-11-22)
- 3Docker Docs: docker container run (read 2026-11-22)
- 4Docker Docs: docker container ls (read 2026-11-22)
- 5Docker Docs: docker container logs (read 2026-11-22)
- 6Docker Docs: docker container stop (read 2026-11-22)
- 7Docker Docs: docker container rm (read 2026-11-22)
- 8Docker Docs: docker container cp (read 2026-11-22)
- 9Docker Docs: docker container stats (read 2026-11-22)
- 10Docker Docs: docker image ls and docker image rm (read 2026-11-22)
- 11Docker Docs: docker buildx build (read 2026-11-22)
- 12Docker Docs: docker compose (read 2026-11-22)
- 13Docker Docs: docker compose up (read 2026-11-22)
- 14Docker Docs: docker compose down (read 2026-11-22)
- 15Docker Docs: docker compose exec (read 2026-11-22)
- 16Docker Docs: docker system prune (read 2026-11-22)
- 17docker/compose
More repos like this
Get tomorrow's repo in your feed
One free, open source repo a day as a short video, shown working, with the catch named. Pull it. Stash it.
