new
| | |
Docker Common Commands Cheat Sheet
Image Management (Images)
# Pull image from repository
docker pull [IMAGE_NAME]:[TAG]
# List local images
docker images
# Remove local image
docker rmi [IMAGE_ID/NAME]
# Filter and search images (Images Filter)
# Filter by dangling images (dangling=true or false)
docker images --filter "dangling=true"
# Filter by image creation time (before, since)
docker images --filter "before=ubuntu:latest"
docker images --filter "since=mysql:5.7"
# Filter by reference name
docker images --filter "reference=nginx:*"
# Build image (in the directory where Dockerfile is located)
docker build -t [IMAGE_NAME]:[TAG] .
# Force remove all unused images (dangling images)
docker image prune -f
Container Lifecycle (Containers)
# Run container
# -d run in background, -p port mapping host:container, --name specify name
docker run -d -p 8080:80 --name my-container [IMAGE_NAME]
# List running containers
docker ps
# List all containers (including stopped ones)
docker ps -a
# Stop container
docker stop [CONTAINER_ID/NAME]
# Start a stopped container
docker start [CONTAINER_ID/NAME]
# Restart container
docker restart [CONTAINER_ID/NAME]
# Remove a stopped container
docker rm [CONTAINER_ID/NAME]
# Force stop and remove a running container
docker rm -f [CONTAINER_ID/NAME]
# Clean up all stopped containers
docker container prune -f
Interaction & Debugging
# Enter the terminal of a running container
docker exec -it [CONTAINER_ID/NAME] /bin/bash
# OR (if bash is not supported)
docker exec -it [CONTAINER_ID/NAME] /bin/sh
# View container logs
docker logs -f [CONTAINER_ID/NAME]
# View the last 100 lines of logs
docker logs --tail 100 -f [CONTAINER_ID/NAME]
# View container detailed information (config, network, etc.)
docker inspect [CONTAINER_ID/NAME]
# List processes running inside the container
docker top [CONTAINER_ID/NAME]
# View container resource usage (CPU/Memory)
docker stats [CONTAINER_ID/NAME]
# Rename container
docker rename [OLD_NAME] [NEW_NAME]
# Stream events from the server in real-time
docker events
Advanced Image Operations
# Show history of an image (layers)
docker history [IMAGE_NAME]
# Save image(s) to a tar archive
docker save -o my-image.tar [IMAGE_NAME]
# Load an image from a tar archive
docker load -i my-image.tar
# Create a new image from a container's changes (use with caution, Dockerfile preferred)
docker commit [CONTAINER_ID] [NEW_IMAGE_NAME]
System Cleanup
# Show docker disk usage
docker system df
# One-key cleanup of all unused resources (images, containers, networks)
# Note: This removes all stopped containers and unused images
docker system prune -a --volumes
File Transfer
# Copy from host to container
docker cp [LOCAL_PATH] [CONTAINER_ID]:[CONTAINER_PATH]
# Copy from container to host
docker cp [CONTAINER_ID]:[CONTAINER_PATH] [LOCAL_PATH]
Network Management
# List networks
docker network ls
# Create a network
docker network create [NETWORK_NAME]
# Inspect a network
docker network inspect [NETWORK_NAME]
Volume Management (Volumes)
# List volumes
docker volume ls
# Create a volume
docker volume create [VOLUME_NAME]
# Remove all unused local volumes
docker volume prune -f
Docker Compose (Multi-container Orchestration)
# Start all services in background
docker-compose up -d
# Stop and remove containers, networks, images, and volumes
docker-compose down
# View service logs
docker-compose logs -f [SERVICE_NAME]
# Build or rebuild services
docker-compose build