|
| 1 | +import htm from "htm"; |
| 2 | +import { createElement } from "react"; |
| 3 | +import { CodeSlide, DemoSlide, Lesson, QuestionsSlide } from "../Layouts/index.js"; |
| 4 | + |
| 5 | +const html = htm.bind(createElement); |
| 6 | + |
| 7 | +function Supplemental3() { |
| 8 | + return html` |
| 9 | + <${Lesson} title="Containerization with Docker" lessonId="supplemental_3" subtitle="Supplemental 3"> |
| 10 | + <section> |
| 11 | + <p>Running software <em>consistently</em> across <em>different machines</em> is hard.</p> |
| 12 | + </section> |
| 13 | + <section class="ml-bullet-slide"> |
| 14 | + <h3>Common problems containers solve</h3> |
| 15 | + <ul> |
| 16 | + <li class="fragment">“Works on my machine” environment drift</li> |
| 17 | + <li class="fragment">Dependency and version conflicts</li> |
| 18 | + <li class="fragment">Manual, error-prone setup steps</li> |
| 19 | + <li class="fragment">Scaling and isolation of services</li> |
| 20 | + </ul> |
| 21 | + </section> |
| 22 | + <section> |
| 23 | + <p><em>Containerization</em> packages your <em>app and its dependencies</em> into a portable, isolated unit.</p> |
| 24 | + </section> |
| 25 | + <section> |
| 26 | + <p>Containers <em>share the host OS kernel</em> and start fast (unlike heavy virtual machines).</p> |
| 27 | + </section> |
| 28 | + <section> |
| 29 | + <p><em>Docker</em> is the most popular tool for building, running, and sharing containers.</p> |
| 30 | + </section> |
| 31 | + <section> |
| 32 | + <p><em>Kubernetes</em> orchestrates containers across many machines for <em>reliability</em> and <em>scale</em>.</p> |
| 33 | + </section> |
| 34 | + <section> |
| 35 | + <p><em>Netflix</em>, <em>Airbnb</em>, <em>Shopify</em>, <em>Google</em>, <em>Reddit</em> (and many more) use containers in production.</p> |
| 36 | + </section> |
| 37 | + <section> |
| 38 | + <p>Getting started is simple: <em>install Docker Desktop</em> and <em>run a container</em>.</p> |
| 39 | + </section> |
| 40 | + <${CodeSlide} lang="bash" badge="Terminal" fontSize=".42em" lineNumbers=true> |
| 41 | +# Verify installation |
| 42 | +docker --version |
| 43 | +
|
| 44 | +# Pull and run a test image |
| 45 | +docker run --rm hello-world |
| 46 | + <//> |
| 47 | + <section> |
| 48 | + <p>Let’s containerize a simple <em>Node.js</em> web app.</p> |
| 49 | + </section> |
| 50 | + <${CodeSlide} lang="javascript" badge="app.js" fontSize=".42em" lineNumbers=true> |
| 51 | +// Minimal HTTP server (no external deps) |
| 52 | +import http from 'http'; |
| 53 | +
|
| 54 | +const server = http.createServer((req, res) => { |
| 55 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 56 | + res.end(JSON.stringify({ ok: true, time: new Date().toISOString() })); |
| 57 | +}); |
| 58 | +
|
| 59 | +server.listen(3000, () => console.log('Listening on http://localhost:3000')); |
| 60 | + <//> |
| 61 | + <${CodeSlide} lang="dockerfile" badge="Dockerfile" fontSize=".38em" lineNumbers=true> |
| 62 | +# Use a small official Node image |
| 63 | +FROM node:20-alpine |
| 64 | +
|
| 65 | +# Create app directory |
| 66 | +WORKDIR /usr/src/app |
| 67 | +
|
| 68 | +# Copy source |
| 69 | +COPY app.js ./ |
| 70 | +
|
| 71 | +# Expose port and run |
| 72 | +EXPOSE 3000 |
| 73 | +CMD ["node", "app.js"] |
| 74 | + <//> |
| 75 | + <${CodeSlide} lang="bash" badge="Terminal" fontSize=".42em" lineNumbers=true> |
| 76 | +# Build image (tag as demo:latest) |
| 77 | +docker build -t demo:latest . |
| 78 | +
|
| 79 | +# Run container and map port 3000 |
| 80 | +docker run --rm -p 3000:3000 demo:latest |
| 81 | + <//> |
| 82 | + <section> |
| 83 | + <p>Use <em>Docker Compose</em> to define <em>multi-service environments</em> in <em>one file</em>.</p> |
| 84 | + </section> |
| 85 | + <${CodeSlide} lang="yaml" badge="docker-compose.yml" fontSize=".34em"> |
| 86 | +services: |
| 87 | + web: |
| 88 | + build: . |
| 89 | + ports: |
| 90 | + - "3000:3000" |
| 91 | + depends_on: |
| 92 | + - db |
| 93 | + db: |
| 94 | + image: postgres:16-alpine |
| 95 | + environment: |
| 96 | + POSTGRES_USER: app |
| 97 | + POSTGRES_PASSWORD: secret |
| 98 | + POSTGRES_DB: appdb |
| 99 | + volumes: |
| 100 | + - db_data:/var/lib/postgresql/data |
| 101 | +volumes: |
| 102 | + db_data: |
| 103 | + <//> |
| 104 | + <${CodeSlide} lang="bash" badge="Compose" fontSize=".42em" lineNumbers=true> |
| 105 | +# Start all services |
| 106 | +docker compose up --build |
| 107 | +
|
| 108 | +# Stop and remove containers |
| 109 | +docker compose down |
| 110 | + <//> |
| 111 | + <section> |
| 112 | + <p>With Docker Compose, teams share reproducible dev and test environments as code.</p> |
| 113 | + </section> |
| 114 | + <${DemoSlide} /> |
| 115 | + <${QuestionsSlide} /> |
| 116 | + <//>`; |
| 117 | +} |
| 118 | + |
| 119 | +export default Supplemental3; |
0 commit comments