🔹 Container Lifecycle
| Command | Description | Where to Run |
|---|---|---|
docker run IMAGE | Run a container from an image (foreground). | Host terminal |
docker run -d IMAGE | Run a container in the background. | Host terminal |
docker run --name NAME IMAGE | Run and assign a name. | Host terminal |
docker start CONTAINER | Start a stopped container. | Host terminal |
docker stop CONTAINER | Gracefully stop a running container. | Host terminal |
docker restart CONTAINER | Restart a container. | Host terminal |
docker kill CONTAINER | Force stop (SIGKILL) a container. | Host terminal |
docker rm CONTAINER | Remove a stopped container. | Host terminal |
docker ps | List running containers. | Host terminal |
docker ps -a | List all containers. | Host terminal |
🔹 Images
| Command | Description | Where to Run |
|---|---|---|
docker images | List all images. | Host terminal |
docker pull IMAGE[:TAG] | Download/pull image. | Host terminal |
docker push IMAGE[:TAG] | Upload/push image to a registry. | Host terminal |
docker rmi IMAGE | Remove image. | Host terminal |
docker build -t NAME:TAG . | Build from Dockerfile. | Host terminal, inside folder with Dockerfile |
🔹 Volumes & Storage
| Command | Description | Where to Run |
|---|---|---|
docker volume ls | List volumes. | Host terminal |
docker volume create NAME | Create a volume. | Host terminal |
docker volume inspect NAME | Inspect volume details. | Host terminal |
docker volume rm NAME | Remove volume. | Host terminal |
docker system df | Show Docker disk usage. | Host terminal |
🔹 Networks
| Command | Description | Where to Run |
|---|---|---|
docker network ls | List networks. | Host terminal |
docker network create NAME | Create a network. | Host terminal |
docker network inspect NAME | Inspect network details. | Host terminal |
docker network rm NAME | Remove a network. | Host terminal |
docker run --network=NAME IMAGE | Run container in specific network. | Host terminal |
🔹 Exec & Logs
| Command | Description | Where to Run |
|---|---|---|
docker exec -it CONTAINER bash | Open an interactive shell in container. | Host terminal → opens a shell inside container |
docker logs CONTAINER | Show logs. | Host terminal |
docker logs -f CONTAINER | Follow logs (stream). | Host terminal |
docker top CONTAINER | Show processes inside container. | Host terminal |
docker inspect CONTAINER | Show full container configuration/metadata. | Host terminal |
🔹 System Cleanup
| Command | Description | Where to Run |
|---|---|---|
docker system prune | Remove unused data (containers, networks, dangling images). | Host terminal |
docker system prune -a | Also remove unused images. | Host terminal |
docker builder prune | Clean build cache. | Host terminal |
docker volume prune | Remove unused volumes. | Host terminal |
🔹 Docker Compose
| Command | Description | Where to Run |
|---|---|---|
docker compose up | Start project (foreground). | Host terminal, inside project folder with docker-compose.yml |
docker compose up -d | Start project in background (detached). | Host terminal, inside project folder |
docker compose down | Stop and remove containers/networks. | Host terminal, inside project folder |
docker compose down -v | Also remove named/anonymous volumes. | Host terminal, inside project folder |
docker compose stop | Stop containers (keep them). | Host terminal, inside project folder |
docker compose start | Start previously stopped containers. | Host terminal, inside project folder |
docker compose restart | Restart containers. | Host terminal, inside project folder |
docker compose ps | List project containers. | Host terminal, inside project folder |
docker compose logs -f | Follow logs of all services. | Host terminal, inside project folder |
docker compose logs -f SERVICE | Follow logs for a single service. | Host terminal, inside project folder |
docker compose exec SERVICE bash | Open shell in a service container. | Host terminal, inside project folder |
🔹 Useful One-Liners
Danger zone
These commands can wipe large parts of your Docker setup if you’re not careful.
Double-check before running, and never do this on a host you care about without backups.
All of these run in the host terminal:
-
Stop all running containers:
docker ps -q | xargs docker stop
- Remove all containers:
```bash
docker ps -aq | xargs docker rm -f
```
- Remove all images:
```bash
docker images -q | xargs docker rmi -f
```
- Remove all volumes:
```bash
docker volume ls -q | xargs docker volume rm
```
---
⚠️ **Rule of thumb**
- If it starts with `docker …` → run it in your **host terminal** (outside containers).
- If it’s `docker exec -it CONTAINER bash` → you’ll land **inside the container** and then run Linux commands there.
- If it’s `docker compose …` → run it in the **folder that has your `docker-compose.yml`** (unless you pass `-f` to another file).
- Add `sudo` at the start if your Docker setup requires root.