Useful Docker Commands

A practical cheat sheet of Docker commands for running, debugging, inspecting and cleaning up containers and images — the ones worth keeping close at hand.

Bart Tworek3 min readdockerclicontainers

Docker has a huge CLI surface, but day to day you only need a handful of commands. This is the list I keep coming back to — grouped by what you're trying to do, with a note on the flags that make each one more useful.

Running containers

Run a throwaway container

To start a container, get an interactive shell, and have it removed automatically when you exit:

docker run --rm -it <image-name>:<tag> sh

Run a container in the background with ports and environment variables

docker run -d --name my-api -p 8080:80 -e ASPNETCORE_ENVIRONMENT=Development <image-name>:<tag>

-p 8080:80 maps port 8080 on your machine to port 80 inside the container, -e sets an environment variable, and -d runs it detached in the background. Giving the container a --name means you can refer to it by name in every other command instead of copying its ID.

List containers

docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'

-a includes stopped containers (by default only running ones are shown), and --format trims the output down to the columns you actually care about.

Debugging containers

Follow a container's logs

docker logs -f --tail 100 <container-name>

-f streams new output as it's written, and --tail 100 starts from the last 100 lines instead of dumping the entire log history. Add --timestamps to prefix each line with the time it was written.

Open a shell inside a running container

docker exec -it <container-name> sh

This is the quickest way to check what files ended up in the image, whether environment variables are set correctly, or whether the container can reach another service on the network.

Copy files in and out of a container

docker cp <container-name>:/app/logs/error.log ./error.log
docker cp ./appsettings.json <container-name>:/app/appsettings.json

Handy for grabbing a log file or a crash dump without having to mount a volume in advance.

Inspect a container or image

To see the full low-level configuration of a container or image — environment variables, mounts, network settings, health check status and more — as JSON:

docker inspect <container-or-image-name>

The output is long, so use a Go template to pull out a single value. For example, to get a container's IP address:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-name>

Show live resource usage

To monitor CPU, memory, network and disk I/O for all running containers in real time:

docker stats

Add --no-stream to print a single snapshot and exit, which is useful in scripts.

Building images

Build without the cache and see full output

docker build --no-cache --progress=plain -t <image-name>:<tag> .

--no-cache forces every step to run again, which helps rule out stale layers when a build behaves unexpectedly. --progress=plain prints the complete output of each step instead of BuildKit's collapsed view, so you can see exactly what a RUN command printed before it failed.

Show an image's layers

To see each layer of an image, the instruction that created it, and how much space it takes up:

docker history --no-trunc --format '{{.ID}}\t{{.CreatedSince}}\t{{.CreatedBy}}\t{{.Size}}' <image-name>:<tag>

The --no-trunc flag stops Docker from truncating layer IDs and commands, so you can see the full instruction that created each layer. This is the first thing to check when an image is unexpectedly large — usually one layer stands out, and it's often a package cache or build artefact that should have been cleaned up in the same RUN step, or left behind in an earlier stage of a multi-stage build.

Docker Compose

docker compose up -d --build   # build images if needed and start all services in the background
docker compose logs -f api     # follow the logs of a single service
docker compose ps              # show the status of every service
docker compose down            # stop and remove containers and networks
docker compose down -v         # ...and also remove the volumes (deletes data!)

Cleaning up

Check how much space Docker is using

Before deleting anything, it's worth seeing where the space has gone:

docker system df

This breaks disk usage down into images, containers, local volumes, and build cache, along with how much of each is reclaimable.

Remove unused resources

Docker keeps stopped containers, dangling images, unused networks and build cache around by default, which can quietly consume tens of gigabytes over time. To remove everything not currently used by a running container:

docker system prune -a --volumes

Omit -a to only remove dangling images (untagged layers left behind by rebuilds) rather than every image without a running container, and omit --volumes to keep unused volumes — important if they hold data, such as a local database, that you want to keep.

If you'd rather clean up one category at a time:

docker container prune   # stopped containers
docker image prune -a    # images not used by any container
docker builder prune     # build cache
docker volume prune      # unused anonymous volumes

Note: prune commands ask for confirmation before deleting anything. Add -f to skip the prompt in scripts — but double-check what you're about to remove first, as there's no undo.