248 lines
5.0 KiB
Markdown
248 lines
5.0 KiB
Markdown
|
|
Hier ist ein kompaktes Docker Compose Cheat Sheet, das du direkt in der Shell bzw. in deiner Doku nutzen kannst. [devhints](https://devhints.io/docker-compose)
|
|||
|
|
|
|||
|
|
***
|
|||
|
|
|
|||
|
|
## Grundstruktur docker-compose.yml
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
version: "3.8"
|
|||
|
|
|
|||
|
|
services:
|
|||
|
|
web:
|
|||
|
|
image: nginx:alpine
|
|||
|
|
ports:
|
|||
|
|
- "8080:80"
|
|||
|
|
volumes:
|
|||
|
|
- ./html:/usr/share/nginx/html:ro
|
|||
|
|
environment:
|
|||
|
|
- NGINX_HOST=localhost
|
|||
|
|
depends_on:
|
|||
|
|
- redis
|
|||
|
|
|
|||
|
|
redis:
|
|||
|
|
image: redis:7-alpine
|
|||
|
|
restart: unless-stopped
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Wichtige Felder: [dev](https://dev.to/maiobarbero/docker-compose-cheat-sheet-2kgi)
|
|||
|
|
|
|||
|
|
- services: Definition der Container.
|
|||
|
|
- image: Fertiges Image verwenden.
|
|||
|
|
- build: Image per Dockerfile bauen.
|
|||
|
|
- ports: Port-Mapping host:container.
|
|||
|
|
- volumes: Volumes oder Bind-Mounts.
|
|||
|
|
- environment / env_file: Umgebungsvariablen.
|
|||
|
|
- depends_on: Startreihenfolge / Health-Abhängigkeiten.
|
|||
|
|
- restart: Neustart-Policy (no, always, on-failure, unless-stopped).
|
|||
|
|
|
|||
|
|
***
|
|||
|
|
|
|||
|
|
## Wichtige CLI‑Kommandos
|
|||
|
|
|
|||
|
|
Neuere Docker Versionen verwenden `docker compose` statt `docker-compose` (beides meist verfügbar). [blog.devops](https://blog.devops.dev/docker-compose-cheat-sheet-b47495fe3762)
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# Projekt starten (Foreground)
|
|||
|
|
docker compose up
|
|||
|
|
|
|||
|
|
# Projekt starten (Detached)
|
|||
|
|
docker compose up -d
|
|||
|
|
|
|||
|
|
# Neu bauen und starten
|
|||
|
|
docker compose up --build -d
|
|||
|
|
|
|||
|
|
# Alles stoppen und Ressourcen entfernen
|
|||
|
|
docker compose down
|
|||
|
|
|
|||
|
|
# Nur Container stoppen (aber nicht löschen)
|
|||
|
|
docker compose stop
|
|||
|
|
|
|||
|
|
# Gestoppte Container wieder starten
|
|||
|
|
docker compose start
|
|||
|
|
|
|||
|
|
# Logs aller Services
|
|||
|
|
docker compose logs
|
|||
|
|
|
|||
|
|
# Live-Logs, nur bestimmter Service
|
|||
|
|
docker compose logs -f web
|
|||
|
|
|
|||
|
|
# Aktuelle Container im Projekt
|
|||
|
|
docker compose ps
|
|||
|
|
|
|||
|
|
# Neu bauen ohne zu starten
|
|||
|
|
docker compose build
|
|||
|
|
|
|||
|
|
# Shell in laufendem Container
|
|||
|
|
docker compose exec web /bin/sh
|
|||
|
|
# oder
|
|||
|
|
docker compose exec web bash
|
|||
|
|
|
|||
|
|
# Einmaligen Befehl in neuem Container ausführen
|
|||
|
|
docker compose run --rm web echo "test"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
***
|
|||
|
|
|
|||
|
|
## Typische Compose-Optionen (services)
|
|||
|
|
|
|||
|
|
### Image bauen oder verwenden
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
services:
|
|||
|
|
app:
|
|||
|
|
# Aus Dockerfile im aktuellen Verzeichnis
|
|||
|
|
build: .
|
|||
|
|
# oder detaillierter:
|
|||
|
|
build:
|
|||
|
|
context: ./app
|
|||
|
|
dockerfile: Dockerfile.dev
|
|||
|
|
target: dev
|
|||
|
|
|
|||
|
|
worker:
|
|||
|
|
# Fertiges Image
|
|||
|
|
image: ubuntu:22.04
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
### Ports
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
services:
|
|||
|
|
web:
|
|||
|
|
ports:
|
|||
|
|
- "8080:80" # host:container
|
|||
|
|
expose:
|
|||
|
|
- "3000" # nur für andere Services, nicht nach außen
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
### Volumes / Bind‑Mounts
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
services:
|
|||
|
|
db:
|
|||
|
|
image: postgres:16
|
|||
|
|
volumes:
|
|||
|
|
- db_data:/var/lib/postgresql/data # Named Volume
|
|||
|
|
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro # Bind-Mount
|
|||
|
|
|
|||
|
|
volumes:
|
|||
|
|
db_data:
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
### Environment / env_file
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
services:
|
|||
|
|
app:
|
|||
|
|
environment:
|
|||
|
|
APP_ENV: production
|
|||
|
|
DEBUG: "false"
|
|||
|
|
# oder alternative Schreibweise
|
|||
|
|
# environment:
|
|||
|
|
# - APP_ENV=production
|
|||
|
|
# - DEBUG=false
|
|||
|
|
env_file:
|
|||
|
|
- .env
|
|||
|
|
- app.env
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
### depends_on inkl. Health
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
services:
|
|||
|
|
db:
|
|||
|
|
image: postgres:16
|
|||
|
|
healthcheck:
|
|||
|
|
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
|||
|
|
interval: 10s
|
|||
|
|
timeout: 5s
|
|||
|
|
retries: 5
|
|||
|
|
start_period: 20s
|
|||
|
|
|
|||
|
|
app:
|
|||
|
|
image: my/app
|
|||
|
|
depends_on:
|
|||
|
|
db:
|
|||
|
|
condition: service_healthy
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
### restart‑Policy
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
services:
|
|||
|
|
worker:
|
|||
|
|
image: my/worker
|
|||
|
|
restart: unless-stopped # oder: "no", "always", "on-failure"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
### command / entrypoint
|
|||
|
|
|
|||
|
|
```yaml
|
|||
|
|
services:
|
|||
|
|
app:
|
|||
|
|
image: my/app
|
|||
|
|
command: ["bundle", "exec", "thin", "-p", "3000"]
|
|||
|
|
# oder
|
|||
|
|
# command: bundle exec thin -p 3000
|
|||
|
|
entrypoint: ["/app/start.sh"]
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
***
|
|||
|
|
|
|||
|
|
## Nützliche Projekt‑Kommandos
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# Konfiguration prüfen (inkl. Merge von mehreren Files)
|
|||
|
|
docker compose config
|
|||
|
|
|
|||
|
|
# Nur bestimmte Services starten
|
|||
|
|
docker compose up -d web db
|
|||
|
|
|
|||
|
|
# Nur einen Service neu bauen
|
|||
|
|
docker compose build web
|
|||
|
|
|
|||
|
|
# Volumes beim down mit löschen
|
|||
|
|
docker compose down -v
|
|||
|
|
|
|||
|
|
# Nur Images, die von compose gebaut wurden, löschen
|
|||
|
|
docker compose down --rmi local
|
|||
|
|
|
|||
|
|
# Service skalieren (V2 Syntax)
|
|||
|
|
docker compose up -d --scale worker=3
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
***
|
|||
|
|
|
|||
|
|
## Kurzer Überblick als Tabelle
|
|||
|
|
|
|||
|
|
| Bereich | Beispiel / Zweck |
|
|||
|
|
|---------------|-------------------------------------------|
|
|||
|
|
| Start | `docker compose up -d` |
|
|||
|
|
| Stop | `docker compose down` |
|
|||
|
|
| Logs | `docker compose logs -f web` |
|
|||
|
|
| Shell | `docker compose exec web /bin/sh` |
|
|||
|
|
| Build | `docker compose build` |
|
|||
|
|
| Konfig check | `docker compose config` |
|
|||
|
|
| Ports | `ports: ["8080:80"]` |
|
|||
|
|
| Volumes | `volumes: ["./code:/app"]` |
|
|||
|
|
| Env | `environment: { APP_ENV: prod }` |
|
|||
|
|
| Abhängigkeit | `depends_on: ["db"]` |
|
|||
|
|
|
|||
|
|
[devhints](https://devhints.io/docker-compose)
|
|||
|
|
|
|||
|
|
Wenn du willst, kann ich dir daraus eine druckbare A4‑Cheat‑Sheet‑Version im Markdown- oder PDF‑Stil bauen, zugeschnitten auf deine typischen Stacks (z.B. PostgreSQL + Redis + Traefik).
|