DevDocsDev Docs
Docker CLI

Docker CLI Guide

Essential Docker command-line interface commands for container management

The Docker CLI is your primary interface for interacting with Docker. This guide covers essential commands for container and image management, debugging, and system administration.

Command Structure

Docker commands follow the pattern: docker [object] [command] [options] Example: docker container ls --all

Command Structure

Quick Navigation

Essential Commands Overview

# Run a container
docker run -d -p 8080:80 --name web nginx:alpine

# List containers
docker ps          # Running only
docker ps -a       # All containers

# Container lifecycle
docker start web
docker stop web
docker restart web
docker rm web

# Interact with container
docker exec -it web sh
docker logs -f web
docker inspect web
# Pull image
docker pull nginx:alpine

# Build image
docker build -t myapp:latest .

# List images
docker images

# Tag image
docker tag myapp:latest myregistry/myapp:v1.0

# Push image
docker push myregistry/myapp:v1.0

# Remove image
docker rmi nginx:alpine
# List networks
docker network ls

# Create network
docker network create mynetwork

# Connect container
docker network connect mynetwork container_name

# Disconnect container
docker network disconnect mynetwork container_name

# Inspect network
docker network inspect mynetwork

# Remove network
docker network rm mynetwork
# List volumes
docker volume ls

# Create volume
docker volume create mydata

# Inspect volume
docker volume inspect mydata

# Remove volume
docker volume rm mydata

# Prune unused volumes
docker volume prune

Common Workflows

Running a Web Server

# Run nginx with port mapping
docker run -d \
  --name webserver \
  -p 8080:80 \
  -v $(pwd)/html:/usr/share/nginx/html:ro \
  nginx:alpine

# Check it's running
docker ps

# View logs
docker logs webserver

# Stop and remove
docker stop webserver && docker rm webserver

Building and Running an Application

# Build image
docker build -t myapp:latest .

# Run container
docker run -d \
  --name myapp \
  -p 3000:3000 \
  -e NODE_ENV=production \
  myapp:latest

# Check logs
docker logs -f myapp

# Execute command inside
docker exec -it myapp sh

Development Workflow

# Build with no cache
docker build --no-cache -t myapp:dev .

# Run with volume mount for hot reload
docker run -d \
  --name myapp-dev \
  -p 3000:3000 \
  -v $(pwd):/app \
  -v /app/node_modules \
  myapp:dev npm run dev

# Follow logs
docker logs -f myapp-dev

# Rebuild and restart
docker stop myapp-dev && docker rm myapp-dev
docker build -t myapp:dev . && docker run -d --name myapp-dev ...

Command Quick Reference

Container Commands

CommandDescription
docker runCreate and start container
docker startStart stopped container
docker stopStop running container
docker restartRestart container
docker rmRemove container
docker execExecute command in container
docker logsView container logs
docker inspectDetailed container info
docker cpCopy files to/from container
docker statsContainer resource usage

Image Commands

CommandDescription
docker buildBuild image from Dockerfile
docker pullDownload image from registry
docker pushUpload image to registry
docker imagesList local images
docker rmiRemove image
docker tagTag image
docker historyShow image layers
docker saveExport image to tar
docker loadImport image from tar

System Commands

CommandDescription
docker system dfDisk usage
docker system pruneRemove unused data
docker infoSystem-wide information
docker versionDocker version info

Global Options

# Specify Docker host
docker -H tcp://host:2375 ps

# Debug mode
docker --debug ps

# Use different config
docker --config ~/.docker/alt ps

# Format output as JSON
docker ps --format json

Output Formatting

Use --format with Go templates for custom output.

# List container names and status
docker ps --format "table {{.Names}}\t{{.Status}}"

# JSON output
docker ps --format json

# Custom template
docker ps --format "{{.ID}}: {{.Image}} ({{.Status}})"

# Inspect specific field
docker inspect --format '{{.NetworkSettings.IPAddress}}' container_name

Common Format Fields

FieldDescription
.IDContainer ID
.NamesContainer name
.ImageImage name
.StatusContainer status
.PortsPort mappings
.SizeContainer size
.CreatedAtCreation time

Shell Completion

# Bash (add to ~/.bashrc)
source <(docker completion bash)

# Zsh (add to ~/.zshrc)
source <(docker completion zsh)

# Fish
docker completion fish | source

Aliases for Productivity

# Add to ~/.bashrc or ~/.zshrc
alias d='docker'
alias dc='docker compose'
alias dps='docker ps'
alias dpsa='docker ps -a'
alias dimg='docker images'
alias drm='docker rm'
alias drmi='docker rmi'
alias dexec='docker exec -it'
alias dlogs='docker logs -f'
alias dprune='docker system prune -af'

Next Steps

On this page