Przejdลบ do treล›ci

๐Ÿ“˜ 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

1
2
3
4
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"]