123 lines
7.1 KiB
HTML
123 lines
7.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Bash 13: Docker & Container Lifecycle Scripting</title>
|
|
<link rel="stylesheet" href="../../../assets/style.css">
|
|
</head>
|
|
<body>
|
|
<div class="app-layout">
|
|
<div class="app-main">
|
|
<header>
|
|
<div class="header-container">
|
|
<div class="header-badge">💎 Lehrpfad 4: Cloud DevOps, BATS & Masterprojekt</div>
|
|
<h1>Bash 13: Docker & Container Lifecycle Scripting</h1>
|
|
<p>Automatisiere Container: Health-Polling, Volume-Backups über ephemere Hilfs-Container, Image-Pruning und Resource-Monitoring.</p>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container">
|
|
<!-- 1. THEORIE: CONTAINER HEALTH & POLLING -->
|
|
<section class="card">
|
|
<h2>🐳 1. Container Lifecycle & Health-Polling</h2>
|
|
<p>Bevor Integrationstests oder App-Deployments starten, muss sichergestellt sein, dass Container wie PostgreSQL oder Redis wirklich ansprechbar sind:</p>
|
|
|
|
<pre><code># 🎯 Status mit inspect abfragen:
|
|
status=$(docker inspect --format='{{.State.Health.Status}}' my-postgres)
|
|
|
|
# Mögliche Werte:
|
|
# - "starting" (Container bootet, Healthcheck läuft noch)
|
|
# - "healthy" (Healthcheck meldet Erfolg)
|
|
# - "unhealthy" (Dienst reagiert nicht korrekt)</code></pre>
|
|
</section>
|
|
|
|
<!-- 2. THEORIE: VOLUME BACKUP -->
|
|
<section class="card">
|
|
<h2>💾 2. Das Docker Volume-Backup Pattern</h2>
|
|
<p>Statt proprietäre Pfade in <code>/var/lib/docker/volumes</code> anzufassen, mounted ein schlanker Helper-Container (z.B. Alpine) das Volume und erstellt das Backup:</p>
|
|
|
|
<div style="background: var(--bg-subtle); padding: 16px; border-radius: var(--radius-sm); font-family: var(--font-mono); font-size: 0.85rem; line-height: 1.5; margin-bottom: 16px; border: 1px solid var(--border-color);">
|
|
[Host-System /backups] ◀──────┐<br>
|
|
│ (tar.gz)<br>
|
|
[Alpine Helper-Container] ─┴─> tar czf /backup/db.tar.gz -C /volume_data .<br>
|
|
▲<br>
|
|
│ (-v db_data:/volume_data)<br>
|
|
[Named Volume: db_data]
|
|
</div>
|
|
|
|
<div class="box box-analogie">
|
|
<div class="box-title">📦 Didaktische Analogie: Der mobile Umzugshelfer</div>
|
|
<p>Ein Volume ist wie ein abgeschlossener Lagerraum. Der ephemere Alpine-Container ist der Umzugshelfer, der kurz den Raum betritt, alles in Kartons packt (<code>tar czf</code>), die Kiste vor die Lagertür stellt und sich danach sofort wieder in Luft auflöst (<code>--rm</code>).</p>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 3. CHEAT-SHEET -->
|
|
<section class="card">
|
|
<h2>📑 3. Cheat-Sheet: Docker Automation</h2>
|
|
<div style="overflow-x: auto;">
|
|
<table style="width: 100%; border-collapse: collapse; font-size: 0.9rem; text-align: left;">
|
|
<thead>
|
|
<tr style="border-bottom: 2px solid var(--border-color); background: var(--bg-subtle);">
|
|
<th style="padding: 10px;">Befehl</th>
|
|
<th style="padding: 10px;">Optionen / Format</th>
|
|
<th style="padding: 10px;">Zweck</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr style="border-bottom: 1px solid var(--border-color);">
|
|
<td style="padding: 10px;"><code>docker inspect</code></td>
|
|
<td style="padding: 10px;"><code>--format='{{.State.Health.Status}}'</code></td>
|
|
<td style="padding: 10px;">Liest gezielt Metadaten & Health-Status aus</td>
|
|
</tr>
|
|
<tr style="border-bottom: 1px solid var(--border-color);">
|
|
<td style="padding: 10px;"><code>docker run --rm</code></td>
|
|
<td style="padding: 10px;"><code>-v vol:/data -v $(pwd):/backup</code></td>
|
|
<td style="padding: 10px;">Führt Einmal-Aufgaben aus & löscht Container danach</td>
|
|
</tr>
|
|
<tr style="border-bottom: 1px solid var(--border-color);">
|
|
<td style="padding: 10px;"><code>docker system prune</code></td>
|
|
<td style="padding: 10px;"><code>-f -a --filter "until=168h"</code></td>
|
|
<td style="padding: 10px;">Löscht ungenutzte Images, Layer & Container</td>
|
|
</tr>
|
|
<tr style="border-bottom: 1px solid var(--border-color);">
|
|
<td style="padding: 10px;"><code>docker stats</code></td>
|
|
<td style="padding: 10px;"><code>--no-stream --format json</code></td>
|
|
<td style="padding: 10px;">Gibt Momentaufnahme von CPU/RAM als JSON</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 4. AUFGABEN CHECKLISTE -->
|
|
<section class="card">
|
|
<h2>🎯 Aufgaben & Checkliste in <code>aufgabe.sh</code></h2>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t1">
|
|
<label class="task-label" for="t1"><strong>TODO 1:</strong> <code>poll_container_health(container_name, max_retries, sleep_sec)</code> implementieren.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t2">
|
|
<label class="task-label" for="t2"><strong>TODO 2:</strong> <code>build_volume_backup_command(volume, host_dir, archive)</code> mit Alpine generieren.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t3">
|
|
<label class="task-label" for="t3"><strong>TODO 3:</strong> <code>build_docker_prune_command(all_flag, until_hours)</code> konfigurieren.</label>
|
|
</div>
|
|
<div class="task-item">
|
|
<input type="checkbox" class="task-checkbox" id="t4">
|
|
<label class="task-label" for="t4"><strong>TODO 4:</strong> <code>parse_container_stats_json(json_str)</code> parsen.</label>
|
|
</div>
|
|
|
|
<div style="margin-top: 25px; display: flex; gap: 12px; flex-wrap: wrap;">
|
|
<a href="../../../workspace.html?course=bash&track=track_4_cloud_devops_und_master&chapter=13_docker_und_container_scripting" class="btn" style="background: #eab308; color: #000; font-weight: 700;">💻 In Web-IDE öffnen →</a>
|
|
<a href="../12_cron_systemd_und_service_automation/index.html" class="btn btn-secondary">← Zurück zu Bash 12</a>
|
|
<a href="../14_ci_cd_und_github_actions_shell/index.html" class="btn btn-secondary">Weiter zu Bash 14 →</a>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|