Container Operations
| Command | Description |
|---|---|
docker run <image> | Run a container |
docker run -it <image> | Run in interactive mode |
docker run -d <image> | Run in background |
docker run -p 8080:80 <image> | Map ports |
docker ps | List running containers |
docker ps -a | List all containers |
docker stop <container> | Stop a container |
docker start <container> | Start a container |
docker rm <container> | Remove a container |
docker exec -it <container> sh | Execute shell in container |
Image Operations
| Command | Description |
|---|---|
docker images | List images |
docker pull <image> | Download an image |
docker build -t <name> . | Build an image |
docker rmi <image> | Remove an image |
docker tag <image> <new-name> | Tag an image |
docker push <image> | Push image to registry |
Logs & Information
| Command | Description |
|---|---|
docker logs <container> | Show container logs |
docker logs -f <container> | Follow logs in real-time |
docker inspect <container> | Show container details |
docker stats | Show resource usage |
Docker Compose
| Command | Description |
|---|---|
docker compose up | Start services |
docker compose up -d | Start in background |
docker compose down | Stop and remove services |
docker compose ps | List services |
docker compose logs | Show logs |
docker compose build | Build images |
Cleanup
| Command | Description |
|---|---|
docker system prune | Remove unused resources |
docker container prune | Remove stopped containers |
docker image prune | Remove unused images |
docker volume prune | Remove unused volumes |
Useful One-liners
| Command | Description |
|---|---|
docker stop $(docker ps -q) | Stop all containers |
docker rm $(docker ps -aq) | Remove all containers |
docker rmi $(docker images -q) | Remove all images |
docker logs --tail 100 -f <container> | Follow logs from last 100 lines |
docker exec -it <container> sh -c "env" | Check environment variables |
Troubleshooting
| Situation | Solution |
|---|---|
| Container won’t start | Check logs with docker logs <container> |
| Port already in use | Use lsof -i :<port> to find and kill the process |
| Out of disk space | Check usage with docker system df, then prune |
| Network issues | Inspect with docker network inspect <network> |
Related Articles
- Docker Container Internals - Understanding VM vs Container
- Container Orchestration - Scaling with Kubernetes
- Microservices Architecture - Container-based architecture