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

# Developing with Docker

Mo Khan

Backend Engineer

Composition Analysis, GitLab

# Agenda

* Definitions
* Architecture
* Start/Stop container (ps, start, stop)
* Getting a shell (run vs exec)
* Dockerfile
* Analyzing an image (dive, docker layers)
* Shrinking an image (compression, discuss the trade offs of having more v. less layers)

# Definitions

* Image: is like a class
* Container: is like an instance of a class (i.e. object)

# Definitions - Image/Container

* Person is a class definition
* "you" and "mo" are instances of the class Person
* instances of person can interact with one another

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

mo = Person.new
you = Person.new

mo.first_bump(you)
```

# Definitions

Registry: stores images and makes them available to others

For example:

* https://index.docker.io
* https://registry.gitlab.com

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

# Architecture

```text
        ----------
        | Client |
        ----------
        | build  |
        | pull   |
        | run    |
        ----------
            |
            V
      --------------
      | Host       |
      --------------
      | Daemon     |
      | Containers |
      | Images     |
      --------------
            |  A
            V  |
       ------------
       | Registry |
       ------------
       | Images   |
       ------------
```

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

# $ docker image ls

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

# $ docker ps

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

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

```terminal32
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


# 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
```

```terminal8
tree ./examples/001
```

# 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
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
docker exec -it $(docker ps | grep developing-with-docker:latest | awk '{ print $1 }' | tail -n1) /bin/sh
```

# dive

```terminal32
dive $(docker ps | grep developing-with-docker:latest | awk '{ print $1 }' | tail -n1) /bin/sh
```

* Describe layers
* Downloading multiple layers in parallel vs 1 large layer

# Compression

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

# Compression (zstd)

* Deflate files within layers

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

# Compression (zstd)

* Inflate files when container is launched

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

# Compression

Before:

After:

# Summary

# Fin

Thank you for your time

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