Docker Command Reference
A systematic reference covering basic to advanced Docker commands used in daily operations.
Basic Information
Check Docker Version
docker version
Display detailed information:
docker info
Check System Information
# Check disk usage
docker system df
# System-wide information
docker system info
Image Management
List Images
# Display all images
docker images
# Display images sorted by size
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
Pull Images
# Download image from Docker Hub
docker pull <image_name>
# Specify a particular tag
docker pull <image_name>:<tag>
Examples:
docker pull ubuntu:22.04
docker pull ros:humble
Build Images
# Build image from Dockerfile
docker build -t <image_name> <path>
# Build with specific tag
docker build -t <image_name>:<tag> .
# Pass build arguments
docker build --build-arg <arg_name>=<value> -t <image_name> .
Examples:
docker build -t myapp:latest .
docker build -t myapp:v1.0 /path/to/dockerfile
Remove Images
# Remove specific image
docker rmi <image_id>
# Remove all unused images
docker image prune
# Force removal
docker rmi -f <image_id>
Container Management
Create and Run Containers
Basic Execution
# Create and run new container
docker run <image_id>
# Run in interactive mode
docker run -it <image_name> /bin/bash
# Run in background
docker run -d <image_name>
Advanced Options
# GUI-enabled container with volume mounting
docker run -it \
-v <local_path>:/data \
--device=/dev/dri \
--group-add video \
--volume=/tmp/.X11-unix:/tmp/.X11-unix \
--env="DISPLAY=$DISPLAY" \
--env="QT_X11_NO_MITSHM=1" \
--name=<container_name> \
<image_name>:<tag> \
/bin/bash
List Containers
# Display running containers
docker ps
# Display all containers (including stopped)
docker ps -a
# Display with custom format
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
Start, Stop, and Restart Containers
# Start existing container
docker start <container_id>
# Start in interactive mode
docker start -ia <container_id>
# Stop container
docker stop <container_id>
# Restart container
docker restart <container_id>
# Force stop
docker kill <container_id>
Connect to Containers
# Connect to running container
docker exec -it <container_id> /bin/bash
# Connect as specific user
docker exec -it --user <username> <container_id> /bin/bash
# Execute command
docker exec <container_id> <command>
Remove Containers
# Remove stopped container
docker rm <container_id>
# Force remove running container
docker rm -f <container_id>
# Remove all stopped containers
docker container prune
Image and Container Conversion
Create Image from Container
# Basic commit
docker commit <container_id> <new_image_name>
# Specify author and message
docker commit -a="<author>" -m="<message>" <container_id> <new_image_name>:<version>
Example:
docker commit -a="youkoutaku" -m="Added development tools" mycontainer myapp:v1.0
Tag Images
# Tag existing image with new name
docker tag <image_id> <username>/<repository>:<tag>
# Follow Docker Hub naming convention
docker tag myapp:latest youkoutaku/myapp:v1.0
Docker Hub Operations
Login and Logout
# Login to Docker Hub
docker login
# Login to specific registry
docker login <registry_url>
# Logout
docker logout
Push and Search Images
# Push image to Docker Hub
docker push <username>/<repository>:<tag>
# Search for images
docker search <image_name>
Volume and Network Management
Volume Management
# List volumes
docker volume ls
# Create volume
docker volume create <volume_name>
# Inspect volume
docker volume inspect <volume_name>
# Remove unused volumes
docker volume prune
Network Management
# List networks
docker network ls
# Create network
docker network create <network_name>
# Inspect network
docker network inspect <network_name>
# Connect container to network
docker network connect <network_name> <container_id>
Logging and Monitoring
View Logs
# Display container logs
docker logs <container_id>
# Follow logs in real-time
docker logs -f <container_id>
# Display last N lines
docker logs --tail 50 <container_id>
# Display logs with timestamps
docker logs -t <container_id>
Monitor Resource Usage
# Display real-time resource usage
docker stats
# Monitor specific container only
docker stats <container_id>
# Display statistics once
docker stats --no-stream
System Cleanup
Bulk Cleanup Commands
# Remove all unused resources
docker system prune
# Include images in cleanup
docker system prune -a
# Include volumes in cleanup
docker system prune --volumes
# Force execution (no confirmation)
docker system prune -f
Individual Cleanup
# Remove stopped containers
docker container prune
# Remove unused images
docker image prune
# Remove unused networks
docker network prune
# Remove unused volumes
docker volume prune
Troubleshooting
Common Diagnostic Commands
# List processes in container
docker top <container_id>
# Detailed container information
docker inspect <container_id>
# Show filesystem changes in container
docker diff <container_id>
# Show image history
docker history <image_id>
Ports and Processes
# Check port mappings
docker port <container_id>
# Check running processes
docker exec <container_id> ps aux
# Check network connections
docker exec <container_id> netstat -tlnp
Advanced Usage Examples
Bulk Operations on Multiple Containers
# Remove all stopped containers
docker rm $(docker ps -aq -f status=exited)
# Stop all running containers
docker stop $(docker ps -q)
# Remove containers with specific name pattern
docker rm $(docker ps -aq -f name=test*)
Docker Compose Integration
# Start services from Docker Compose file
docker-compose up
# Start in background
docker-compose up -d
# Stop and remove services
docker-compose down
# View logs
docker-compose logs
Command Aliases Examples
Useful aliases for efficient workflow:
# Add to ~/.bashrc or ~/.zshrc
alias dps='docker ps'
alias dpsa='docker ps -a'
alias di='docker images'
alias drm='docker rm'
alias drmi='docker rmi'
alias dstop='docker stop'
alias dstart='docker start'
alias dexec='docker exec -it'
alias dlogs='docker logs'
alias dclean='docker system prune -f'
References
Efficiency Tips
- Use tab completion to simplify entering container IDs and image names
- Filter
docker ps
ordocker images
output with grep - Use
--help
option to check detailed help for each command - Monitor real-time events with
docker system events