Docker is a platform for developing, shipping, and running applications in isolated environments called containers.
Installation
# Debian / Ubuntu (official Docker CE repository)
sudo apt install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update && sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# RHEL / CentOS / Fedora
sudo dnf install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Arch Linux
sudo pacman -S docker docker-compose
# Add your user to the docker group (avoids needing sudo for every command)
sudo usermod -aG docker $USER
# Log out and back in for the group change to take effectService Management
sudo systemctl enable --now docker # Start Docker and enable on boot
sudo systemctl status docker # Check if Docker is running
sudo systemctl restart docker # Restart Docker daemonImage Management
docker images # List all local images
docker pull ubuntu:latest # Download an image from Docker Hub
docker pull nginx:1.25-alpine # Download a specific version/tag
docker rmi ubuntu:latest # Delete a local image
docker build -t myapp:1.0 . # Build an image from Dockerfile in current directory
docker build -t myapp:1.0 -f custom.Dockerfile . # Use a specific Dockerfile
docker tag myapp:1.0 registry.example.com/myapp:1.0 # Tag for a remote registry
docker push registry.example.com/myapp:1.0 # Push image to a registry
docker history myapp:1.0 # Show the layer history of an image
docker image inspect myapp:1.0 # Show detailed image metadata as JSONContainer Management
docker ps # List running containers
docker ps -a # List all containers (including stopped)
docker run -d --name web nginx # Run in background (detached) with a name
docker run -it ubuntu bash # Run interactively with a bash shell
docker run --rm alpine echo "hello" # Run and auto-remove container when it exits
docker stop web # Gracefully stop a container (sends SIGTERM)
docker start web # Start a stopped container
docker restart web # Restart a running container
docker rm web # Delete a stopped container
docker rm -f web # Force delete a running container
docker rename old_name new_name # Rename a containerPort Forwarding and Volumes
# Map host port 8080 to container port 80
docker run -d -p 8080:80 nginx
# Map multiple ports
docker run -d -p 8080:80 -p 8443:443 nginx
# Bind mount: mount a host directory into the container
docker run -d -v /host/path:/container/path nginx
# Named volume: Docker manages the storage location
docker run -d -v myvolume:/var/lib/mysql mysql
# Read-only mount
docker run -d -v /host/config:/etc/nginx/conf.d:ro nginxEnvironment Variables
# Set environment variables at runtime
docker run -d -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=mydb mysql
# Load environment variables from a file
docker run -d --env-file .env myappLogs and Debugging
docker logs web # View container logs
docker logs -f web # Follow logs in real-time
docker logs --tail 100 web # Show last 100 log lines
docker inspect web # Show detailed container config as JSON
docker exec -it web sh # Execute a shell inside a running container
docker exec -it web bash # Execute bash (if available)
docker top web # Show running processes inside a container
docker stats # Show live CPU/memory/network usage for all containers
docker stats web # Show stats for a specific containerDocker Networking
docker network ls # List all networks
docker network create mynetwork # Create a custom bridge network
docker network inspect mynetwork # Show network details
docker network rm mynetwork # Delete a network
# Run a container on a specific network
docker run -d --name web --network mynetwork nginx
# Connect a running container to a network
docker network connect mynetwork web
# Disconnect a container from a network
docker network disconnect mynetwork webDocker Compose
Docker Compose (docker compose) defines and runs multi-container applications using a docker-compose.yml file.
docker compose up # Start all services defined in docker-compose.yml
docker compose up -d # Start in detached (background) mode
docker compose down # Stop and remove all containers, networks
docker compose down -v # Also remove named volumes
docker compose ps # List running compose services
docker compose logs # View logs for all services
docker compose logs -f web # Follow logs for a specific service
docker compose build # Build/rebuild service images
docker compose pull # Pull latest images for all services
docker compose restart # Restart all services
docker compose exec web sh # Execute a shell in a running serviceSystem Cleanup
docker system prune # Remove stopped containers, dangling images, unused networks
docker system prune -a # Also remove all unused images (not just dangling)
docker system prune --volumes # Also remove unused volumes
docker system df # Show Docker disk usage summary
docker volume prune # Remove all unused volumes
docker image prune # Remove dangling images only