Docker has revolutionized how we package and deploy applications. As a DevOps engineer, understanding containerization is essential for managing infrastructure at scale.
What is Docker?
Docker is a containerization platform that allows you to package your application and all its dependencies into a lightweight, portable container. Instead of shipping your application with a full operating system, Docker containers share the host OS kernel while maintaining isolation.
Key Benefits
- Consistency - Works the same on your laptop, CI/CD pipeline, and production servers
- Scalability - Deploy hundreds of containers across clusters effortlessly
- Isolation - Prevents dependency conflicts between different applications
- Speed - Containers start in milliseconds instead of minutes
Getting Started with Docker
Your First Dockerfile
A Dockerfile is a blueprint for creating Docker images. Here’s a practical example:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Breaking it down:
FROM- Base image to start fromWORKDIR- Working directory inside containerCOPY- Copy files from host to containerRUN- Execute commands during buildEXPOSE- Document which ports the app usesCMD- Default command when container starts
Build and Run
# Build the image
docker build -t my-app:1.0 .
# Run the container
docker run -p 3000:3000 my-app:1.0
# Run in background (detached mode)
docker run -d -p 3000:3000 my-app:1.0
Docker Compose for Multi-Container Apps
When you need multiple services working together (web app + database + cache), Docker Compose is your friend:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
environment:
DATABASE_URL: postgres://user:password@db:5432/myapp
db:
image: postgres:15-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: myapp
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Start the entire stack with a single command:
docker-compose up -d
Best Practices
- Use specific base image versions - Avoid
latesttags which can break unexpectedly - Keep images small - Use Alpine variants of images (they’re 10x smaller)
- Multi-stage builds - Build in one stage, copy artifacts to final stage
- Don’t run as root - Create a non-root user in your container
- Security scanning - Use
docker scanto find vulnerabilities
Next Steps
- Explore Docker Hub for pre-built images
- Learn Docker networking for container communication
- Master Docker volumes for persistent data storage
- Move to Kubernetes for orchestration at scale
Docker is foundational for modern DevOps. Start with simple applications, build containerized solutions, and scale with confidence.
Want to learn more? Check out the Docker documentation.