herolib-virt 0.3.13

Virtualization and container management for herolib (buildah, nerdctl, kubernetes)
Documentation
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
# nerdctl Essentials

This guide provides a comprehensive overview of essential nerdctl functionality to help you get started quickly. nerdctl is a Docker-compatible CLI for containerd, with additional features specifically designed for containerd environments.

## Introduction

nerdctl is a Docker-compatible CLI for containerd. It provides the same user experience as the Docker CLI (`docker`) but leverages the more efficient containerd container runtime. Key differences and advantages include:

- Direct integration with containerd (no extra daemon required)
- Support for containerd-specific features
- First-class support for rootless mode
- Compatibility with Docker commands
- Additional nerdctl-specific commands

## Basic Configuration

nerdctl can be configured using the `nerdctl.toml` configuration file:

- Rootful mode: `/etc/nerdctl/nerdctl.toml`
- Rootless mode: `~/.config/nerdctl/nerdctl.toml`

Example configuration:

```toml
debug = false
debug_full = false
address = "unix:///run/containerd/containerd.sock"
namespace = "default"
snapshotter = "overlayfs"
cgroup_manager = "systemd"
hosts_dir = ["/etc/containerd/certs.d", "/etc/docker/certs.d"]
```

Common configuration properties:

| Property            | CLI Flag                          | Description                |
|---------------------|-----------------------------------|----------------------------|
| `address`           | `--address`, `--host`, `-a`, `-H` | containerd address         |
| `namespace`         | `--namespace`, `-n`               | containerd namespace       |
| `snapshotter`       | `--snapshotter`                   | containerd snapshotter     |
| `cni_path`          | `--cni-path`                      | CNI binary directory       |
| `data_root`         | `--data-root`                     | Persistent state directory |
| `insecure_registry` | `--insecure-registry`             | Allow insecure registry    |

## Container Management

### Running Containers

**Run a container**:
```
nerdctl run [OPTIONS] IMAGE [COMMAND] [ARG...]
```

Common options:
- `-i, --interactive`: Keep STDIN open
- `-t, --tty`: Allocate a pseudo-TTY
- `-d, --detach`: Run container in background
- `--name`: Assign a name to the container
- `-p, --publish`: Publish container's port to the host
- `-v, --volume`: Bind mount a volume
- `-e, --env`: Set environment variables
- `--rm`: Automatically remove the container when it exits
- `--restart=(no|always|on-failure|unless-stopped)`: Restart policy
- `--net, --network`: Connect container to a network

Examples:
```bash
# Run an interactive container and automatically remove it when it exits
nerdctl run -it --rm alpine sh

# Run a detached container with port mapping
nerdctl run -d --name nginx -p 8080:80 nginx

# Run a container with a volume mount
nerdctl run -it --rm -v $(pwd):/data alpine ls /data
```

### Managing Containers

**List containers**:
```
nerdctl ps [OPTIONS]
```

Options:
- `-a, --all`: Show all containers (default shows just running)
- `-q, --quiet`: Only display container IDs
- `-s, --size`: Display total file sizes

**Stop a container**:
```
nerdctl stop [OPTIONS] CONTAINER [CONTAINER...]
```

**Start a container**:
```
nerdctl start [OPTIONS] CONTAINER [CONTAINER...]
```

**Remove a container**:
```
nerdctl rm [OPTIONS] CONTAINER [CONTAINER...]
```

Options:
- `-f, --force`: Force removal of running container
- `-v, --volumes`: Remove anonymous volumes

**View container logs**:
```
nerdctl logs [OPTIONS] CONTAINER
```

Options:
- `-f, --follow`: Follow log output
- `--since`: Show logs since timestamp
- `-t, --timestamps`: Show timestamps
- `-n, --tail`: Number of lines to show from the end of logs

**Execute a command in a running container**:
```
nerdctl exec [OPTIONS] CONTAINER COMMAND [ARG...]
```

Options:
- `-i, --interactive`: Keep STDIN open
- `-t, --tty`: Allocate a pseudo-TTY
- `-d, --detach`: Detached mode
- `-w, --workdir`: Working directory
- `-e, --env`: Set environment variables

## Image Management

### Working with Images

**List images**:
```
nerdctl images [OPTIONS]
```

Options:
- `-a, --all`: Show all images
- `-q, --quiet`: Only show numeric IDs
- `--digests`: Show digests

**Pull an image**:
```
nerdctl pull [OPTIONS] NAME[:TAG|@DIGEST]
```

Options:
- `--platform=(amd64|arm64|...)`: Pull content for specific platform
- `-q, --quiet`: Suppress verbose output

**Push an image**:
```
nerdctl push [OPTIONS] NAME[:TAG]
```

**Build an image**:
```
nerdctl build [OPTIONS] PATH
```

Options:
- `-t, --tag`: Name and optionally tag the image
- `-f, --file`: Name of the Dockerfile
- `--build-arg`: Set build-time variables
- `--no-cache`: Do not use cache when building

**Remove an image**:
```
nerdctl rmi [OPTIONS] IMAGE [IMAGE...]
```

Options:
- `-f, --force`: Force removal

**Tag an image**:
```
nerdctl tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
```

**Save an image to a tar archive**:
```
nerdctl save [OPTIONS] IMAGE [IMAGE...]
```

Options:
- `-o, --output`: Write to a file

**Load an image from a tar archive**:
```
nerdctl load [OPTIONS]
```

Options:
- `-i, --input`: Read from a tar archive file

## Network Management

### Working with Networks

**List networks**:
```
nerdctl network ls [OPTIONS]
```

**Create a network**:
```
nerdctl network create [OPTIONS] NETWORK
```

Common options:
- `-d, --driver=(bridge|macvlan|ipvlan)`: Driver to manage the network
- `--subnet`: Subnet in CIDR format (e.g., "10.5.0.0/16")
- `--gateway`: Gateway for the subnet
- `--ipam-driver=(default|host-local|dhcp)`: IP address management driver

**Remove a network**:
```
nerdctl network rm NETWORK [NETWORK...]
```

**Inspect a network**:
```
nerdctl network inspect [OPTIONS] NETWORK [NETWORK...]
```

**Prune networks**:
```
nerdctl network prune [OPTIONS]
```

### Network Types

nerdctl supports the following network types:
- `bridge` (default on Linux): Creates a bridge interface on the host
- `host`: Uses the host's network stack
- `none`: No networking
- `macvlan`: Connects container interfaces directly to host interfaces
- `ipvlan`: Similar to macvlan but shares host's IP address

Example creating a macvlan network:
```bash
nerdctl network create macnet --driver macvlan \
  --subnet=192.168.5.0/24 \
  --gateway=192.168.5.1 \
  -o parent=eth0
```

## Volume Management

### Working with Volumes

**List volumes**:
```
nerdctl volume ls [OPTIONS]
```

**Create a volume**:
```
nerdctl volume create [OPTIONS] [VOLUME]
```

**Remove a volume**:
```
nerdctl volume rm [OPTIONS] VOLUME [VOLUME...]
```

**Inspect a volume**:
```
nerdctl volume inspect [OPTIONS] VOLUME [VOLUME...]
```

**Prune volumes**:
```
nerdctl volume prune [OPTIONS]
```

### Volume Flags for Containers

Volume-related flags when running containers:
- `-v, --volume`: Bind mount a volume (format: `SRC:DST[:OPTIONS]`)
- `--mount`: Attach a filesystem mount to the container
- `--tmpfs`: Mount a tmpfs directory

Volume options:
- `rw`: Read/write (default)
- `ro`: Read-only
- `rro`: Recursive read-only (kernel >= 5.12)
- `shared`, `slave`, `private`: Non-recursive propagation
- `rshared`, `rslave`, `rprivate`: Recursive propagation

Examples:
```bash
# Mount a host directory
nerdctl run -it --rm -v /host/path:/container/path:ro alpine ls /container/path

# Use tmpfs
nerdctl run -it --rm --tmpfs /tmp:size=64m,exec alpine ls /tmp
```

## Compose

nerdctl includes Docker Compose compatibility, allowing you to define and run multi-container applications.

**Run Compose applications**:
```
nerdctl compose up [OPTIONS]
```

Options:
- `-d, --detach`: Run containers in the background
- `--build`: Build images before starting containers
- `--no-build`: Don't build images, even if they're missing
- `--force-recreate`: Force recreation of containers

**Stop Compose applications**:
```
nerdctl compose down [OPTIONS]
```

Options:
- `-v, --volumes`: Remove named volumes and anonymous volumes

**View Compose logs**:
```
nerdctl compose logs [OPTIONS] [SERVICE...]
```

Other Compose commands:
- `nerdctl compose build`: Build service images
- `nerdctl compose ps`: List containers
- `nerdctl compose pull`: Pull service images
- `nerdctl compose exec`: Execute a command in a running container
- `nerdctl compose restart`: Restart services

Example `compose.yml`:
```yaml
version: "3.8"
services:
  web:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:
```

## Rootless Mode

nerdctl supports rootless containers, allowing unprivileged users to create and manage containers. This provides better security isolation compared to running everything as root.

### Setup Rootless Mode

1. Install required dependencies (see https://rootlesscontaine.rs/getting-started/common/)
2. Set up rootless containerd:
   ```
   containerd-rootless-setuptool.sh install
   ```
3. Enable lingering for your user (to keep services running after logout):
   ```
   sudo loginctl enable-linger $(whoami)
   ```
4. For building images, install BuildKit in rootless mode:
   ```
   containerd-rootless-setuptool.sh install-buildkit
   ```

When running in rootless mode, nerdctl automatically uses the appropriate socket and configuration.

### Limitations and Considerations

- Resource limits require cgroup v2 and systemd
- By default, ports below 1024 cannot be published (use slirp4netns port driver or configure capabilities)
- Some file system operations might be restricted
- Network performance can be slower (consider using bypass4netns to improve performance)

## Registry Authentication

nerdctl uses the same authentication configuration as Docker, located in `${DOCKER_CONFIG}/config.json` (default: `$HOME/.docker/config.json`).

**Log in to a registry**:
```
nerdctl login [OPTIONS] [SERVER]
```

Options:
- `-u, --username`: Username
- `-p, --password`: Password
- `--password-stdin`: Take the password from stdin

**Log out from a registry**:
```
nerdctl logout [SERVER]
```

### Registry Certificates

For private registries with custom certificates, place certificates in:
- Rootful: `/etc/containerd/certs.d/<HOST:PORT>/` or `/etc/docker/certs.d/<HOST:PORT>/`
- Rootless: `~/.config/containerd/certs.d/<HOST:PORT>/` or `~/.config/docker/certs.d/<HOST:PORT>/`

## Advanced Features

### GPU Support

nerdctl supports NVIDIA GPU passthrough to containers:

```
nerdctl run -it --rm --gpus all nvidia/cuda:12.3.1-base-ubuntu20.04 nvidia-smi
```

Options for `--gpus`:
- `all`: Use all available GPUs
- Custom configuration: `--gpus '"capabilities=utility,compute",device=GPU-UUID'`

### BuildKit Integration

BuildKit provides advanced image building capabilities:

1. Set up BuildKit (different for rootful and rootless):
   ```
   # Rootless with containerd worker
   CONTAINERD_NAMESPACE=default containerd-rootless-setuptool.sh install-buildkit-containerd
   ```

2. Use advanced build features:
   ```
   nerdctl build --output=type=local,dest=./output --platform=linux/amd64,linux/arm64 .
   ```

### Namespace Management

**Create a namespace**:
```
nerdctl namespace create NAMESPACE
```

**List namespaces**:
```
nerdctl namespace ls
```

**Remove a namespace**:
```
nerdctl namespace remove NAMESPACE
```

### Security Features

nerdctl supports various security features:

- `--security-opt seccomp=profile.json`: Apply a seccomp profile
- `--security-opt apparmor=profile`: Apply an AppArmor profile
- `--cap-add`/`--cap-drop`: Add or drop Linux capabilities
- `--privileged`: Give extended privileges to the container

## Typical Workflow Example

```bash
# Create a container from an existing image
container=$(nerdctl run -d --name my-nginx nginx:latest)

# Execute a command in the container
nerdctl exec $container apt-get update
nerdctl exec $container apt-get install -y curl

# Copy local configuration files to the container
nerdctl cp ./nginx.conf $container:/etc/nginx/nginx.conf

# Commit the container to create a new image
nerdctl commit $container my-custom-nginx:latest

# Stop and remove the container
nerdctl stop $container
nerdctl rm $container

# Create a new container from our custom image
nerdctl run -d --name nginx-custom -p 8080:80 my-custom-nginx:latest

# Build an image using a Dockerfile
nerdctl build -t my-app:latest .

# Push the image to a registry
nerdctl push my-custom-nginx:latest docker.io/username/my-custom-nginx:latest

# List images
nerdctl images

# List containers
nerdctl ps -a
```