Essential Docker Commands 🚀
Master these commands and you'll be containerizing like a pro. Click any command to see its details.
docker build
Click to expanddocker build -t my-app . Build an image from a Dockerfile
docker run
Click to expanddocker run -p 3000:3000 my-app Run a container from an image
docker ps
Click to expanddocker ps -a List containers
docker stop
Click to expanddocker stop container-name Gracefully stop a container
docker images
Click to expanddocker images List all images on your system
docker exec
Click to expanddocker exec -it container bash Run commands inside a running container
Dockerfile Best Practices 📋
Write better Dockerfiles with these proven patterns and optimizations.
What NOT to Do
# Bad Dockerfile
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y nodejs
RUN apt-get install -y npm
RUN apt-get install -y python3
RUN apt-get install -y git
COPY . /app
WORKDIR /app
RUN npm install
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"] Uses latest tag (unpredictable)
Multiple RUN commands create extra layers
Copies files before installing dependencies
Installs unnecessary packages
Best Practice
# Good Dockerfile
FROM node:18-alpine
WORKDIR /app
# Copy package files first
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production && \
npm cache clean --force
# Copy source code
COPY . .
# Build application
RUN npm run build
# Use non-root user
USER node
EXPOSE 3000
CMD ["npm", "start"] Specific, lightweight base image
Optimized for Docker layer caching
Copies dependencies first for better caching
Runs as non-root user for security
Layer Optimization
Combine RUN commands with && to reduce layers. Each instruction creates a new layer.
Multi-stage Builds
Use multiple FROM statements to create smaller production images. Copy only what you need.
Security First
Never run as root user. Use official images. Keep images updated and scan for vulnerabilities.
Docker Compose: Multi-Container Magic 🎭
When you need multiple containers working together, Docker Compose is your best friend. Define your entire application stack in one file.
docker-compose.yml
YAMLversion: '3.8'
services:
# Frontend React app
web:
build: ./frontend
ports:
- "3000:3000"
depends_on:
- api
environment:
- REACT_APP_API_URL=http://localhost:4000
# Backend API
api:
build: ./backend
ports:
- "4000:4000"
depends_on:
- db
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/myapp
- JWT_SECRET=your-secret-key
# PostgreSQL Database
db:
image: postgres:15
environment:
- POSTGRES_DB=myapp
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
volumes:
- postgres_data:/var/lib/postgresql/data
# Redis Cache
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
postgres_data: Essential Commands
docker-compose up Start all services
docker-compose up -d Start in background
docker-compose down Stop and remove containers
Development Commands
docker-compose build Rebuild images
docker-compose logs View all service logs
docker-compose ps Show running services
Why Use Docker Compose?
One Command Deploy
Start your entire application stack with a single command
Network Isolation
Services can communicate securely in their own network
Environment Parity
Development environment matches production exactly
Easy Scaling
Scale individual services up or down as needed
Production Deployment Tips 🏭
Taking Docker to production? Here are the essential practices for reliable, secure deployments.
Health Checks
HEALTHCHECK --interval=30s \
CMD curl -f http://localhost/health Add health checks to ensure your containers are actually working, not just running.
Resource Limits
docker run --memory=512m \
--cpus=1.0 my-app Always set memory and CPU limits to prevent containers from consuming all resources.
Centralized Logging
--log-driver=json-file \
--log-opt max-size=10m Configure proper logging drivers and rotation to prevent disk space issues.
Security Scanning
docker scan my-image:latest Regularly scan images for vulnerabilities and keep base images updated.
Orchestration
Kubernetes, Docker Swarm,
or managed services Use orchestration platforms for automatic scaling, rolling updates, and high availability.
Monitoring
Prometheus + Grafana
or DataDog, New Relic Set up comprehensive monitoring for container metrics, logs, and application performance.
Ready to Get Hands-On?
You've got the knowledge - now let's put it into practice!