-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides.typ
667 lines (461 loc) · 14 KB
/
slides.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#import "@preview/touying:0.6.1": *
#import themes.simple: *
#show: simple-theme.with(
aspect-ratio: "16-9",
header: text(fill: gray, size: 14pt)[An Introduction to Docker],
footer: link(
"https://osdg.iiit.ac.in",
image("./assets/osdg.png", width: 1.5cm),
),
footer-right: text(
fill: gray,
size: 16pt,
)[#link("https://github.com/arnu515")[Aarnav Pai]],
)
= An Introduction to Docker
#align(
top + center,
text(
fill: gray,
size: 12pt,
[containers containing contained contrived concepts contain containers],
),
)
#align(bottom)[
#link(
"https://osdg.iiit.ac.in",
image("./assets/osdg.png", width: 2.5cm),
)
#align(
center,
text(
fill: gray,
size: 16pt,
)[#link("https://github.com/arnu515")[Aarnav Pai]],
)
]
== Let's deploy an application!
#text(
size: 24pt,
link("https://github.com/OSDG-IIITH/intro-to-docker/tree/main/py-example"),
)
#pause
#align(center, image("./assets/instabad.png", height: 8.5cm))
#pagebreak()
Its dependencies:
- Python `3.13`
#pause
- `requirements.txt` -- FastAPI, `asyncpg`, `jinja2`, etc.
#pause
- Postgres 17
#pause
- #strike("Redis") Valkey 8
#pause
I've taken extra care to ensure that I use the latest features of every dependency.
== It should be easy... right?
Let's deploy this on a server on #link("https://m.do.co/c/371591aa3027")[DigitalOcean].
#pause
#image("./assets/do_droplet.png")
#pause
- Ubuntu 22.04 -- A 2 year old LTS. _Reasonable_
#pagebreak()
Let's check the python version
#pause
```bash
$ python3 --version
Python 3.10.12
```
#pause
Two years behind -- too old!
#pagebreak()
Let's install the correct version
```bash
$ sudo apt update && sudo apt upgrade -y
```
#pause
```bash
$ sudo add-apt-repository ppa:deadsnakes/ppa
```
#pause
```bash
$ sudo apt install python3.13
```
#pagebreak()
Looks like that's done!
#pause
```bash
$ python3 --version
Python 3.10.12
```
#pause
That's not correct!
#pause
```bash
$ python3.13 --version
Python 3.13.3
```
#pause
We have two Python versions installed, but ehh... let's just move on.
== Getting the code
Let's get the code on the server
```bash
$ git clone https://github.com/OSDG-IIITH/intro-to-docker
```
#pause
```bash
$ mv intro-to-docker/py-example . && rm -fr intro-to-docker
```
#pause
```bash
$ cd py-example
```
#pause
```bash
$ ls
main.py requirements.txt schema.sql static
post_images ruff.toml src views
```
== `venv` time!
Always use a virtual environment!
```bash
$ python3.13 -m venv venv
Error: Command '['/home/user/py-example/venv/bin/python3.13', '-m', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
```
#pause
Dammit!
```bash
$ sudo apt install python3.13-venv -y
```
#pause
```bash
$ python3.13 -m venv venv
```
#meanwhile
#align(
bottom + center,
text(size: 12pt, fill: gray)[
Yes, I know that there are 3102 better package managers (`pdm`, `uv`, etc) for
python, but I've kept it simple for demonstration's sake.
#v(0.5em)
],
)
#pagebreak()
Activate and install!
```bash
$ source venv/bin/activate
```
#pause
```bash
$ pip install -r requirements.txt
...
Successfully installed Jinja2-3.1.6 MarkupSafe-3.0.2 PyYAML-6.0.2 Pygments-2.19.1 annotated-types-0.7.0 anyio-4.9.0 ...
```
#pause
Yay!
== That's not all...
We also need Postgres and #strike[Redis] Valkey
#pause
The version in Ubuntu's repos is Postgres 14, which is too old, we need 17
#pause
```bash
$ # from https://www.postgresql.org/download/linux/ubuntu/
$ sudo apt install -y postgresql-common
$ sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
```
#pause
```bash
$ sudo apt install postgresql-17 -y
```
== Postgres setup
Create a user and database named `instabad`:
```sql
$ sudo -u postgres psql
# inside the postgres shell
> create user instabad with password 'instabad';
CREATE ROLE
> create database instabad with owner = instabad;
CREATE DATABASE
```
#pause
Also update `/var/lib/postgresql/17/main/pg_hba.conf` to allow `instabad` to log in with `md5` auth (or `trust` me bro).
#pagebreak()
Seed the schema:
```bash
psql -U instabad -f schema.sql
```
And create a `.env` file for the application
```bash
$ cp .env{.example,}
$ sed -i 's/^\(DATABASE_URL\)=.*$/\1=postgres:\/\/instabad:instabad@localhost:5432\/instabad/' .env
```
== Valkey setup
Valkey did not even exist when this version of Ubuntu came out.
We'll need to download the binary from #link("https://valkey.io/download")[valkey.io].
#pause
```bash
$ wget -O valkey.tgz https://download.valkey.io/releases/valkey-8.1.0-jammy-x86_64.tar.gz
$ tar -xvzf valkey.tgz
$ mv valkey-* valkey
```
#pause
```bash
$ ./valkey/bin/valkey-cli --version
./valkey/bin/valkey-cli: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.38' not found (required by ./valkey/bin/valkey-cli)
```
#pagebreak()
The version of glibc is too old!
#pause
We'll have to compile valkey manually with muslibc instead, or compile an older version.
#pause
#v(2em)
#text(size: 36pt)[I give up!]
#pause
#text(size: 28pt)[If only there was a way to make this easier...]
= Enter Docker!
#pause
Docker, simply put, is a tool for managing containers.
== What is a Container?
A container is a _loosely_ isolated environment, which runs on a host machine.
#pause
- Multiple containers can run on the same host machine
#pause
- Applications you wish to deploy are put in containers
#pause
- Containers contain everything required to run an application, thus the application
usually never has to reach out to the host machine -- isolation!
#pause
- However, some resources (like networking, or the filesystem) can be shared with
or among containers -- _loose_ isolation!
#pagebreak()
=== What does this mean?
#pause
- No need to worry about tool conflicts (like the two versions of python we had earlier)
#pause
- No need to worry about adding random repositories (like in the case of postgres)
#pause
- No need to worry about glibc!
#pause
Everything that is needed by the application is in the container#uncover("6-")[, or, _as we'll see later_,
in multiple containers working together!]
#pause
#pause
#text(
size: 14pt,
)[If you've heard of virtual machines, this should _sound_ familiar.]
== ELI5
Board work!
== Virtual Machines vs Containers
#pause
#grid(
columns: (1fr, 1fr),
gutter: 1cm,
align(center + horizon, image("assets/vmvscontainer.png")),
text(size: 16pt)[
#pause
- Virtual Machines use a whole Guest Operating System.
- They take longer to boot and shut down.
- More resource intensive and slower, since it has to support a whole operating system
- They can run any operating system -- Windows too!
- Containers use the host's Operating System, and are isolated.
- They are supported by the Kernel. Startup and shutdown is instant!
- Faster!
- They usually run Linux.
],
)
#text(
size: 18pt,
)[They each have their own perks, their usefulness depends on the situation!]
== What does Docker do?
#text(size: 22pt)[
Docker is a tool used for _managing_ (not just!) containers.
#pause
- It creates containers, monitors them, and deletes them too.
#pause
- It configures containers and allows them to interact with the host system.
#pause
- It builds _images_, which are templates for creating containers.
#pause
- It creates _volumes_, which are _persistent storage_ for containers.
#pause
Containerization is an open standard called OCI, the #strong[O]pen #strong[C]ontainer #strong[I]nitiative.
There are other tools that work on OCI containers too, like `podman`, `lxc`/`lxd`, `kubernetes`, etc.
#pause
You can even write your own, since it's all just a bunch of syscalls anyway!
]
#pagebreak()
Let's create a container!
#pause
Try running
```bash
$ docker run --rm hello-world
```
#pause
This command downloads the `hello-world` _image_ from the Docker Hub _repository_ and
creates a _container_ for you.
The `--rm` flag means "remove this container when it's done executing".
#pagebreak()
#text(size: 16pt)[
```plain
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
[...]
For more examples and ideas, visit:
https://docs.docker.com/get-started/
```
]
== To wrap up...
#text(size: 22pt)[
The benefits of using Docker (or any other container management tool):
- *Simplicity:* To run a container, all you need is its image and a container management tool.
- *Flexibility:* You can put in a container whatever you like! You can build new containers
on top of other containers.
- *Shareability:* Images, the blueprints used to build a container, can be uploaded to remote
_repositories_, which allows them to be shared just as easily as remote git repositories make
it to share code.
- *Reproducibility\*:* A container is always built the same way. If it works on your machine, it'll
work on mine too!
]
#place(
bottom + right,
text(
fill: gray,
size: 14pt,
)[\* Does not apply to all cases (NixOS mentioned).],
)
== Today's Agenda
#[
#set text(size: 16pt)
#show sym.checkmark: it => text(fill: green, it)
- #strike[Why Docker] #sym.checkmark
#pause
- #strike[What is Docker] #sym.checkmark
- #strike[What's a container] #sym.checkmark
- #strike[The difference between a container and a virtual machine] #sym.checkmark
#pause
- How to use Docker
- Create containers, images, and volumes
- Use registries
- Deploy applications
#pause
- `docker compose`
- Make containers talk to each other!
- Create container replicas
#pause
- Fun things:
- Running GUI apps in docker
- A little bit of Kubernetes
]
#meanwhile
#place(
center + bottom,
text(fill: gray, size: 12pt, [`docker`? I hardly even know her.]),
)
== Images
Images are _blueprints_ for building containers. They contain everything required to create containers,
its files, configuration, and dependencies.
#pause
Images can be _pushed_ to remote repositories, and _pulled_ from them as well. This makes it very easy
to deploy containers.
#pause
Images can be _tagged_, as in given a name and version.
#pause
You can build images on top of other images.
For example, the #link("https://hub.docker.com/_/postgres")[`postgres`] image is built on
top of the `debian` or `alpine` image, depending on the tag you download.
#pagebreak()
#text(size: 20pt)[
Let's download the `postgres` image:
#pause
```bash
$ docker pull postgres:alpine
```
Here, `postgres` is the name of the image, and `alpine` is its tag, which can be seen as a version.
#pause
There are other tags too,
- `17`
- `17:alpine`
- `16`, etc (https://hub.docker.com/_/postgres/tags)
The default tag (if not specified), is `latest`.
]
== Repositories
The default repository that Docker uses is called Docker Hub (https://hub.docker.com). It contains
images for many popular tools (`python`, `nodejs`, `rust`, `postgres`, `mariadb`, `redis`, etc.).
There are other repositories available from popular cloud providers like GitHub, Google, AWS, etc.
You can even create your own repository, it's an open standard anyway!
We'll learn how to push images a little later!
== Your second container!
Interacting with the system:
- Environment Variables
- Network Ports
- Bind Mounts
- Volumes
== Alpine Linux
A security-oriented, _lightweight_ Linux distribution based on _musl libc_ and _busybox_.
#place(
right,
text(
size: 13pt,
)[-- #link("https://alpinelinux.org")[alpinelinux.org]],
)
== Let's create an Image
_Dockerize_ a simple NodeJS application
- Dockerfile
- Layer caching
#pause
Reducing the size of an image:
- `.dockerignore`
- Alpine Linux
- Multi-stage builds
== Show it to the World!
Let's push the image on Docker Hub!
== Let's do the same for Python
Let's Dockerize #text(fill: gradient.conic(angle: 180deg, ..color.map.rainbow))[Instabad]!
== Docker Compose
#[
#show link: it => text(fill: blue, it)
#set text(size: 20pt)
== Awesome Resources
- This 1-hour introduction video by Travis Media \
https://youtu.be/i7ABlHngi1Q \
- The (unofficial) Docker handbook \
https://docker-handbook.farhan.dev/
- The Docker documentation \
Their tutorial: https://docs.docker.com/get-started/ \
`docker` command reference: https://docs.docker.com/reference/cli/docker/ \
`Dockerfile` reference: https://docs.docker.com/reference/dockerfile/ \
`docker-compose` reference: https://docs.docker.com/reference/compose-file/
- Other awesome resources: https://github.com/veggiemonk/awesome-docker
== Fun Stuff
- Awesome Stacks: `docker compose` config for several popular tools \
(start your self-hosting journey today!) \
https://github.com/ethibox/awesome-stacks
- Kubernetes: (a basic intro by Travis Media: https://youtu.be/r2zuL9MW6wc) \
Nigel Poulton's _The Kubernetes Book_ (available in the Library!) \
Jeff Geerling's Kubernetes 101: https://www.youtube.com/playlist?list=PL2_OBreMn7FoYmfx27iSwocotjiikS5BD
- `distrobox`: Distros in a box (container) https://distrobox.it
- `dokku`: Docker-powered self-hosted Heroku https://github.com/dokku/dokku
- #link("https://containers.dev")[`containers.dev`]: Docker-powered development environments
- `lazydocker`: Docker TUI https://github.com/jesseduffield/lazydocker
]
= Thank you!
#text(size: 20pt)[
This session was brought to you by #link("https://osdg.iiit.ac.in")[OSDG \@ IIITH]
]
#text(size: 16pt, fill: gray)[
#link("mailto:[email protected]")[Aarnav Pai]: https://github.com/arnu515 \
]
#place(
bottom + center,
link(
"https://osdg.iiit.ac.in",
image("./assets/osdg.png", width: 2.5cm),
),
)