Docker Basics
What is Docker?
Section titled “What is Docker?”Docker is a platform for packaging applications and dependencies into containers - isolated, lightweight, and portable environments.
Key Concepts
Section titled “Key Concepts”| Concept | Description |
|---|---|
| Image | Blueprint for creating a container |
| Container | Running instance of an image |
| Dockerfile | File defining how to build an image |
| Volume | Persistent storage for containers |
| Network | Connectivity between containers |
Basic Commands
Section titled “Basic Commands”# Pull imagedocker pull node:20-alpine
# Run containerdocker run -d -p 3000:3000 --name myapp node:20-alpine
# List containersdocker ps
# Stop & removedocker stop myapp && docker rm myappDockerfile Example
Section titled “Dockerfile Example”FROM node:20-alpineWORKDIR /appCOPY package*.json ./RUN npm ci --only=productionCOPY . .EXPOSE 3000CMD ["node", "server.js"]Docker Compose
Section titled “Docker Compose”services: web: build: . ports: - "3000:3000" depends_on: - db db: image: postgres:16-alpine environment: POSTGRES_PASSWORD: secret volumes: - pgdata:/var/lib/postgresql/data
volumes: pgdata:- Use multi-stage builds to reduce image size
- Always use
.dockerignoreto exclude unnecessary files - Prefer alpine images for production