pull/this/repo
RoundupGit, GitHub and developer tools#docker#terminal#how-to

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.

RoundupDocker CLI cheat sheet: run, exec, logs, compose and cleanup

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.

CommandWhat it does
docker run -d --name web -p 8080:80 nginxstart a container in the background, port 8080 on your machine to 80 inside
docker pslist running containers
docker ps -alist all containers, including stopped ones
docker exec -it web shopen a shell inside a running container
docker logs -f webstream a container's output
docker stop webstop it gracefully
docker rm webdelete a stopped container
docker imageslist images on this machine
docker compose up -dstart every service in the Compose file, in the background
docker system pruneremove 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:

terminal
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:

terminal
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:

terminal
docker exec -it -u root web sh
docker exec -it -u 1000:1000 web sh

The 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?

FlagWhat it doesExample
-d, --detachrun the command in the backgrounddocker exec -d web touch /tmp/ok
-e, --envset an environment variabledocker exec -e DEBUG=1 web env
--env-fileread variables from a filedocker exec --env-file .env web env
-w, --workdirworking directory inside the containerdocker exec -it -w /app web sh
-u, --userrun as a user or UIDdocker 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.

FlagWhat it doesExample
-drun in the background and print the IDdocker run -d nginx
-itinteractive terminaldocker run -it ubuntu bash
--namegive the container a namedocker run -d --name web nginx
-ppublish a port, host:containerdocker run -p 127.0.0.1:80:8080 myapp
-vmount a host folder or a volumedocker run -v "$PWD":/app myapp
-eset an environment variabledocker run -e TZ=UTC myapp
--env-fileload variables from a filedocker run --env-file .env myapp
--rmdelete the container when it exitsdocker run --rm -it alpine sh
--restartrestart policydocker run -d --restart unless-stopped myapp
--networkjoin a networkdocker run --network mynet myapp
-wworking directorydocker run -w /app myapp
--entrypointreplace the image's entrypointdocker run --entrypoint sh myapp
--platformpick a platformdocker 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?

CommandWhat it does
docker ps -qonly container IDs
docker ps -lthe latest created container
docker ps -sinclude file sizes
docker logs --tail 100 weblast 100 lines (-n is the short form)
docker logs --since 10m weblogs from the last 10 minutes
docker logs -t webadd timestamps
docker statslive CPU and memory use of running containers
docker stats --no-streamone snapshot instead of a live feed
docker top webprocesses running inside a container
docker inspect weblow-level details as JSON
docker cp ./file.txt web:/appcopy a file into a container
docker cp web:/var/log/ ./logscopy files out of a container
docker stop -t 30 webstop, waiting 30 seconds before killing
docker start web / docker restart webstart or restart a container
docker rm -f webforce-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?

CommandWhat 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:alpinedownload an image
docker push myname/myapp:1.0upload an image to a registry
docker imageslist images (-a includes intermediate ones)
docker rmi myappdelete an image (-f forces it)
docker login / docker logoutsign 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.

CommandWhat it does
docker compose up -dcreate and start all services in the background
docker compose up -d --buildrebuild images first
docker compose up -d --force-recreaterecreate containers even if nothing changed
docker compose pslist the project's containers (-a includes stopped)
docker compose logs -ffollow logs from every service
docker compose logs -n 50 weblast 50 lines from one service
docker compose exec web shshell into a service (a TTY is allocated by default)
docker compose pulldownload newer images for the services
docker compose restart / stoprestart or stop services
docker compose downstop and remove containers and networks
docker compose down -valso remove named and anonymous volumes
docker compose configprint the resolved Compose file
docker compose -f other.yaml up -duse 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?

CommandWhat it removes
docker container pruneall stopped containers
docker image prunedangling images (-a for all unused images)
docker volume ls / docker volume prunelist volumes / remove unused local volumes
docker network lslist networks
docker system prunestopped containers, unused networks, dangling images, unused build cache
docker system prune -athe same, plus every image no container uses
docker system prune --volumesalso 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

  1. 1Docker Docs: docker CLI reference (read 2026-11-22)
  2. 2Docker Docs: docker container exec (read 2026-11-22)
  3. 3Docker Docs: docker container run (read 2026-11-22)
  4. 4Docker Docs: docker container ls (read 2026-11-22)
  5. 5Docker Docs: docker container logs (read 2026-11-22)
  6. 6Docker Docs: docker container stop (read 2026-11-22)
  7. 7Docker Docs: docker container rm (read 2026-11-22)
  8. 8Docker Docs: docker container cp (read 2026-11-22)
  9. 9Docker Docs: docker container stats (read 2026-11-22)
  10. 10Docker Docs: docker image ls and docker image rm (read 2026-11-22)
  11. 11Docker Docs: docker buildx build (read 2026-11-22)
  12. 12Docker Docs: docker compose (read 2026-11-22)
  13. 13Docker Docs: docker compose up (read 2026-11-22)
  14. 14Docker Docs: docker compose down (read 2026-11-22)
  15. 15Docker Docs: docker compose exec (read 2026-11-22)
  16. 16Docker Docs: docker system prune (read 2026-11-22)
  17. 17docker/compose

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.