Docker packages applications and their dependencies into portable containers that run the same way everywhere. This cheat sheet groups the Docker and Docker Compose commands you use every day — building images, running containers, reading logs, managing volumes and networks, and cleaning up.
Keep this page bookmarked next to your terminal. Each command comes with a short explanation of what it does. Working on the command line more broadly? See the <a href="/cheatsheet/linux-commands">Linux Commands cheat sheet</a>.
Images
| Command | What it does |
|---|
docker pull <image> | Download an image from a registry (e.g. Docker Hub). |
docker images | List images stored locally. |
docker build -t name:tag . | Build an image from the Dockerfile in the current folder. |
docker rmi <image> | Remove a local image. |
docker tag src target | Add a new tag to an existing image. |
docker push name:tag | Upload an image to a registry. |
docker history <image> | Show the layers that make up an image. |
Containers
| Command | What it does |
|---|
docker run <image> | Create and start a container from an image. |
docker run -d <image> | Run a container in the background (detached). |
docker run -p 8080:80 <image> | Map host port 8080 to container port 80. |
docker run -it <image> sh | Run interactively and open a shell. |
docker ps | List running containers. |
docker ps -a | List all containers, including stopped ones. |
docker stop <container> | Gracefully stop a running container. |
docker start <container> | Start a stopped container. |
docker rm <container> | Remove a stopped container. |
docker exec -it <container> sh | Open a shell inside a running container. |
Logs & Inspection
| Command | What it does |
|---|
docker logs <container> | Show a container's logs. |
docker logs -f <container> | Follow (stream) a container's logs live. |
docker inspect <container> | Show low-level details as JSON. |
docker stats | Live CPU, memory, and network usage per container. |
docker top <container> | Show the processes running in a container. |
Volumes & Networks
| Command | What it does |
|---|
docker volume create <name> | Create a named volume for persistent data. |
docker volume ls | List volumes. |
docker run -v host:/path <image> | Mount a host directory into a container. |
docker network ls | List networks. |
docker network create <name> | Create a user-defined network. |
Docker Compose
| Command | What it does |
|---|
docker compose up | Create and start all services from compose.yaml. |
docker compose up -d | Start all services in the background. |
docker compose down | Stop and remove services, networks, and containers. |
docker compose ps | List the services defined in the project. |
docker compose logs -f | Follow logs from all services. |
docker compose build | Build or rebuild the services' images. |
Cleanup
| Command | What it does |
|---|
docker system prune | Remove stopped containers, unused networks, and dangling images. |
docker system prune -a | Also remove all unused images, not just dangling ones. |
docker image prune | Remove dangling images. |
docker volume prune | Remove unused volumes. |
docker container prune | Remove all stopped containers. |
Docker Commands Cheat Sheet FAQs
What is the difference between a Docker image and a container?
An image is a read-only template that includes your app and its dependencies. A container is a running (or stopped) instance created from an image. You can start many containers from the same image.
What is the difference between docker run and docker start?
docker run creates a new container from an image and starts it. docker start restarts an existing, stopped container. Use run the first time and start to bring a stopped container back.
How do I get a shell inside a running container?
Use docker exec -it <container> sh (or bash if the image has it). The -it flags attach an interactive terminal so you can run commands inside the container.
How do I remove all stopped containers and unused images?
Run docker system prune to remove stopped containers, unused networks, and dangling images. Add -a to also remove all images that aren't used by a container. Be careful — this frees a lot of space but is not reversible.
What is Docker Compose used for?
Docker Compose defines and runs multi-container apps from a single compose.yaml file. docker compose up starts every service at once, and docker compose down stops and removes them — ideal for local development stacks.
How do I persist data when a container is removed?
Use a volume. Create one with docker volume create name and mount it with -v name:/path, or mount a host folder with -v /host/path:/path. Data in a volume survives after the container is deleted.
How do I expose a container's port to my machine?
Use -p host:container when running, for example docker run -p 8080:80 nginx maps your machine's port 8080 to the container's port 80, so you can open http://localhost:8080.