Introduction
Docker commands are used to build images, run containers, manage images and volumes, inspect running applications, and clean up unused resources. They are the main way to work with Docker from the terminal.
What Are Docker Commands?
Basic Concept
Docker commands are terminal instructions that control Docker’s main objects:
- Images — The template or blueprint
- Containers — The running instance created from an image
- Networks — Enable container communication
- Volumes — Persistent storage
Why Docker Matters
Docker is widely used because it makes applications portable:
- The same image can often run on different systems if Docker is installed
- Makes development, testing, and deployment easier
- Reduces “works on my machine” problems
- Enables consistent environments across teams
General Docker Commands
Basic Information and Setup
These commands are used for basic Docker management and information.
| Command | Purpose |
|---|---|
docker --help | Show main Docker help menu |
docker info | Display Docker system information |
docker version | Show Docker client and server version |
docker login | Login to Docker Hub or registry |
docker logout | Sign out from registry |
Examples
# Show Docker help menu
docker --help
# Display system information
docker info
# Check Docker version
docker version
# Login to Docker Hub
docker login
# Logout from registry
docker logoutWhen to Use
These commands are useful when:
- Setting up Docker for the first time
- Checking whether Docker is working correctly
- Managing registry authentication
- Verifying system configuration
Image Commands
What Are Docker Images?
Images are the blueprints for containers. You use image commands to list, build, download, upload, and delete images.
Image Management Commands
| Command | Purpose |
|---|---|
docker images | List all local images |
docker pull <image> | Download image from registry |
docker push <image> | Upload image to registry |
docker rmi <image> | Remove an image |
docker build | Build image from Dockerfile |
DOCKER IMAGES — List Images
Lists all local images stored on your machine.
# List all images
docker images
# List with more details
docker images --digests
# Show only image IDs
docker images -q
# Filter images
docker images --filter "dangling=true"DOCKER PULL — Download Image
Downloads an image from Docker Hub or another registry.
# Pull Ubuntu image
docker pull ubuntu
# Pull specific version (tag)
docker pull ubuntu:20.04
# Pull from custom registry
docker pull myregistry.com/myimage:latest
# Pull with progress
docker pull nginxDOCKER PUSH — Upload Image
Uploads a local image to a registry.
# Push to Docker Hub
docker push myusername/myapp:1.0
# Push to custom registry
docker push myregistry.com/myapp:latest
# Tag before pushing
docker tag myapp:latest myusername/myapp:latest
docker push myusername/myapp:latestDOCKER RMI — Remove Image
Removes an image from your system.
# Remove an image
docker rmi myapp
# Remove by image ID
docker rmi 8ac48589692a
# Force remove
docker rmi -f myapp
# Remove multiple images
docker rmi image1 image2 image3
# Remove all images
docker rmi $(docker images -q)DOCKER BUILD — Build Image
Builds an image from a Dockerfile.
# Build from Dockerfile in current directory
docker build -t myapp .
# Build with specific Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
# Build without cache
docker build --no-cache -t myapp .
# Build with build arguments
docker build --build-arg VERSION=1.0 -t myapp .
# Build with multiple tags
docker build -t myapp:latest -t myapp:1.0 .Practical Image Examples
# Download Ubuntu image
docker pull ubuntu
# Build custom image
docker build -t myapp .
# Tag image for registry
docker tag myapp myusername/myapp:1.0
# Push to registry
docker push myusername/myapp:1.0
# Delete when no longer needed
docker rmi myappContainer Run Commands
Creating and Starting Containers
These commands create and start containers from images.
DOCKER RUN — Create and Run
Creates and starts a container from an image.
| Option | Purpose |
|---|---|
--name | Give container a custom name |
-d | Run in detached mode (background) |
-p | Map ports |
-v | Mount volumes |
-e | Set environment variables |
--rm | Auto-remove after exit |
-it | Interactive terminal |
Basic Examples
# Simple run
docker run ubuntu echo "Hello Docker"
# Run with custom name
docker run --name mycontainer ubuntu
# Run in detached mode (background)
docker run -d nginx
# Keep running in background
docker run -d --name web nginxPort Mapping
# Map single port
docker run -p 8080:80 nginx
# Host port 8080 → Container port 80
# Map multiple ports
docker run -p 8080:80 -p 3000:3000 myapp
# Map to random port
docker run -p 80 nginxVolumes and Mounts
# Mount a directory
docker run -v /host/path:/container/path myapp
# Mount named volume
docker run -v myvolume:/data myapp
# Read-only mount
docker run -v /host/path:/container/path:ro myappEnvironment Variables
# Set environment variable
docker run -e DATABASE_URL=postgres://localhost myapp
# Set multiple variables
docker run -e VAR1=value1 -e VAR2=value2 myapp
# Load from file
docker run --env-file .env myappAuto-Cleanup
# Remove container after it exits
docker run --rm alpine echo "Temporary container"
# Useful for temporary tasks
docker run --rm -v $(pwd):/data ubuntu ls -la /dataInteractive Mode
# Interactive terminal
docker run -it ubuntu bash
# Run command and keep attached
docker run -it --name myapp myapp:latestComprehensive Example
docker run -d \
--name web \
-p 8080:80 \
-v /data:/app/data \
-e ENVIRONMENT=production \
--rm \
nginx:latestContainer Management Commands
Controlling Containers
Once containers are created, these commands help you control them.
| Command | Purpose |
|---|---|
docker ps | Show running containers |
docker ps -a | Show all containers |
docker start | Start stopped container |
docker stop | Stop running container |
docker restart | Restart container |
docker rm | Remove container |
docker kill | Force stop container |
DOCKER PS — List Containers
Shows containers.
# Show running containers
docker ps
# Show all containers (including stopped)
docker ps -a
# Show only container IDs
docker ps -q
# Show with custom format
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
# Filter containers
docker ps -a --filter "status=exited"
docker ps -a --filter "name=web"DOCKER START/STOP — Container Control
Start stopped containers or stop running containers.
# Start a stopped container
docker start container_name
# Stop a running container gracefully
docker stop container_name
# Stop with timeout
docker stop -t 10 container_name
# Stop all running containers
docker stop $(docker ps -q)DOCKER RESTART — Restart Container
Stops and starts the container again.
# Restart a container
docker restart container_name
# Restart with timeout
docker restart -t 10 container_nameDOCKER RM — Remove Container
Removes a stopped container.
# Remove a container
docker rm container_name
# Remove running container (force)
docker rm -f container_name
# Remove multiple containers
docker rm container1 container2 container3
# Remove all stopped containers
docker container pruneDOCKER KILL — Force Stop
Stops a container immediately.
# Force stop a container
docker kill container_name
# Kill all running containers
docker kill $(docker ps -q)Practical Container Management
# Start a web server
docker run -d --name web nginx
# List running containers
docker ps
# Stop the container
docker stop web
# List all containers
docker ps -a
# Start it again
docker start web
# Remove it
docker rm webContainer Access Commands
Entering Running Containers
These commands let you enter or interact with a running container.
| Command | Purpose |
|---|---|
docker exec | Run command inside container |
docker attach | Connect to container process |
DOCKER EXEC — Execute Commands
Runs a command inside a running container interactively.
# Run a single command
docker exec container_name ls -la
# Open interactive shell
docker exec -it container_name bash
docker exec -it container_name sh
# Run as specific user
docker exec -u root container_name whoami
# Set environment variable
docker exec -e VAR=value container_name echo $VAR
# Run with working directory
docker exec -w /app container_name pwdDOCKER ATTACH — Connect to Container
Connects your terminal to the main process of the container.
# Attach to container
docker attach container_name
# Detach without stopping (Ctrl + P, Ctrl + Q)Practical Examples
# Explore container filesystem
docker exec -it mycontainer bash
# Run specific command
docker exec -it mycontainer python script.py
# Check logs inside
docker exec mycontainer cat /var/log/app.log
# Create a file inside
docker exec mycontainer touch /tmp/test.txtLogs and Inspection
Understanding Container Behavior
These commands help you understand what a container is doing and why it may have failed.
| Command | Purpose |
|---|---|
docker logs | Show container output |
docker inspect | Show detailed information |
docker stats | Show resource usage |
docker top | Show running processes |
DOCKER LOGS — View Output
Shows the output printed by the container.
# Show all logs
docker logs container_name
# Follow logs live
docker logs -f container_name
# Show last 10 lines
docker logs --tail 10 container_name
# Show logs with timestamps
docker logs -t container_name
# Show last hour of logs
docker logs --since 1h container_name
# Show until specific time
docker logs --until 10m container_name
# Combine options
docker logs -f --tail 50 -t container_nameDOCKER INSPECT — Detailed Information
Shows detailed JSON information about containers or images.
# Inspect a container
docker inspect container_name
# Get specific field
docker inspect --format='{{.NetworkSettings.IPAddress}}' container_name
# Inspect image
docker inspect image_name
# Inspect network
docker inspect network_nameDOCKER STATS — Resource Usage
Shows live CPU, memory, network, and I/O usage.
# Show stats for all containers
docker stats
# Show stats for specific container
docker stats container_name
# Non-streaming mode
docker stats --no-streamDOCKER TOP — View Processes
Shows the processes running inside a container.
# Show processes
docker top container_name
# Show with detailed info
docker top container_name -efDebugging Scenario
# Container crashed?
docker logs myapp
# Follow logs in real-time
docker logs -f myapp
# Check resource usage
docker stats myapp
# Inspect container details
docker inspect myapp
# Check running processes
docker top myappCopy Commands
File Transfer Between Host and Container
Docker can copy files between the host machine and containers.
| Command | Purpose |
|---|---|
docker cp container:path host:path | Copy FROM container |
docker cp host:path container:path | Copy TO container |
DOCKER CP — Copy Files
# Copy file from container to host
docker cp mycontainer:/var/log/app.log ./app.log
# Copy file from host to container
docker cp ./config.yml mycontainer:/etc/config.yml
# Copy directory
docker cp mycontainer:/app/logs ./logs
# Copy to different path
docker cp mycontainer:/data mycontainer:/backupPractical Use Cases
# Pull application logs
docker cp myapp:/var/log/app.log ./logs/
# Add configuration during debugging
docker cp config.json mycontainer:/app/
# Extract generated files
docker cp myapp:/output/results.csv ./results/
# Backup database files
docker cp db_container:/var/lib/postgresql ./db_backup/Network Commands
Container Communication
Docker networks connect containers to each other and to the outside world.
| Command | Purpose |
|---|---|
docker network create | Create new network |
docker network ls | List networks |
docker network inspect | Show network details |
docker network connect | Add container to network |
docker network disconnect | Remove from network |
DOCKER NETWORK CREATE — Create Network
Creates a new network.
# Create bridge network
docker network create mynetwork
# Create custom subnet
docker network create --subnet=192.168.0.0/16 mynetwork
# Create overlay network (for swarm)
docker network create --driver overlay mynetworkDOCKER NETWORK LS — List Networks
Lists Docker networks.
# List all networks
docker network ls
# Filter networks
docker network ls --filter driver=bridgeDOCKER NETWORK INSPECT — Network Details
Shows detailed information about a network.
# Inspect network
docker network inspect mynetwork
# See connected containers
docker network inspect mynetworkDOCKER NETWORK CONNECT/DISCONNECT
Manages container network membership.
# Connect container to network
docker network connect mynetwork container_name
# Disconnect from network
docker network disconnect mynetwork container_nameMulti-Container Networking Example
# Create network
docker network create myapp
# Run web container
docker run -d --name web --network myapp nginx
# Run database container
docker run -d --name db --network myapp postgres
# Both can communicate by name: web → dbVolume Commands
Persistent Storage
Volumes are used for persistent storage. They keep data even if the container is removed.
| Command | Purpose |
|---|---|
docker volume create | Create new volume |
docker volume ls | List volumes |
docker volume inspect | Show volume details |
docker volume rm | Delete volume |
DOCKER VOLUME CREATE — Create Volume
Creates a new volume.
# Create volume
docker volume create myvolume
# Create with driver
docker volume create --driver local myvolumeDOCKER VOLUME LS — List Volumes
Lists all volumes.
# List volumes
docker volume ls
# Filter volumes
docker volume ls --filter dangling=trueDOCKER VOLUME INSPECT — Volume Details
Shows details about a volume.
docker volume inspect myvolumeDOCKER VOLUME RM — Delete Volume
Deletes a volume.
# Remove volume
docker volume rm myvolume
# Force remove
docker volume rm -f myvolume
# Remove all unused volumes
docker volume pruneUsing Volumes
# Run container with volume
docker run -d --name db -v dbdata:/var/lib/postgresql postgres
# Mount multiple volumes
docker run -d \
-v dbdata:/var/lib/postgresql \
-v config:/etc/postgresql \
postgresWhy Volumes Matter
Volumes are very important for databases because:
- Database files persist even if container is removed
- Data survives container restart
- Can share volumes between containers
- Better performance than bind mounts
Cleanup Commands
Managing Docker Storage
Docker can leave behind unused containers, images, networks, and volumes. Cleanup commands help recover space.
| Command | Purpose |
|---|---|
docker system prune | Remove unused resources |
docker container prune | Remove stopped containers |
docker image prune | Remove unused images |
docker volume prune | Remove unused volumes |
docker network prune | Remove unused networks |
DOCKER SYSTEM PRUNE — Full Cleanup
Removes unused Docker resources.
# Remove unused containers, images, networks
docker system prune
# Also remove unused volumes
docker system prune -a --volumes
# Force without confirmation
docker system prune -fDOCKER CONTAINER PRUNE — Clean Containers
Removes all stopped containers.
# Remove stopped containers
docker container prune
# Force without confirmation
docker container prune -f
# Filter before pruning
docker container prune --filter "until=24h"DOCKER IMAGE PRUNE — Clean Images
Removes unused images.
# Remove dangling images
docker image prune
# Remove all unused images
docker image prune -a
# Force without confirmation
docker image prune -fDOCKER VOLUME PRUNE — Clean Volumes
Removes unused volumes.
# Remove unused volumes
docker volume prune
# Force without confirmation
docker volume prune -fDOCKER NETWORK PRUNE — Clean Networks
Removes unused networks.
# Remove unused networks
docker network pruneCleanup Best Practices
# Check what will be deleted
docker system df
# Safe cleanup (not -a)
docker system prune
# Full cleanup (remove everything unused)
docker system prune -a
# When storage is critical
docker image prune -a -fWarning: Cleanup is permanent! Deleted data may not be recoverable.
Dockerfile Concepts
Building Images from Dockerfile
Many Docker commands work together with a Dockerfile. A Dockerfile is a text file that contains instructions for building an image.
Common Dockerfile Instructions
| Instruction | Purpose |
|---|---|
FROM | Base image |
RUN | Execute command during build |
COPY | Copy files from host |
WORKDIR | Set working directory |
EXPOSE | Document ports |
ENV | Set environment variables |
CMD | Default command |
Example Dockerfile
# Base image
FROM ubuntu:20.04
# Install dependencies
RUN apt-get update && apt-get install -y python3
# Set working directory
WORKDIR /app
# Copy application code
COPY . .
# Install Python dependencies
RUN pip install -r requirements.txt
# Expose port
EXPOSE 5000
# Set environment variable
ENV PYTHONUNBUFFERED=1
# Default command
CMD ["python3", "app.py"]Build and Run Workflow
# Build image from Dockerfile
docker build -t myapp .
# Run container from image
docker run -d -p 5000:5000 myapp
# Check logs
docker logs -f myapp
# Access container
docker exec -it myapp bashDocker Compose Commands
Managing Multi-Container Applications
Docker Compose is used when one application needs multiple containers, such as a web app and a database.
Docker Compose Commands
| Command | Purpose |
|---|---|
docker compose up | Create and start services |
docker compose down | Stop and remove services |
docker compose ps | List running services |
docker compose logs | Show service logs |
DOCKER COMPOSE UP — Start Services
Creates and starts the services defined in a Compose file.
# Start services
docker compose up
# Start in background
docker compose up -d
# Start specific service
docker compose up web
# Rebuild images
docker compose up --build
# Scale service
docker compose up --scale web=3DOCKER COMPOSE DOWN — Stop Services
Stops and removes the Compose services.
# Stop and remove services
docker compose down
# Also remove volumes
docker compose down -v
# Also remove images
docker compose down --rmi allDOCKER COMPOSE PS — List Services
Lists the running Compose services.
docker compose psDOCKER COMPOSE LOGS — Service Logs
Shows logs from Compose services.
# Show all logs
docker compose logs
# Follow logs
docker compose logs -f
# Show logs from specific service
docker compose logs -f webExample Docker Compose File
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
networks:
- myapp
db:
image: postgres:13
environment:
POSTGRES_DB: myapp
POSTGRES_PASSWORD: secret
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- myapp
volumes:
postgres_data:
networks:
myapp:
driver: bridgeUsing Docker Compose
# Start application stack
docker compose up -d
# View logs
docker compose logs -f
# List services
docker compose ps
# Stop everything
docker compose downSwarm and Service Commands
Distributed Container Orchestration
Docker also supports services in swarm mode for running distributed workloads.
Swarm Commands
| Command | Purpose |
|---|---|
docker swarm init | Initialize swarm |
docker swarm join | Join existing swarm |
docker node ls | List swarm nodes |
docker service create | Create service |
docker service ls | List services |
docker service scale | Scale service replicas |
DOCKER SWARM INIT — Initialize Swarm
Starts a swarm on the current machine.
# Initialize swarm
docker swarm init
# Initialize with custom address
docker swarm init --advertise-addr 192.168.1.100DOCKER SWARM JOIN — Join Swarm
Adds another node to the swarm.
# Join existing swarm
docker swarm join --token <token> <manager-ip>:2377DOCKER NODE LS — List Nodes
Lists nodes in the swarm.
docker node lsDOCKER SERVICE CREATE — Create Service
Creates a service.
# Create service
docker service create --name web nginx
# With replicas
docker service create --name web --replicas 3 nginx
# With port mapping
docker service create --name web -p 8080:80 nginx
# With environment variables
docker service create -e VAR=value --name app myappDOCKER SERVICE LS — List Services
Lists services.
docker service lsDOCKER SERVICE SCALE — Scale Service
Changes the number of service replicas.
# Scale to 5 replicas
docker service scale web=5
# Scale multiple services
docker service scale web=3 db=2DOCKER SERVICE LOGS — Service Logs
Shows logs for a service.
# Show service logs
docker service logs web
# Follow logs
docker service logs -f webPractical Workflow Example
Complete Docker Workflow
Here’s a practical workflow showing how commands work together:
# 1. Download an image
docker pull nginx
# 2. Run container in background
docker run -d -p 8080:80 --name web nginx
# 3. Check if it's running
docker ps
# 4. View container logs
docker logs web
# 5. Enter the container
docker exec -it web bash
# 6. Make some changes or inspect
ls -la /usr/share/nginx/html
# 7. Exit container (inside shell: exit)
# 8. Stop the container
docker stop web
# 9. Remove the container
docker rm web
# 10. Remove the image
docker rmi nginxMulti-Container Application Workflow
# 1. Create a network
docker network create myapp
# 2. Run database
docker run -d \
--name db \
--network myapp \
-e POSTGRES_PASSWORD=secret \
postgres
# 3. Run web app
docker run -d \
--name web \
--network myapp \
-p 8080:80 \
myapp:latest
# 4. Check status
docker ps
# 5. View logs
docker logs -f web
# 6. Cleanup
docker network disconnect myapp web
docker network disconnect myapp db
docker rm web db
docker network rm myappDocker Commands Quick Reference
Image Management
| Command | Purpose |
|---|---|
docker images | List images |
docker pull <image> | Download image |
docker push <image> | Upload image |
docker rmi <image> | Remove image |
docker build -t <name> . | Build from Dockerfile |
Container Management
| Command | Purpose |
|---|---|
docker run <image> | Create and run container |
docker ps | List running containers |
docker ps -a | List all containers |
docker start <id> | Start container |
docker stop <id> | Stop container |
docker rm <id> | Remove container |
Container Interaction
| Command | Purpose |
|---|---|
docker exec -it <id> bash | Open shell in container |
docker logs <id> | View logs |
docker logs -f <id> | Follow logs |
docker stats <id> | View resource usage |
docker cp | Copy files |
Networks and Volumes
| Command | Purpose |
|---|---|
docker network create <name> | Create network |
docker volume create <name> | Create volume |
docker network ls | List networks |
docker volume ls | List volumes |
Cleanup
| Command | Purpose |
|---|---|
docker system prune | Remove unused resources |
docker container prune | Remove stopped containers |
docker image prune | Remove unused images |
docker volume prune | Remove unused volumes |
Study Notes Summary
Docker commands can be grouped into a few main areas:
By Function
- Image Management —
build,pull,push,rmi - Container Management —
run,start,stop,restart,rm - Inspection —
logs,inspect,stats,ps - File Movement —
cp - Networking —
network create,connect,inspect - Storage —
volume create,inspect,prune - Cleanup —
system prune,image prune,container prune
By Object
Docker commands work on four main objects:
- Images — Templates for containers
- Containers — Running instances
- Networks — Container communication
- Volumes — Persistent storage
Key Takeaways
- Images are blueprints; containers are running instances
- Networks enable container-to-container communication
- Volumes provide persistent storage
- Compose simplifies multi-container applications
- Swarm enables distributed container orchestration
- Always clean up unused resources
Learning Path
- Master basic commands (
run,ps,logs,stop,rm) - Learn image building (
build,pull,push) - Understand networking and volumes
- Practice Docker Compose for multi-container apps
- Explore Swarm for advanced orchestration
Summary
Docker becomes much easier once you understand that most commands work on only four things: images, containers, networks, and volumes.
By mastering Docker commands, you can:
- Build and manage containerized applications
- Create reproducible environments
- Deploy consistent applications across systems
- Scale applications efficiently
- Collaborate effectively with teams
Practice these commands regularly to build confidence and efficiency in container management!
