- Published on
Docker Snippets for Container Management
views·3 mins read
Container Management
Remove All Stopped Containers
Clean up your environment by removing containers that are not running.
docker container prune
Stop All Running Containers
Stop every container currently running on your host.
docker stop $(docker ps -q)
Remove All Containers
Forcefully remove all containers (running or stopped).
docker rm -f $(docker ps -aq)
View Container Logs
Stream logs from a specific container to your terminal.
docker logs -f <container_id_or_name>
Execute Command in Running Container
Open an interactive shell inside a running container.
docker exec -it <container_id_or_name> /bin/bash
# Or for alpine-based images
docker exec -it <container_id_or_name> /bin/sh
Inspect Container Details
Get detailed JSON information about a container's configuration.
docker inspect <container_id_or_name>
Copy Files to/from Container
# From host to container
docker cp ./local-file.txt <container_id>:/path/in/container
# From container to host
docker cp <container_id>:/path/in/container ./local-file.txt
Image Management
Remove All Unused Images
Free up disk space by removing dangling images.
docker image prune -a
Build Image from Dockerfile
docker build -t my-app:latest .
Tag an Image
docker tag my-app:latest my-repo/my-app:v1.0
Push Image to Registry
docker push my-repo/my-app:v1.0
List Images by Size
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
Volumes & Networks
Remove Unused Volumes
docker volume prune
List All Networks
docker network ls
Create a Custom Network
docker network create my-network
Docker Compose
Start Services in Background
docker-compose up -d
Stop and Remove Resources
docker-compose down
View Compose Logs
docker-compose logs -f
Rebuild and Restart
docker-compose up -d --build
Run a One-off Command
docker-compose run --rm web-service npm test
Advanced Debugging
Check Resource Usage (Stats)
Monitor CPU, memory, and network usage in real-time.
docker stats
Check Disk Usage
See how much space Docker is using for images, containers, and volumes.
docker system df
Deep Clean (System Prune)
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
docker system prune -a --volumes