forked from realestate-com-au/intro-to-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1068 lines (775 loc) · 16.8 KB
/
index.html
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>An introduction to Docker</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css" />
<link rel="shortcut icon" href="media/favicon.png" type="image/png" />
</head>
<body>
<textarea id="source">
class: center, middle
# An introduction to
![Docker](media/docker/small_v-trans.png)
---
class: center, middle
# Courtesy of
![REA Group](media/REA.png)
---
# Agenda
.lhs[
* Intro and overview
* [Check your setup](#prep)
* [Hello world!](#hello-world)
* [Key concepts](#key-concepts)
* [Containers vs Virtual Machines](#containers-vs-vms)
* [The clever bits](#the-clever-bits)
* Images
* [Names and tags](#image-names-and-tags)
* [Building images](#building-images)
* [Publishing images](#publishing-images)
]
.rhs[
* Containers
* [Ports](#ports)
* [Environment variables](#environment)
* [Networks](#networks)
* [Volumes](#volumes)
* [Docker Compose](#compose)
]
---
name: prep
# Preparation
You will need **a good connection to the internet** during this setup, and
(to some extent) while going through the course, as we'll be installing
software, and downloading Docker "images".
It's useful to have **a local copy of these training materials**:
`git clone https://github.com/realestate-com-au/intro-to-docker.git`
You definitely need **Docker and Docker Compose**.<br/>
Installation intructions follow.
---
# Installing Docker
#### OS X
* Install [Docker for Mac](https://docs.docker.com/docker-for-mac/)
#### Windows
* Install [Docker for Windows](https://docs.docker.com/docker-for-windows/)
#### Linux
```
$ `curl -sSL https://get.docker.com/ | sh`
```
---
# Docker Engine
.center[
![on Linux](media/linux_docker_host.svg)
![on Windows](media/mac_docker_host.svg)
]
???
Docker only runs on Linux.
On OS X and Windows, it runs inside a VM.
---
# Check your setup
Let's check that everything is working:
```
$ `./setup.sh`
YAY: /var/run/docker.sock exists
YAY: I can talk to docker
Docker is at localhost
YAY: docker version 1.12.0
YAY: docker-compose version 1.8.0
Pulling some images to get you started ...
```
Not working? Ask for help!
.small.lhs[
Don't worry _too_ much if you get complaints about versions of `docker`
and `docker-compose`; _most_ things will still work.)
]
---
name: hello-world
# Try it: Hello world!
"Pull" an image:
```
$ `docker pull hello-world`
Using default tag: latest
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
```
Run a container:
```
$ `docker run hello-world`
Hello from Docker.
…
```
---
name: key-concepts
# Key concepts
.lhs[
## "image"
* passive
* a file system snapshot
* similar to:
- Virtual Machine image
- AMI (Amazon Machine Image)
]
--
.rhs[
## "container"
* active
* a group of processes
* similar to:
- Virtual Machine
- EC2 instance
]
--
.center[
![on Linux](diagrams/images-and-containers.png)
]
---
# Try it: different Linux distros
Run Ubuntu:
```
$ `docker run ubuntu dpkg -l`
```
or CentOS:
```
$ `docker run centos rpm -qa`
```
--
…
Speedy, huh?
---
# Try it: run a shell
```
$ `docker run -i -t ubuntu bash`
```
* `-i` = Keep STDIN open
* `-t` = Allocate a pseudo-TTY
Now, try doing an Ubuntu thing:
```
root@container# `apt-get update`
```
---
name: containers-vs-vms
# Containers vs. Virtualisation
.center[
![VMs vs. containers](diagrams/containers-vs-vms.png)
]
???
With virtualisation, you have the added weight of a guest kernel.
With Docker, processes communicate directly with the host kernel.
---
# More containers vs. VMs
<table class="compare">
<tr>
<td></td>
<th>Virtual<br/>Machines</th>
<th>Docker</th>
</tr>
<tr>
<td class="factor">image size</td>
<td>Gigabytes</td>
<td>Megabytes</td>
</tr>
<tr>
<td class="factor">startup time</td>
<td>minutes</td>
<td>sub-second</td>
</tr>
<tr>
<td class="factor">Linux kernel is</td>
<td>separate</td>
<td>shared</td>
</tr>
<tr>
<td class="factor">isolation is</td>
<td>complete</td>
<td>pretty good</td>
</tr>
<tr>
<td class="factor">used to encapsulate</td>
<td>servers</td>
<td>services</td>
</tr>
</table>
---
# Try it: which kernel?
Run `uname -a` on both `ubuntu` and `centos`:
```
$ `docker run ubuntu uname -a`
$ `docker run centos uname -a`
```
Can you explain the result?
--
…
Different distros, same kernel: same result.
.center[
![2 distros, one kernel](diagrams/two-distros-one-kernel.png)
]
---
# Try it: overhead, much?
Use "time" to measure the overhead of running a command in a container.
```
$ `time docker run ubuntu bash -c "time sleep 1"`
```
How much time did Docker add?
---
name: the-clever-bits
class: center, middle, big
# The clever bits
Linux "cgroups" + "image" management =
![Docker](media/docker/small_v-trans.png)
---
# Linux "control groups"
.big.center["`chroot` on steroids"]
<br/>
Isolate processes by limiting access to:
- file system
- hardware resources
- other processes
Not a new idea:
* aka LXC
* project started by Google
* used extensively by PaaS such as Heroku
* similar to Solaris Zones, BSD Jails
---
# Union file systems
.lhs[
Images are made of "layers", and can **share** base layers.
Container root file system is just another (writable) layer.
Various drivers supported:
- aufs
- devicemapper
- btrfs
- overlay
- zfs
]
.rhs[
<table class="docker-image">
<tr><td class="writable">container root</td></tr>
<tr><td>↓</td></tr>
<tr><td class="snapshot">image layer</td></tr>
<tr><td>↓</td></tr>
<tr><td class="snapshot">image layer</td></tr>
<tr><td>↓</td></tr>
<tr><td class="snapshot">image layer</td></tr>
</table>
]
---
# Try it: test container file system isolation
Attempt to cripple your Ubuntu image:
```
$ `docker run -i -t ubuntu bash`
root@3256d8252fe6:/# `ls /usr/bin`
root@3256d8252fe6:/# `rm -fr /usr/bin`
root@3256d8252fe6:/# `ls /usr/bin`
^D
```
Now, start another container:
```
$ `docker run -i -t ubuntu bash`
root@3ed46bfba026:/# `ls /usr/bin`
```
---
# Try it: check out those layers
Use `docker history` to view image layers
```
$ `docker history ubuntu`
IMAGE CREATED CREATED BY
07c86167cdc4 2 days ago /bin/sh -c #(nop) CMD ["/bin
<missing> 2 days ago /bin/sh -c sed -i 's/^#\s*\(
<missing> 2 days ago /bin/sh -c echo '#!/bin/sh'
<missing> 2 days ago /bin/sh -c #(nop) ADD file:b
```
---
name: images
class: center, middle
# Images
---
name: image-names-and-tags
# Image names and tags
Image-name pattern:
.center[
`[[<REGISTRY>/]<NAMESPACE>/]<REPOSITORY>[:<TAG>]`
]
* `<REGISTRY>` defaults to `docker.io` ("official" registry)
* `<NAMESPACE>` defaults to `library`
* `<TAG>` defaults to `latest`
These are all equivalent:
```
$ `docker run hello-world`
$ `docker run hello-world:latest`
$ `docker run library/hello-world`
$ `docker run docker.io/library/hello-world:latest`
$ `docker run sha256:c54a2cc56cbb`
$ `docker run sha256:c54a2cc56cbb2f04003c1cd4507e118af7c0d340fe7e2720f70976c4b75237dc`
```
---
# Try it: investigate tags in the `ubuntu` repository
Run a command using `ubuntu:16.04`:
```
$ `docker run ubuntu:16.04 grep -v "^#" /etc/apt/sources.list`
```
Try the same thing using `ubuntu:14.04`:
```
$ `docker run ubuntu:14.04 grep -v "^#" /etc/apt/sources.list`
```
--
View images in the `ubuntu` repository:
```
$ `docker images ubuntu`
```
---
name: building-images
# Try it: build an image, the hard way
Install some software in an `ubuntu` container:
```
$ `docker run -i -t ubuntu bash`
root@f55393e5dc31:/# `apt-get update && apt-get install -y curl`
…
^D
```
Now, `commit` that container to create an image:
```
$ `docker commit f55393e5dc31 ubuntu-with-curl`
```
Check it out:
```
$ `docker history ubuntu-with-curl`
…
$ `docker run ubuntu-with-curl curl http://example.com`
```
---
# Layers, with labels
```
$ `docker history ubuntu-with-curl`
```
<table class="docker-image">
<tr>
<td class="snapshot">27323a7d221f</td>
<td class="labels">ubuntu-with-curl</td>
</tr>
<tr><td>↓</td></tr>
<tr>
<td class="snapshot">07c86167cdc4</td>
<td class="labels">ubuntu:latest</td>
</tr>
<tr><td>↓</td></tr>
<tr>
<td class="snapshot">...</td>
</tr>
<tr><td>↓</td></tr>
<tr>
<td class="snapshot">...</td>
</tr>
<tr><td>↓</td></tr>
<tr>
<td class="snapshot">...</td>
</tr>
</table>
---
# Try it: build an image, the easy way
Use a `Dockerfile`:
```
FROM ubuntu
RUN apt-get update && apt-get install -y curl
```
to build an image:
```
$ `docker build -t ubuntu-with-curl exercises/ubuntu-with-curl`
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
---> 07c86167cdc4
Step 2 : RUN apt-get update && apt-get install -y curl
---> Running in 51bf195331b7
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [64.4 kB]
…
---> 70b42c74bb66
Removing intermediate container 51bf195331b7
Successfully built 70b42c74bb66
```
---
# Try it: leverage the "build cache"
Use the recipe provided:
```
FROM node:6.2.2
COPY package.json /app/
WORKDIR /app
RUN npm install
COPY index.js /app/
ENV PORT 80
EXPOSE $PORT
CMD ["node", "/app/index.js"]
```
to build an image:
```
$ `docker build -t ciao exercises/ciao`
```
---
# Try it: invalidate the "build cache"
Build it again:
```
$ `docker build -t ciao exercises/ciao`
```
Faster, eh? Observe: "Using cache".
--
Make a change to `exercises/ciao/index.js`, then build again:
```
$ `docker build -t ciao exercises/ciao`
```
What happens at "Step 5"?
--
Now make a change to `exercises/ciao/package.json`, and build again.
```
$ `docker build -t ciao exercises/ciao`
```
What happens at "Step 4"?
---
name: publishing-images
# Publishing images
Sign up for an account at [`http://hub.docker.com`](http://hub.docker.com)
.center[
<img src="media/docker-hub.png" width="100%"/>
]
---
# Try it: push an image to Docker Hub
Authenticate to Docker Hub:
```
$ `docker login`
```
"Tag" an image into _your_ namespace:
```
$ `docker tag ciao YOURNAMEHERE/ciao`
```
Now you can push it:
```
$ `docker push YOURNAMEHERE/ciao`
```
---
# Try it: pull an image someone else pushed
Talk to the esteemed colleague next to you, and ask them
for their Docker Hub username. Then, you shoud be able to
fetch the image _they_ pushed.
```
$ `docker pull YOURNEIGHBOUR/ciao`
```
---
class: middle, center
# Containers
---
# Try it: container basics
Run a container as a daemon (in the background):
```
$ `docker run -d ciao`
```
List the running containers:
```
$ `docker ps`
```
List ALL the containers:
```
$ `docker ps -a`
```
Remove a container:
```
$ `docker rm <ID_OR_NAME>`
```
Remove a RUNNING container:
```
$ `docker rm -f <ID_OR_NAME>`
```
---
# Try it: name your containers
Start a container with a `--name`:
```
$ `docker run --name app ciao`
```
Now you can use the NAME rather than an ID:
```
$ `docker rm -f app`
```
---
name: ports
# Try it: map a port to a host port
```
$ `docker run -d --name app -p 5678:80 ciao`
```
.center[
<img src="diagrams/ciao-explicit-port.png" width=40% />
]
```
$ `curl localhost:5678`
```
Remember to clean up:
```
$ `docker rm -f app`
```
---
# Try it: use a random host port
If you don't specify a host port, Docker will choose one:
```
$ `docker run -d --name app -p 80 ciao`
```
.center[
<img src="diagrams/ciao-random-port.png" width=40% />
]
Use `docker port` to discover which one it chose:
```
$ `docker port app 80`
```
---
# Try it: logs
You can get the output using `docker logs`:
```
$ `docker logs app`
```
or even follow along in real time:
```
$ `docker logs --follow --timestamps app`
```
---
name: environment
# Try it: set environment variables
Assuming your application looks for environment variables, e.g.
```javascript
var MESSAGE = (process.env.MESSAGE || "Ciao mondo.");
```
You can set them to provide configuration:
```
$ `docker run -d --name app -p 5678:80 -e MESSAGE='Hey, guys!' ciao`
```
Test the result:
```
$ `curl localhost:5678`
```
---
name: networks
class: middle, center
# Networks
---
# Consider: a reverse-proxy image
```
upstream backend {
server ${BACKEND:-"app"} fail_timeout=0;
}
server {
listen ${PORT:-80};
location / {
proxy_pass http://backend;
}
}
```
---
# Try it: inter-container networking
Create a "network":
```
$ `docker network create shared`
$ `docker network ls`
```
Attach some containers:
```
$ `docker rm -f app proxy`
$ `docker run -d --net=shared --name app \`
`realestate/ciao`
$ `docker run -d --net=shared --name proxy \`
`-p 5678:1234 -e PORT=1234 -e BACKEND=app \`
`realestate/ciao-proxy`
```
```
$ `curl -si localhost:5678`
```
---
class: center, middle
<img src="diagrams/shared-network.png" width="60%" />
---
name: volumes
class: middle, center
# Volumes
---
# Try it: volume from host
Mount your home directory from the "host":
```
$ `docker run -it --rm -v $HOME:/myhome ubuntu bash`
root@c10a43c38793:/# `mount | grep home`
root@c10a43c38793:/# `cd /myhome; ls`
root@c10a43c38793:/# `echo HELLO FROM DOCKER > written-from-docker`
^D
$ `ls $HOME`
```
---
# Try it: named cache volume
```
$ `docker run -it --rm -v my-cache:/cache ubuntu bash`
root@c10a43c38793:/# `echo TESTING > /cache/test`
^D
```
Observe, volume created:
```
$ `docker volume ls`
```
Use it again:
```
$ `docker run -it --rm -v my-cache:/cache ubuntu bash`
root@f5be5dec8a5a:/# `ls /cache`
```
Clean up:
```
$ `docker volume rm my-cache`
```
---
# Try it: anonymous volume for extra writable space
Tip: you can improve security by:
* mounting your root volume read-only
* creating writable volumes as required
```
$ `docker run -it --rm --read-only -v /scratch ubuntu`
root@c10a43c38793:/# `echo TESTING > /tmp/test`
root@c10a43c38793:/# `echo TESTING > /scratch/test`
```
---
# Try it: share a volume with another container
Create a container, with a volume:
```
$ `docker run -it --rm --name provider -v data:/published ubuntu`
root@118f38503653:/# `echo ohai > /published/stuff`
```
.center[
<img src="diagrams/shared-volume.png" width=80% />
]
Use the volume from a different container:
```
$ `docker run -it --rm --name consumer -v data:/provided ubuntu`
root@d88a7a468b01:/# `cat /provided/stuff`
```
---
# Creating volumes in images
Use the `VOLUME` directive in your `Dockerfile` to create a volume.
Sample from "[postgres](https://github.com/docker-library/postgres/blob/8a9fbcb40f13ccc7762f278b9df611cabe22d300/9.5/Dockerfile)" image:
```
FROM debian:jessie
RUN apt-get install -y postgresql-common postgresql-9.5
…
ENV PGDATA /var/lib/postgresql/data
VOLUME /var/lib/postgresql/data
…
```
---
name: compose
class: middle, center
# Docker Compose
---
# Try it: docker-compose
Given YAML config file:
```
version: '2'
services:
app:
image: realestate/ciao
proxy:
image: realestate/ciao-proxy
depends_on:
- app
environment:
BACKEND: app
ports:
- 5678:80
```
You can start the containers with:
```
$ `cd exercises/composed/basic`
$ `docker-compose up`
```
---
# Try it: under the covers of compose
Generate some logs:
```
$ `curl localhost:5678`
```
What containers are running?
```
$ `docker-compose ps`
$ `docker ps`
```
Take it all down:
```
$ `docker-compose down`
```
---
# Try it: get compose to build your images
```
version: '2'
services:
app:
build: ../../ciao
proxy:
build: ../../ciao-proxy
depends_on:
- app
environment:
BACKEND: app:80
ports:
- 5678:80
```
--
```
$ `cd exercises/composed/autobuilt`
$ `docker-compose up`
```
---
# Try it: sharing a volume
```
version: '2'