Skip to content

Docker Commands Explained

The Docker commands used day to day, grouped by job: build and move images, run and stop containers, tune docker run with flags, read logs and stats, reach inside a container, copy files and talk to registries, manage volumes and networks, drive Compose, and reclaim disk space.

Docker splits into two objects: images (the read-only template) and containers (a running instance of an image). You build or pull an image, run it as a container, inspect what is happening, then clean up. Most people also use Compose to declare multi-container apps. Every command below has a short form; the long form is shown where it is the clearer one.

Reference table · 75 entries
75 of 75 rows
Build
Build an image from a Dockerfile.docker build -t app:1.0 .
Build the image again from scratch, ignoring cached layers.docker build --no-cache -t app:1.0 .
Build only up to a named stage in a multi-stage Dockerfile.docker build --target deps -t app:deps .
Build images through the buildx plugin, which drives BuildKit.docker buildx build -t app:1.0 .
Build one image for several CPU architectures.docker buildx build --platform linux/amd64,linux/arm64 -t app:1.0 --push .
Images
Download an image from a registry.docker pull node:20
List images stored locally.docker images
Create a new tag that points at the same image.docker tag app:1.0 app:latest
Remove one or more local images.docker rmi app:1.0
Container lifecycle
Create and start a container from an image.docker run -p 8080:3000 app
Create a container without starting it.docker create --name web app
Start a container that was stopped.docker start web
Stop a running container gracefully.docker stop web
Stop and start a container in one step.docker restart web
Force-stop a container without waiting for a graceful shutdown.docker kill web
Freeze all processes inside a container.docker pause web
Unfreeze the processes of a paused container.docker unpause web
Block until a container stops, then print its exit code.docker wait web
List running containers; add -a to include stopped ones.docker ps -a
Give an existing container a new name.docker rename web web-old
Change resource limits or the restart policy of a container.docker update --restart=unless-stopped web
Remove a stopped container.docker rm web
Run flags
Run the container in the background and print its ID.docker run -d -p 8080:80 app
Publish a container port on a host port.docker run -p 8080:80 app
Mount a host folder or a named volume into the container.docker run -v app-data:/data app
Set an environment variable in the container.docker run -e MODE=prod app
Give the container a fixed name to use in later commands.docker run --name web app
Delete the container when it exits.docker run --rm app
Start the container on a specific network.docker run --network net app
Tell Docker when to restart the container.docker run --restart unless-stopped app
Allocate a terminal and keep stdin open for an interactive session.docker run -it alpine sh
Replace the image's entrypoint with your own command.docker run --entrypoint sh app
Cap the memory or CPU share the container can use.docker run --memory 512m --cpus 0.5 app
Inspecting
Read a container's stdout and stderr; -f follows new output as it arrives.docker logs -f web
Show low-level config and state of a container or image.docker inspect web
Pull one field out of inspect with a Go template.docker inspect --format '{{.State.Status}}' web
List the host ports a container publishes.docker port web
Show files a container changed compared to its image.docker diff web
Live CPU, memory, and network for running containers.docker stats
List the processes running inside a container.docker top web
Stream Docker events from the daemon as they happen.docker events --filter type=container
Show disk space used by images, containers, volumes, and cache.docker system df
Show system-wide Docker settings and state.docker info
Exec & attach
Run a command inside a running container.docker exec -it web sh
Attach your terminal to a container's main process.docker attach web
Files & registry
Copy files between a container and the local filesystem.docker cp web:/app/logs ./logs
Sign in to a registry from the CLI.docker login
Sign out of a registry.docker logout
Upload a local image to a registry.docker push user/app:1.0
Write an image to a tar file.docker save -o app.tar app:1.0
Read an image from a tar file.docker load -i app.tar
Write a container's filesystem to a tar file.docker export web > web.tar
Create an image from a tar filesystem.docker import web.tar web:snap
Turn a container's current state into a new image.docker commit web app:snap
Volumes
Create a named volume for persistent data.docker volume create data
List volumes.docker volume ls
Remove a volume.docker volume rm data
Networks
List networks.docker network ls
Create a network so containers can reach each other.docker network create net
Connect a running container to a network.docker network connect net web
Compose
Build, (re)create, and start the services in compose.yaml; -d leaves them running in the background.docker compose up -d
Start the services and rebuild their images first.docker compose up --build -d
Run a command inside a running service container.docker compose exec api sh
Tail logs for one or more services.docker compose logs -f api
List the service containers Compose manages.docker compose ps
Stop the service containers without removing them.docker compose stop
Restart the service containers.docker compose restart api
Stop and remove containers and networks; -v also removes named volumes.docker compose down
Show the final Compose config after merges and env substitution.docker compose config
Cleanup
Remove stopped containers, dangling images, and unused networks; -a --volumes also removes unused images and volumes.docker system prune -af
Remove all stopped containers.docker container prune
Remove unused or dangling images.docker image prune -a
Remove volumes no container uses.docker volume prune
Remove networks no container uses.docker network prune
Remove the cached build layers.docker builder prune