Recent
Published May 22, 2026 · 9 min read · 🏷️ DevOps

Docker Commands Every Developer Must Know

Docker has become essential for modern development. This guide covers the 30 most useful Docker commands with practical examples for building, running, and managing containers.

Getting Started

First, verify Docker is installed:

docker --version
docker compose version  # for docker-compose v2

1. Managing Images

Images are the blueprints for your containers. Pull an image from Docker Hub:

docker pull nginx:latest
docker pull node:20-alpine

List all downloaded images:

docker images
docker images -a  # show intermediate images

Remove unused images to free up disk space:

docker image prune -a      # remove all unused images
docker rmi nginx:latest     # remove specific image

2. Running Containers

Start a container from an image:

docker run nginx
docker run -d nginx        # run in detached mode (background)
docker run -p 8080:80 nginx # map port 8080 to container port 80

Run with a name and environment variables:

docker run -d --name my-app -p 3000:3000 \
  -e NODE_ENV=production \
  -v $(pwd)/data:/app/data \
  my-image:latest

3. Container Lifecycle

View running containers:

docker ps                # running containers only
docker ps -a              # all containers (including stopped)
docker ps --filter "status=exited"

Start, stop, and restart containers:

docker stop my-app
docker start my-app
docker restart my-app

Remove containers:

docker rm my-app          # remove stopped container
docker rm -f my-app        # force remove (even if running)

4. Viewing Logs and Debugging

Stream logs from a running container:

docker logs -f my-app
docker logs --tail 100 my-app  # last 100 lines
docker logs --since 1h my-app   # logs from last hour

Inspect container details:

docker inspect my-app
docker exec -it my-app sh      # open shell inside container

5. Docker Compose Commands

Docker Compose manages multi-container applications. Start all services:

docker compose up -d           # start in background
docker compose up --build       # rebuild before starting

Stop and remove all services:

docker compose down
docker compose down -v          # also remove volumes

View logs and status:

docker compose logs -f
docker compose ps

6. Building Images

Build a custom image from a Dockerfile:

docker build -t my-app:latest .
docker build -t my-app:latest --build-arg NODE_ENV=production .

Build with no cache (useful when you need a fresh build):

docker build --no-cache -t my-app:latest .

7. Cleaning Up

Docker can accumulate a lot of disk space over time. Clean up all unused resources:

docker system prune           # remove stopped containers, unused networks
docker system prune -a         # also remove all unused images
docker system df              # show disk usage

Common Workflows

Development Mode

docker compose up --build -d
docker compose logs -f app
docker compose exec app npm run dev

Production Deployment

docker build -t my-app:latest .
docker tag my-app:latest registry.com/my-app:v1.0.0
docker push registry.com/my-app:v1.0.0

Networking Basics

Docker containers need to communicate. Understanding networks is essential:

# List all networks
docker network ls

# Create a custom network
docker network create my-network

# Connect a container to a network
docker network connect my-network container-name

# Inspect a network
docker network inspect bridge

# Run container on specific network
docker run --network my-network nginx

Common network types: bridge (default), host (removes network isolation), overlay (Docker Swarm), none (no network).

Volume Management

Volumes persist data beyond container lifecycle:

# List volumes
docker volume ls

# Create a volume
docker volume create my-data

# Inspect a volume
docker volume inspect my-data

# Run with a volume
docker run -v my-data:/app/data nginx

# Run with bind mount (host directory)
docker run -v $(pwd)/data:/app/data nginx

# Run with read-only mount
docker run -v $(pwd)/config:/app/config:ro nginx

Image Building Deep Dive

A typical Dockerfile example:

FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Build with multi-stage builds to reduce final image size:

docker build -t my-app:latest --target builder .
docker build -t my-app:prod --target production .

Dockerfile Best Practices

  • Use specific versions — Not node:latest, use node:20-alpine
  • Order by change frequency — COPY package files first, then source code
  • Use .dockerignore — Exclude node_modules, .git, build artifacts
  • Minimize layers — Combine RUN commands where possible
  • Use multi-stage builds — Keep production images lean
  • Don't run as root — Use USER directive in Dockerfile

Resource Limits

Prevent containers from consuming all system resources:

# Limit CPU and memory
docker run -d --name web \
  --memory="512m" \
  --cpus="1.0" \
  nginx

# Set memory reservation (soft limit)
docker run -d --name web \
  --memory="512m" \
  --memory-reservation="256m" \
  nginx

# Limit CPU cores
docker run -d --cpuset-cpus="0,1" nginx

Health Checks

Add health checks to monitor container status:

# In Dockerfile
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
  CMD curl -f http://localhost/health || exit 1

# Run with custom health check
docker run -d \
  --health-cmd="curl -f http://localhost/health || exit 1" \
  --health-interval=30s \
  nginx

Troubleshooting

Container exits immediately: Check logs with docker logs container-name

Port already in use: Find the process: docker ps | grep 8080 or use lsof -i :8080

Permission denied: On Linux, you may need sudo docker ... or add your user to the docker group.

Image pull fails: Check Docker daemon is running: docker info

Disk space full: Run cleanup commands:

docker system prune -a
docker volume prune
docker builder prune

Useful One-Liners

# Kill all running containers
docker kill $(docker ps -q)

# Remove all stopped containers
docker container prune

# Remove all dangling images
docker image prune

# Remove all unused volumes
docker volume prune

# Show container disk usage
docker system df

# Watch container stats in real-time
docker stats

# Copy files from container
docker cp container:/app/logs ./logs

# Pause/unpause container
docker pause container-name
docker unpause container-name

Frequently Asked Questions

What's the difference between Docker and a virtual machine?

VMs run a full guest OS with a hypervisor — heavy (GBs, minutes to boot). Docker containers share the host kernel and only package the app + dependencies — light (MBs, seconds to boot). Containers aren't as isolated as VMs (shared kernel is a security boundary), but for most apps the trade-off is worth it.

How do I persist data in Docker?

Use volumes: docker run -v mydata:/var/lib/data mounts a named volume. Bind mounts (-v ./host/path:/container/path) link a host directory. Volumes outlive containers; bind mounts are great for development. Never store data inside the container's writable layer — it disappears when the container is removed.

How do I make a Docker image smaller?

Use alpine or distroless base images (5-50MB vs 200MB+ for Debian). Multi-stage builds — compile in one stage, copy only the artifact to a minimal runtime stage. Clean up apt cache in the same RUN: RUN apt-get update && apt-get install -y pkg && rm -rf /var/lib/apt/lists/*. Order layers from least-changed to most-changed for better cache hits.

← Back to Blog
Copied!