summaryrefslogtreecommitdiff
path: root/README.md
blob: 4dcadb4fb048940ab0baac752b63de1b0fd64f37 (plain)
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
---
title: Developing with Docker
author: gitlab.com/xlgmokha/developing-with-docker
date: 2020-06-10
---

# Developing with Docker - Mo Khan | Software Engineer | Composition Analysis | GitLab

```text
    How
     to
      Docker
       better?
                    ##         .
              ## ## ##        ==
           ## ## ## ## ##    ===
       /"""""""""""""""""\___/ ===
      {                       /  ===-
       \______ O           __/
         \    \         __/
          \____\_______/
```

# Agenda

* Definitions
* Ecosystem
* Build
* Analyze
* Optimize

```text
< What are we going to talk about? >
 ----------------------------------
    \
     \
      \
                    ##        .
              ## ## ##       ==
           ## ## ## ##      ===
       /""""""""""""""""___/ ===
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
       \______ o          __/
        \    \        __/
          \____\______/
```

# Definitions

* Image
* Container
* Registry

# Definitions - Image

A Docker image is similar to a Ruby class.
A class defines the behaviour and data associated with the class.

```ruby
class Person
  def fist_bump(other_person)
  end
end
```

# Definitions

You can't do much with a class until you create
an instance of one.

```ruby
mo = Person.new
you = Person.new
```

# Definitions - Objects

Once a class is instantiated you can invoke
methods on the object. An object can interact
with other objects.

```ruby
mo.first_bump(you)
```

# Definitions - Container

A container is a running instance of an image.
Similar to how an object is an instance of a class.

| Ruby | Docker |
| -- | -- |
| Class | Image |
| Object | Container |

# Identifiers

Classes can be identified by their name.
Images can be identified by their image Id or `name:tag`

Objects can be identified by their `object_id` in Ruby.
Containers can be identified by their container Id or a name.

| Ruby | Docker |
| -- | -- |
| Person | Image ID |
| mo.object_id | Container ID |

# Image identifier

`[registry]name:tag`

If the registry is omitted, then docker.io is assumed.

* registry.gitlab.com/gitlab-org/security-products/license-management:latest
* alpine:latest

# Definitions - Registry

Registry: stores images and makes them available to others

This include metadata about images and blobs for each layer in the image.

For example:

* https://registry-1.docker.io
* https://registry.gitlab.com

```bash
curl -s -i https://registry-1.docker.io/v2/alpine/tags/list
```

# Architecture

```text
                      ----------
                      | Client |
                      ----------
                      | build  |
                      | pull   |
                      | run    |
                      ----------
                          | (tcp/unix socket)
                          V
                    ---------------
                    | Docker Host |
                    ---------------
                    | Daemon     |
                    | Containers |
                    | Images     |
                    --------------
                          |  A
                          V  |
                     ------------
                     | Registry |
                     ------------
                     | Images   |
                     ------------
```

https://docs.docker.com/get-started/overview/#docker-architecture

# /var/run/docker.sock

```bash
$ curl --unix-socket /var/run/docker.sock http://localhost/images/json
```

```terminal32
curl -i --unix-socket /var/run/docker.sock http://localhost/images/json
```

# $ docker version

The docker CLI is an HTTP client that can connect to Unix or TCP sockets.

```terminal32
docker version
```

# $ docker image ls

```terminal32
docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"
```

# $ docker ps

```terminal32
docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}"
```

# $ docker run -it alpine:latest cat /etc/os-release

1. check if "alpine:latest" is on docker host
1. download "alpine:latest" from registry to docker host
1. start a container using the "alpine:latest" image

```terminal32
docker run -it alpine:latest cat /etc/os-release
```

# Dockerfile

```file
path: examples/001/Dockerfile
relative: true
lang: docker
```

https://docs.docker.com/engine/reference/builder/

# FROM alpine:latest

Initializes a build stage and sets a Base Image.

```file
path: examples/001/Dockerfile
relative: true
lang: docker
```

# COPY "hello.rb"

Copy "hello.rb" from the host to
"/usr/local/bin/hello" within the Docker image.

```file
path: examples/001/Dockerfile
relative: true
lang: docker
```

```bash
$ cat examples/001/hello.rb
```
```file
path: examples/001/hello.rb
relative: true
lang: ruby
```

# Dockerfile - RUN

RUN a command from within the image and make "hello" executable.

```file
path: examples/001/Dockerfile
relative: true
lang: docker
lines:
  start: 2
  end: 3
```

# Dockerfile - CMD

Set the default command to run when the docker image
is launched as a container.

```file
path: examples/001/Dockerfile
relative: true
lang: docker
lines:
  start: 3
  end: 4
```

# docker build -t developing-with-docker:latest examples/001/

```terminal32
time docker build -t developing-with-docker:latest examples/001/
```

# docker run developing-with-docker:latest

```terminal32
docker run developing-with-docker:latest
```

# docker run -it developing-with-docker:latest /bin/sh

```terminal32
docker run -it developing-with-docker:latest /bin/sh
```

# docker ps

```terminal32
docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}"
```

# docker exec -it <imageid> /bin/sh

```terminal32
bash -i examples/001/docker-exec.sh
```

# dive

Useful for identifying bloat.
https://github.com/wagoodman/dive

* Displays each layer
* Allows investigating files that are added/removed/changed in each layer

```terminal32
bash examples/001/dive-exec.sh developing-with-docker
```

# docker pull registry.gitlab.com/gitlab-org/security-products/license-management:latest

* More layers == more parallel downloads
* Smaller layers == faster downloads per layer

```terminal32
bash -i examples/001/docker-large-download.sh
```

# docker build -t big-image:latest examples/002/

```file
path: examples/002/Dockerfile
relative: true
lang: docker
```

```terminal32
time docker build -t big-image:latest examples/002/
```

# docker image ls

```terminal32
docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"
```

# dive big-image:latest

```terminal32
bash examples/001/dive-exec.sh big-image
```

# docker build -t small-image:latest examples/003/

* Collapse layers
* Cleanup unnecessary artifacts
* Deflate files within layers
* Inflate files when container is launched

```file
path: examples/003/Dockerfile
relative: true
lang: docker
```

```file
path: examples/003/run.sh
relative: true
lang: sh
```

```terminal32
time docker build -t small-image:latest examples/003/
```

# docker image ls

```terminal32
docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"
```

# dive small-image:latest

```terminal32
bash examples/001/dive-exec.sh small-image
```

# docker run -it small-image:latest

```terminal32
docker run -it small-image:latest
```

# docker image ls

```terminal32
docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"
```

# Summary

* Keep each layer small
* More layers provides opportunity for more parallel downloads

# Fin

Thank you for your time

[gitlab.com/xlgmokha/developing-with-docker](https://gitlab.com/xlgmokha/developing-with-docker)