summaryrefslogtreecommitdiff
path: root/docker_cheat_sheet.md
blob: 621f9d9549d3f17937cb295e1b113fdfc26ef1cd (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
# Docker Cheat Sheet

## Docker CLI

### Management

|                            |                                       |
|----------------------------|---------------------------------------|
|`docker info`               |Display system information             |
|`docker version`            |Display the system's version           |
|`docker login`              |Log in to a Docker registry            |

### Running and stopping

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker pull [imageName]`             |Pull an image from a registry|
|`docker run [imageName]`              |Run containers               |
|`docker run -d [imageName]`           |Detached mode                |
|`docker start [containerName]`        |Start stopped containers     |
|`docker ps`                           |List running containers      |
|`docker ps -a`                        |List running and stopped containers|
|`docker stop [containerName]`         |Stop containers              |
|`docker kill [containerName]`         |Kill containers              |
|`docker image inspect [imageName]`    |Get image info               |

### Limits

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker run --memory="256m" nginx`    |Max memory                   |
|`docker run --cpus=".5" nginx`        |Max CPU                      |

### Attach Shell

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker run -it nginx /bin/bash`   |Attach shell                 |
|`docker run -it microsoft/powershell:nanoserver pwsh.exe`|Attach Powershell|
|`docker container exec -it [containerName] bash`|Attach to a running container|

### Cleaning up

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker rm [containerName]`           |Removes stopped containers   |
|`docker rm $(docker ps -a -q)`        |Removes all stopped containers|
|`docker images`                       |List images                  |
|`docker rmi [imageName]`              |Deletes the image            |
|`docker system prune -a`              |Removes all images not in use by any containers|

### Building

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker build -t [name:tag] .`        |Builds an image using a Dockerfile located in the same folder|
|`docker build -t [name:tag] -f [fileName]`|Builds an image using a Dockerfile located in a different folder|
|`docker tag [imageName] [name:tag]`   |Tag an existing image        |

### Volumes

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker create volume [volumeName]`   |Creates a new volume         |
|`docker volume ls`                    |Lists the volumes            |
|`docker volume inspect [volumeName]`  |Display the volume info      |
|`docker volume rm [volumeName]`       |Deletes a volume             |
|`docker volume prune`                 |Deletes all volumes not mounted|

### Docker Compose

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker compose build`                |Build the images             |
|`docker compose start`                |Start the containers         |
|`docker compose stop`                 |Stop the containers          |
|`docker compose up -d`                |Build and start              |
|`docker compose ps`                   |List what's running          |
|`docker compose rm`                   |Remove from memory           |
|`docker compose down`                 |Stop and remove              |
|`docker compose logs`                 |Get the logs                 |
|`docker compose exec [container] bash`|Run a command in a container |

#### Docker Compose V2 (New Commands)

|                                      |                             |
|--------------------------------------|-----------------------------|
|`docker compose --project-name test1 up -d`|Run an instance as a project|
|`docker compose -p test2 up -d`       |Shortcut                     |
|`docker compose ls`            `      |List running projects        |
|`docker compose cp [containerID]:[SRC_PATH] [DEST_PATH]`|Copy files from the container|
|`docker compose cp [SRC_PATH] [containerID]:[DEST_PATH]`|Copy files to the container|

---

### Running a container

```sh
# pull and run an nginx server
# `--publish 80:80` maps the host port to the container listening port
# `--name webserver` container local name
# `nginx` container image in the Docker registry
docker run --publish 80:80 --name webserver nginx

# list the running containers
docker ps

# stop the container
docker stop webserver

# remove the container
docker rm webserver
```

---

### Dockerfile Example - static HTML site

```Dockerfile
FROM nginx:alpine
COPY . /usr/share/nginx/html
```

```sh
# build
docker build -t webserver-image:v1 .

# run 
docker run -d -p 8080:80 webserver-image:v1

# display
curl localhost:8080
```

### Dockerfile Example - Node site

```Dockerfile
# Base image
FROM alpine
# Install Node and NPM using the package manager
RUN apk add -update nodejs nodejs-npm
# Copy the files from the build context
COPY . /src
WORKDIR /src
# Run a command
RUN npm install
# Adds metadata
EXPOSE 8080
# What to run
ENTRYPOINT ["node", "./app.js"]
```

---

### Mapping a Volume

```sh
# create a volume
docker volume create myvol

# inspect the volume
docker volume inspect myvol

# list the volumes
docker volume ls

# run a container with a volume
# `-v myvol:/app` mapping the volume to a logical folder
docker run -d --name devtest -v myvol:/app nginx:latest
```

#### Mapping to a local folder

```sh
# run a container with a volume
# `d:/test` specify a folder
docker run -d --name devtest -v d:/test:/app nginx:latest

# inspect the container
docker inspect devtest
```

---

## Docker Compose

- Plugin for docker
    - *define and run multi-container Docker applications with [YAML](./yaml_cheat_sheet.html)*
- `docker: 'compose' is not a docker command.`
    - **Solution:** On some systems (I think this pertains to Docker Compose V1), `docker compose` should be run as `docker-compose`

### Example Docker Compose file

The following example defines 3 containers:

```yaml
version: '3.9'

services:
  webapi1:
    image: academy.azurecr.io/webapi1
    ports:
      - '8081:80'
    restart: always

  webapi2:
    image: academy.azurecr.io/webapi2
    ports:
      - '8082:80'
    restart: always

  apigateway:
    image: academy.azurecr.io/apigateway
    ports:
      - '80:80'
    restart: always
```

### Docker Compose Example

```sh
# build the service
docker compose build

# builds, (re)creates, starts, attaches to containers for a service
docker compose up

# list the services
docker compose ps

# bring down what was created by UP
docker compose down
```

### Resource Limits example

```yaml
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 150M
        reservations:
          cpus: '0.25'
          memory: 20M
```

### Environment Variables example

```yaml
services:
  web:
    image: nginx:alpine
    environment:
      - DEBUG=1
      - FOO=BAR
```

Environment variables can be overwritten at the command line:
- `docker compose up -d -e DEBUG=0`

#### Referencing an environment variable

```
# set an environment variable
export POSTGRES_VERSION=14.3

# powershell
$env:POSTGRES_VERSION="14.3"
```

```yaml
services:
  db:
    images: "postgres:${POSTGRES_VERSION}"
```

You can also create a `.env` file containing environment variable definitions

### Dependence example

```yaml
services:
  app:
    image: myapp
    depends_on:
      - db
  db:
    image: postgres
    networks:
      - back-tier
```

The example Docker Compose file has the `db` service start before the `app` service can start

### Volumes example

```yaml
services:
  app:
    image: myapp
    depends_on:
      - db
  db:
    image: postgres
    volumes:
      - db-data:/etc/data
    networks:
      - back-tier

volumes:
  db-data:
```

### Restart Policy example

```yaml
services:
  app:
    image: myapp
    restart: always
    depends_on:
      - db
  db:
    image: postgres
    restart: always
```

- By default, containers will not be restarted upon reboot
- Possible options include:

|                  |                                                 |
|------------------|-------------------------------------------------|
|`no`              |Does not restart a container under any circumstances (default)|
|`always`          |Always restarts the container until its removal  |
|`on-failure`      |Restarts a container if the exit code indicates an error|
|`unless-stopped`  |Restarts a container irrespective of the exit code but will stop restarting when the service is stopped or removed|