← Back to Blog
Getting Started with Docker: A Practical Guide
dockerdevopstutorial
Docker has revolutionized how we develop, ship, and run applications. If you're new to containerization, this guide will help you get started.
What is Docker?
Docker is a platform that uses containerization to package applications and their dependencies together. This ensures that your application runs consistently across different environments.
Key Concepts
- Image: A blueprint for creating containers
- Container: A running instance of an image
- Dockerfile: Instructions for building an image
- Registry: A repository for storing images (like Docker Hub)
Your First Dockerfile
Here's a simple Dockerfile for a Node.js application:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Let's break this down:
FROM node:18-alpine- Use Node.js 18 on Alpine Linux (lightweight)WORKDIR /app- Set the working directoryCOPY package*.json ./- Copy package filesRUN npm install- Install dependenciesCOPY . .- Copy application codeEXPOSE 3000- Document which port the app usesCMD ["npm", "start"]- Command to run the application
Building and Running
Build your image:
docker build -t my-app .
Run a container:
docker run -p 3000:3000 my-app
Docker Compose for Multi-Container Apps
For applications with multiple services, use Docker Compose:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
environment:
DATABASE_URL: postgresql://db:5432/myapp
db:
image: postgres:15
environment:
POSTGRES_DB: myapp
POSTGRES_PASSWORD: secret
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
Run with:
docker-compose up
Best Practices
- Use official base images - They're maintained and secure
- Minimize layers - Combine RUN commands when possible
- Use .dockerignore - Exclude unnecessary files
- Don't run as root - Create a non-root user
- Use multi-stage builds - Keep production images small
Example multi-stage build:
# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm ci --only=production
CMD ["node", "dist/index.js"]
Conclusion
Docker simplifies deployment and ensures consistency across environments. Start with simple containers and gradually explore more advanced features like orchestration with Kubernetes.
Happy containerizing!