Docker is a platform that allows you to package and run applications in lightweight, portable containers regardless of the environment in which it’s installed.
Image Management
Action
Command
List all images
docker images -a
Pull an image
docker pull postgres:latest
Build an image
docker build -t myapp .
Remove an image
docker rmi <image_id>
Remove all images
docker rmi $(docker images -q)
Container Management
Action
Command
Run a container
docker run <image>
Run in background
docker run -d <image>
Stop a container
docker stop <container_id>
Restart a container
docker restart <container_id>
Pause
docker pause <container_id>
Unpause
docker unpause <container_id>
Remove a container
docker rm <container_id>
Remove all containers
docker rm $(docker ps -aq)
Stop all containers
docker stop $(docker ps -q)
Inspection and Monitoring
Action
Command
List active containers
docker ps
List all containers
docker ps -a
View container logs
docker logs <container_id>
View resource usage
docker stats
Inspect a container
docker inspect <container_id>
Connect to a container
docker exec -it <container_id> bash(or sh)
List Docker networks
docker network ls
List Docker volumes
docker volume ls
Docker system info
docker info
Check installed versions
docker version
Cleanup (Prune)
Action
Command
Clean stopped containers
docker container prune
Clean unused images
docker image prune
Clean unused networks
docker network prune
Clean unused volumes
docker volume prune
Full cleanup (dangerous!)
docker system prune -a
Remove untagged images
docker image prune -a
Troubleshooting and Debug
Action
Command
View real-time logs
docker logs -f <container_id>
View container processes
docker top <container_id>
Container statistics
docker stats <container_id>
Inspect file changes
docker diff <container_id>
Image history
docker history <image_id>
Test network connectivity
docker exec <container_id> ping google.com
Copy from container to host
docker cp <container>:/path/file ./file
Copy from host to container
docker cp ./file <container>:/path/file
Dockerfile
Dockerfile = recipe for creating a custom image
Simple Example - Nginx
FROM nginx:alpineCOPY index.html /usr/share/nginx/html/EXPOSE 80