๐ Docker Examples
Practical examples and readyโtoโuse templates for Docker and Docker Compose.
๐๏ธ MultiโStage Build Example
1
2
3
4
5
6
7
8
9
10
11
12 | FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
|
๐ณ Minimal Compose Stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | services:
api:
image: myapp:latest
restart: unless-stopped
networks:
- backend
- traefik-net
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`api.example.com`)"
- "traefik.http.routers.api.entrypoints=websecure"
- "traefik.http.routers.api.tls.certresolver=le"
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
networks:
- backend
networks:
backend:
traefik-net:
external: true
|
๐ง Debugging Example
| docker compose logs -f api
docker inspect api
docker exec -it api sh
docker compose ps -a
|
๐ Secure Python App Example
1
2
3
4
5
6
7
8
9
10
11
12 | FROM python:3.12-alpine
RUN adduser -D appuser
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER appuser
EXPOSE 8000
CMD ["python", "main.py"]
|