3074 words
15 minutes
Docker Commands

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.

CommandPurpose
docker --helpShow main Docker help menu
docker infoDisplay Docker system information
docker versionShow Docker client and server version
docker loginLogin to Docker Hub or registry
docker logoutSign 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 logout

When 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#

CommandPurpose
docker imagesList all local images
docker pull <image>Download image from registry
docker push <image>Upload image to registry
docker rmi <image>Remove an image
docker buildBuild 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 nginx

DOCKER 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:latest

DOCKER 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 myapp

Container 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.

OptionPurpose
--nameGive container a custom name
-dRun in detached mode (background)
-pMap ports
-vMount volumes
-eSet environment variables
--rmAuto-remove after exit
-itInteractive 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 nginx

Port 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 nginx

Volumes 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 myapp

Environment 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 myapp

Auto-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 /data

Interactive Mode#

# Interactive terminal
docker run -it ubuntu bash

# Run command and keep attached
docker run -it --name myapp myapp:latest

Comprehensive Example#

docker run -d \
  --name web \
  -p 8080:80 \
  -v /data:/app/data \
  -e ENVIRONMENT=production \
  --rm \
  nginx:latest

Container Management Commands#

Controlling Containers#

Once containers are created, these commands help you control them.

CommandPurpose
docker psShow running containers
docker ps -aShow all containers
docker startStart stopped container
docker stopStop running container
docker restartRestart container
docker rmRemove container
docker killForce 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_name

DOCKER 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 prune

DOCKER 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 web

Container Access Commands#

Entering Running Containers#

These commands let you enter or interact with a running container.

CommandPurpose
docker execRun command inside container
docker attachConnect 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 pwd

DOCKER 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.txt

Logs and Inspection#

Understanding Container Behavior#

These commands help you understand what a container is doing and why it may have failed.

CommandPurpose
docker logsShow container output
docker inspectShow detailed information
docker statsShow resource usage
docker topShow 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_name

DOCKER 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_name

DOCKER 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-stream

DOCKER TOP — View Processes#

Shows the processes running inside a container.

# Show processes
docker top container_name

# Show with detailed info
docker top container_name -ef

Debugging 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 myapp

Copy Commands#

File Transfer Between Host and Container#

Docker can copy files between the host machine and containers.

CommandPurpose
docker cp container:path host:pathCopy FROM container
docker cp host:path container:pathCopy 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:/backup

Practical 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.

CommandPurpose
docker network createCreate new network
docker network lsList networks
docker network inspectShow network details
docker network connectAdd container to network
docker network disconnectRemove 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 mynetwork

DOCKER NETWORK LS — List Networks#

Lists Docker networks.

# List all networks
docker network ls

# Filter networks
docker network ls --filter driver=bridge

DOCKER NETWORK INSPECT — Network Details#

Shows detailed information about a network.

# Inspect network
docker network inspect mynetwork

# See connected containers
docker network inspect mynetwork

DOCKER 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_name

Multi-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 → db

Volume Commands#

Persistent Storage#

Volumes are used for persistent storage. They keep data even if the container is removed.

CommandPurpose
docker volume createCreate new volume
docker volume lsList volumes
docker volume inspectShow volume details
docker volume rmDelete volume

DOCKER VOLUME CREATE — Create Volume#

Creates a new volume.

# Create volume
docker volume create myvolume

# Create with driver
docker volume create --driver local myvolume

DOCKER VOLUME LS — List Volumes#

Lists all volumes.

# List volumes
docker volume ls

# Filter volumes
docker volume ls --filter dangling=true

DOCKER VOLUME INSPECT — Volume Details#

Shows details about a volume.

docker volume inspect myvolume

DOCKER 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 prune

Using 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 \
  postgres

Why 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.

CommandPurpose
docker system pruneRemove unused resources
docker container pruneRemove stopped containers
docker image pruneRemove unused images
docker volume pruneRemove unused volumes
docker network pruneRemove 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 -f

DOCKER 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 -f

DOCKER VOLUME PRUNE — Clean Volumes#

Removes unused volumes.

# Remove unused volumes
docker volume prune

# Force without confirmation
docker volume prune -f

DOCKER NETWORK PRUNE — Clean Networks#

Removes unused networks.

# Remove unused networks
docker network prune

Cleanup 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 -f

Warning: 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#

InstructionPurpose
FROMBase image
RUNExecute command during build
COPYCopy files from host
WORKDIRSet working directory
EXPOSEDocument ports
ENVSet environment variables
CMDDefault 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 bash

Docker 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#

CommandPurpose
docker compose upCreate and start services
docker compose downStop and remove services
docker compose psList running services
docker compose logsShow 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=3

DOCKER 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 all

DOCKER COMPOSE PS — List Services#

Lists the running Compose services.

docker compose ps

DOCKER 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 web

Example 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: bridge

Using Docker Compose#

# Start application stack
docker compose up -d

# View logs
docker compose logs -f

# List services
docker compose ps

# Stop everything
docker compose down

Swarm and Service Commands#

Distributed Container Orchestration#

Docker also supports services in swarm mode for running distributed workloads.

Swarm Commands#

CommandPurpose
docker swarm initInitialize swarm
docker swarm joinJoin existing swarm
docker node lsList swarm nodes
docker service createCreate service
docker service lsList services
docker service scaleScale 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.100

DOCKER SWARM JOIN — Join Swarm#

Adds another node to the swarm.

# Join existing swarm
docker swarm join --token <token> <manager-ip>:2377

DOCKER NODE LS — List Nodes#

Lists nodes in the swarm.

docker node ls

DOCKER 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 myapp

DOCKER SERVICE LS — List Services#

Lists services.

docker service ls

DOCKER 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=2

DOCKER SERVICE LOGS — Service Logs#

Shows logs for a service.

# Show service logs
docker service logs web

# Follow logs
docker service logs -f web

Practical 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 nginx

Multi-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 myapp

Docker Commands Quick Reference#

Image Management#

CommandPurpose
docker imagesList 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#

CommandPurpose
docker run <image>Create and run container
docker psList running containers
docker ps -aList all containers
docker start <id>Start container
docker stop <id>Stop container
docker rm <id>Remove container

Container Interaction#

CommandPurpose
docker exec -it <id> bashOpen shell in container
docker logs <id>View logs
docker logs -f <id>Follow logs
docker stats <id>View resource usage
docker cpCopy files

Networks and Volumes#

CommandPurpose
docker network create <name>Create network
docker volume create <name>Create volume
docker network lsList networks
docker volume lsList volumes

Cleanup#

CommandPurpose
docker system pruneRemove unused resources
docker container pruneRemove stopped containers
docker image pruneRemove unused images
docker volume pruneRemove unused volumes

Study Notes Summary#

Docker commands can be grouped into a few main areas:

By Function#

  • Image Managementbuild, pull, push, rmi
  • Container Managementrun, start, stop, restart, rm
  • Inspectionlogs, inspect, stats, ps
  • File Movementcp
  • Networkingnetwork create, connect, inspect
  • Storagevolume create, inspect, prune
  • Cleanupsystem prune, image prune, container prune

By Object#

Docker commands work on four main objects:

  1. Images — Templates for containers
  2. Containers — Running instances
  3. Networks — Container communication
  4. 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#

  1. Master basic commands (run, ps, logs, stop, rm)
  2. Learn image building (build, pull, push)
  3. Understand networking and volumes
  4. Practice Docker Compose for multi-container apps
  5. 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!

Docker Commands
https://ryo11blog.netlify.app/posts/docker_commands/
Author
Ranjung Yeshi Norbu
Published at
2026-03-06