# 30 Essential Docker Commands: A Practical Guide for SREs

### 1\. Container Management (Lifecycle)

| **Command** | **Purpose** | **Example** |
| --- | --- | --- |
| `docker ps` | List running containers. | `docker ps -a` (to see all containers, including those that exited with errors). |
| `docker run` | Create and start a container. | `docker run -d --name web nginx` (run a web server in the background/detached). |
| `docker stop` | Stop a running container. | `docker stop $(docker ps -q)` (stop all running containers at once). |
| `docker rm` | Remove a container. | `docker rm -f web` (force removal of a running container). |
| `docker exec` | Run a command in a container. | `docker exec -it web bash` (get an interactive shell inside a container). |
| `docker logs` | Fetch container logs. | `docker logs -f --tail 100 web` (follow the last 100 log lines in real-time). |

### 2\. Image Management

| **Command** | **Purpose** | **Example** |
| --- | --- | --- |
| `docker images` | List available images. | `docker images -q` (get only the IDs of all local images). |
| `docker pull` | Download an image. | `docker pull redis:latest` (ensure you have the latest version before deployment). |
| `docker build` | Build an image from a Dockerfile. | `docker build -t my-app:v1 .` (tag a new version of your application). |
| `docker rmi` | Remove an image. | `docker rmi $(docker images -f "dangling=true" -q)` (clean up unused/dangling images). |
| `docker tag` | Create a tag for an image. | `docker tag my-app:v1 myrepo/my-app:v1` (prepare an image for a registry). |
| `docker push` | Upload an image to a registry. | `docker push myrepo/my-app:v1` (deploy the image to Docker Hub or ACR/ECR). |

### 3\. Observability & Troubleshooting

| **Command** | **Purpose** | **Example** |
| --- | --- | --- |
| `docker stats` | Live resource usage. | `docker stats --no-stream` (get a snapshot of CPU/RAM usage per container). |
| `docker inspect` | Low-level info on objects. | `docker inspect web` (Information about the container) |
| `docker top` | Display running processes. | `docker top web` (see what processes are running inside the container host-side). |
| `docker port` | List port mappings. | `docker port web` (verify which host port is mapped to the container's port). |
| `docker events` | Real-time events from server. | `docker events --since 1h` (audit what happened in the Docker engine recently). |
| `docker diff` | Changes in the FS. | `docker diff web` (see what files were modified/added in the container layer). |

### 4\. Network & Volumes

| **Command** | **Purpose** | **Example** |
| --- | --- | --- |
| `docker network ls` | List networks. | `docker network ls` (check available drivers like bridge, host, or overlay). |
| `docker network inspect` | Detailed network info. | `docker network inspect bridge` (see which containers are attached to a network). |
| `docker volume ls` | List volumes. | `docker volume ls` (identify persistent data volumes). |
| `docker volume prune` | Remove unused volumes. | `docker volume prune` (recover disk space from orphaned volumes). |
| `docker cp` | Copy files to/from container. | `docker cp web:/etc/nginx/nginx.conf .` (extract a config file for local review). |
| `docker network connect` | Connect container to network. | `docker network connect my-net web` (dynamically attach a container to a new network). |

### 5\. Advanced & Cleanup

| **Command** | **Purpose** | **Example** |
| --- | --- | --- |
| `docker system prune` | Total cleanup. | `docker system prune -a --volumes` (wipe everything unused to free up disk). |
| `docker system df` | Show docker disk usage. | `docker system df` (diagnose why the Docker partition is full). |
| `docker-compose up` | Orchestrate multiple containers. | `docker-compose up -d` (deploy an entire stack defined in a YAML). |
| `docker-compose logs` | View logs from a stack. | `docker-compose logs -f app` (follow logs for a specific service in the stack). |
| `docker commit` | Create image from container. | `docker commit web my-emergency-image` (save a container's state for forensics). |
| `docker wait` | Wait for a container to stop. | `docker wait web` (useful in scripts to take action after a container finishes). |
