Checking Docker Information
# Display Docker version
docker version
# Show detailed Docker system information
docker info
# Get help with Docker commands
docker --help
Image Operations
Image operations can utilize image names, full IDs, or shortened IDs.
Listing Images
# Show local images
docker images
# Include intermediate layers
docker images -a
# Display only image IDs
docker images -q
# Show all image IDs including intermediate layers
docker images -qa
# Show image digest information
docker images --digests
# Display complete image information
docker images --no-trunc
# Show creation history of specified image
docker history -H redis
Searching for Images
# Search for MySQL images in registry
docker search mysql
# Filter results by stars
docker search --filter=stars=600 mysql
# Show full description
docker search --no-trunc mysql
# Show only automated builds
docker search --automated mysql
Pulling Images
# Download latest Redis image
docker pull redis
# Download all Redis images
docker pull -a redis
# Download from private repository
docker pull bitnami/redis
Removing Images
# Remove single image
docker rmi redis
# Force removal
docker rmi -f redis
# Remove multiple images
docker rmi -f redis tomcat nginx
# Remove all local images
docker rmi -f $(docker images -q)
Building Images
# Create Dockerfile
cd /docker/dockerfile
vim mycentos
# Build image from Dockerfile
docker build -f /docker/dockerfile/mycentos -t mycentos:1.1
Container Operasions
Container operations can use container IDs or names.
Starting Containers
# Run container interactively
docker run -i -t --name mycentos
# Run container in background
docker run -d mycentos
Note: Containers may exit immediately when running in background. Docker requires a foreground process to keep containers running.
# Start stopped container
docker start redis
# Restart container
docker restart redis
Container Processes
# List processes in container
docker top redis
# Show processes for all running containers
for container in $(docker ps | grep Up | awk '{print $1}'); do echo && docker top $container; done
Container Logs
# View container logs
docker logs rabbitmq
# Follow logs with timestamps
docker logs -f -t --tail=20 redis
# View recent logs since specific date
docker logs --since="2019-05-21" --tail=10 redis
Entering and Exiting Containers
# Enter container during creation
docker run -it centos /bin/bash
# Exit and stop container
exit
# Detach from container (Ctrl + P + Q)
# Attach to existing container
docker attach --sig-proxy=false centos
# Open new terminal session in container
docker exec -i -t centos /bin/bash
# Execute command and show output
docker exec -i -t centos ls -l /tmp
# Execute command in background
docker exec -d centos touch cache.txt
Viewing Containers
# Show running containers
docker ps
# Show only running container IDs
docker ps -q
# Show all containers
docker ps -a
# Show file sizes
docker ps -s
# Show most recent container
docker ps -l
# Show last 3 containers
docker ps -n 3
# Show complete output without truncation
docker ps --no-trunc
# Inspect image metadata
docker inspect redis
# Get container IP address
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis
Stopping and Removing Containers
# Stop running container
docker stop redis
# Kill running container
docker kill redis
# Remove stopped container
docker rm redis
# Force remove running container
docker rm -f redis
# Remove multiple containers
docker rm -f $(docker ps -a -q)
docker ps -a -q | xargs docker rm
# Remove network links
docker rm -l db
# Remove with volumes
docker rm -v redis
Volume Mounting
# Mount host directory to container
docker run -it -v /Desktop:/Desktop/container centos
# Mount as read-only
docker run -it -v /Desktop:/Desktop/container:ro centos
Creating Imagess from Containers
# Commit container to new image
docker commit -a="AuthorName" -m="Custom Redis" [redis-container-id] myredis:v1.1
Data Transfer Between Host and Container
# Copy from container to host
docker cp rabbitmq:/[container-path] [local-path]
# Copy from host to container
docker cp [local-path] rabbitmq:/[container-path]/
# Copy with path renaming
docker cp [local-path] rabbitmq:/[container-path]