What is Docker?
Docker is an open-source platform that automates the deployment, scaling, and management of applications using containerization.
Why Docker?
- Consistency: Works the same everywhere
- Isolation: Apps run in separate containers
- Portability: Run on any system with Docker
- Efficiency: Lightweight compared to VMs
Basic Commands
# Pull an image
docker pull nginx
# Run a container
docker run -d -p 8080:80 nginx
# List running containers
docker ps
# Stop a container
docker stop
Dockerfile Example
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Docker Compose
For multi-container applications:
version: "3.8"
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: secret