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 v21. Managing Images
Images are the blueprints for your containers. Pull an image from Docker Hub:
docker pull nginx:latest
docker pull node:20-alpineList all downloaded images:
docker images
docker images -a # show intermediate imagesRemove unused images to free up disk space:
docker image prune -a # remove all unused images
docker rmi nginx:latest # remove specific image2. 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 80Run 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:latest3. 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-appRemove 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 hourInspect container details:
docker inspect my-app
docker exec -it my-app sh # open shell inside container5. 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 startingStop and remove all services:
docker compose down
docker compose down -v # also remove volumesView logs and status:
docker compose logs -f
docker compose ps6. 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 usageCommon Workflows
Development Mode
docker compose up --build -d
docker compose logs -f app
docker compose exec app npm run devProduction 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.0Networking 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 nginxCommon 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 nginxImage 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, usenode: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" nginxHealth 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 \
nginxTroubleshooting
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 pruneUseful 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