Skillversitas

Docker Commands

A complete list of commonly used Docker commands for managing containers, images, and resources.

Container Commands

# Run a container
docker run [options] image [command]

# Common options:
-d              # Detach (background)
--name          # Name the container
-p host:container  # Port mapping
-v host:container  # Volume mount
-e KEY=VALUE    # Environment variable
--network       # Set the network
--restart       # Restart policy

# Example:
docker run -d --name web -p 80:80 -e NGINX_HOST=localhost nginx

# View containers
docker ps                    # Running containers
docker ps -a                 # All containers
docker ps -q                 # IDs only

# Control containers
docker start container_name  # Start
docker stop container_name   # Stop
docker restart container_name # Restart
docker pause container_name  # Pause
docker unpause container_name # Unpause
docker kill container_name   # Kill

# Delete containers
docker rm container_name     # Delete a stopped container
docker rm -f container_name  # Force delete

# Enter a container
docker exec -it container_name bash   # Run bash
docker exec container_name command    # Run a command
docker attach container_name          # Attach to the process

Image Commands

# View images
docker images                # All images
docker images -q             # IDs only

# Pull/Push images
docker pull image_name       # Download an image
docker push image_name       # Upload an image

# Build an image
docker build -t name:tag .   # Build from a Dockerfile
docker build --no-cache -t name:tag .  # Without cache

# Delete images
docker rmi image_name        # Delete an image
docker rmi -f image_name     # Force delete
docker image prune           # Delete unused images

# Tag an image
docker image tag source target

# View details
docker inspect image_name
docker history image_name

Volume Commands

# View volumes
docker volume ls

# Create a volume
docker volume create volume_name

# Delete a volume
docker volume rm volume_name
docker volume prune           # Delete unused volumes

# Inspect a volume
docker volume inspect volume_name

Network Commands

# View networks
docker network ls

# Create a network
docker network create network_name

# Delete a network
docker network rm network_name
docker network prune

# Inspect a network
docker network inspect network_name

# Connect/Disconnect a container
docker network connect network_name container_name
docker network disconnect network_name container_name

System Commands

# System info
docker info

# Statistics
docker stats                 # Real-time stats
docker stats --no-stream     # One-shot

# Disk usage
docker system df             # Summary
docker system df -v          # Detailed

# Cleanup
docker system prune          # Delete unused
docker system prune -a       # Delete everything
docker system prune -a --volumes  # Delete everything + volumes

# Events
docker events                # Real-time events
docker events --since="1h"   # Events from the last hour

Use Case: Daily Application Commands

# Deploy an update
docker-compose pull
docker-compose up -d

# Check status
docker-compose ps

# View logs
docker-compose logs -f

# Back up the database
docker exec myapp-db pg_dump -U admin myapp > backup.sql

# Restart a service
docker-compose restart api

# Cleanup
docker system prune -f

Docker Compose Commands

# Run all services
docker-compose up -d

# Stop all services
docker-compose down

# Check status
docker-compose ps

# View logs
docker-compose logs
docker-compose logs -f service_name

# Restart a service
docker-compose restart service_name

# Rebuild
docker-compose build
docker-compose up -d --build

# Scale a service
docker-compose up -d --scale api=3

Tips and Tricks

# Run a command in many containers
docker ps -q | xargs -I {} docker exec {} command

# Stop all running containers
docker stop $(docker ps -q)

# Delete all stopped containers
docker container prune -f

# Find containers by name
docker ps -a | grep myapp

# Export/Import a container
docker export container_name > container.tar
docker import container.tar new_image:tag

# Save/Load an image
docker save image_name > image.tar
docker load < image.tar

Everyday Use Case

Case: A developer needs to run several daily commands.

# Morning: Check the status of all services
docker-compose ps

# Afternoon: View logs while debugging
docker-compose logs -f api

# Evening: Cleanup before committing
docker system prune -f

# Night: Back up data
docker exec myapp-db pg_dump -U admin myapp > backup_$(date +%Y%m%d).sql

Aliases for Efficiency

# Add to ~/.bashrc or ~/.zshrc
alias dc='docker-compose'
alias dps='docker ps'
alias dpsa='docker ps -a'
alias dex='docker exec -it'
alias dlog='docker logs -f'
alias dprune='docker system prune -f'

# Usage:
dc up -d
dps
dex web bash
dlog api
dprune

Conclusion

Mastering Docker commands is very important for productivity. Create aliases for frequently used commands and learn the commands for troubleshooting.