Debian Codegraph @

docs/ctr-remote.md

raw
1 # Optimize Images with `ctr-remote image optimize`
2
3 This doc describes example usages of `ctr-remote image optimize` command for converting images into eStargz.
4
5 `ctr-remote images optimize` command (call `ctr-remote` in this doc) enables users to convert an image into eStargz.
6 This command works on containerd so containerd needs to run on your environment.
7 So this converts an image stored in containerd and stores the resulting image to containerd.
8
9 Because the resulting image is stored to containerd, you can use `ctr-remote image pull` and `ctr-remote image push` commands for pulling/pushing images from/to regstries.
10 [nerdctl](https://github.com/containerd/nerdctl), Docker-compatible CLI for containerd, allows you to pull/push images using `~/.docker/config.json`.
11 Various other containerd-based commands like `ctr-remote content get`, `ctr-remote images export` and other `ctr-remote` and `nerdctl` commands can also be used for debugging and inspecting the resulting eStargz image.
12
13 The converted eStargz image can be lazily pulled by Stargz Snapshotter which can speed up the container startup.
14 Because this image is backward compatible to OCI/Docker image, this can be also run by other runtimes that don't support lazy pull (e.g. Docker).
15
16 Though lazy pull speeds up the container's startup, it's possible, especially with slow network, that the runtime performance becomes lower because reading files can induce remotely downloading file contents.
17 For mitigating this, `ctr-remote` also allows to *optimize* the image against the *workload* the image runs.
18 Here, workload means the configuration of the container that runs from that image, including the entrypoint program, environment variables, user etc.
19 This optimization is done by baking the information about files that are likely accessed during runtime (called *prioritized files*), to the image.
20 On runtime, Stargz Snapshotter prefetches these prioritized files before mounting the layer for making sure these files are locally accessible.
21 This can avoid downloading chunks on every file read and mitigate the runtime performance drawbacks.
22
23 :information_source: For more details about eStargz and its optimization, refer also to [eStargz: Standard-Compatible Extensions to Tar.gz Layers for Lazy Pulling Container Images](/docs/stargz-estargz.md).
24
25 :information_source: Please see also [Creating smaller eStargz images](/docs/smaller-estargz.md) if you're interested in creating a smaller size of eStargz images.
26
27 ## Requirements
28
29 - containerd: Release binaries are available on https://github.com/containerd/containerd/releases.
30 - CNI plugins (if network connection is needed during optimization): Release binaries are available on https://github.com/containernetworking/plugins.
31
32 `ctr-remote` requires CAP_SYS_ADMIN.
33 Rootless execution of this command is still WIP.
34
35 For trying the examples described in this doc, you can also use the docker-compose-based demo environment.
36 You can setup this environment as the following commands.
37 *Note that this runs privileged containers on your host.*
38
39 ```
40 $ cd ${GOPATH}/src/github.com/containerd/stargz-snapshotter/script/demo
41 $ docker-compose build containerd_demo
42 $ docker-compose up -d
43 $ docker exec -it containerd_demo /bin/bash
44 (inside container) # ./script/demo/run.sh
45 ```
46
47 ## Optimizing an image
48
49 The following command optimizes an (non-eStargz) image `ghcr.io/stargz-containers/golang:1.15.3-buster-org` (this is a copy of `golang:1.15.3-buster`) and pushes the result eStargz image into `registry2:5000/golang:1.15.3-esgz`.
50 This doesn't append workload-related configuration options (e.g. `--entrypoint`) so this optimizes the image against the default configurations baked to the image e.g. through Dockefile instructions (`ENTRYPOINT`, etc) when building the original image.
51
52 ```
53 ctr-remote image pull ghcr.io/stargz-containers/golang:1.15.3-buster-org
54 ctr-remote image optimize --oci ghcr.io/stargz-containers/golang:1.15.3-buster-org registry2:5000/golang:1.15.3-esgz
55 ctr-remote image push --plain-http registry2:5000/golang:1.15.3-esgz
56 ```
57
58 When you run `ctr-remote image optimize`, this runs the source image (`ghcr.io/stargz-containers/golang:1.15.3-buster-org`) as a container and profiles all file accesses during the execution.
59 Then these accessed files are marked as "prioritized" files and will be prefetched on runtime.
60
61 You can specify the GZIP compression level the converter should use using the `--estargz-compression-level` flag. The values range from 1-9. If the flag isn't provided, the compression level will default to 9.
62
63 A value of 9 indicates the archive will be gzipped with max compression. This will reduce the bytes transferred over the network but increase the CPU cycles required to decompress the payload. Whereas gzip compression value 1 indicates archive will be gzipped with least compression. This will increase the bytes transferred over the network but decreases the CPU cycles required to decompress the payload. This value should be chosen based on the workload and host characteristics.
64
65 The following example optimizes an image with a compression level of 1.
66
67 ```console
68 # ctr-remote image optimize --oci --estargz-compression-level 1 ghcr.io/stargz-containers/golang:1.15.3-buster-org registry2:5000/golang:1.15.3-esgz
69 ```
70
71 You can enable host networking for the container using the `net-host` flag.
72
73 ```console
74 # ctr-remote i optimize -t -i --oci --entrypoint='[ "/bin/bash", "-c" ]' --net-host --args='[ "ip a && curl example.com" ]' ghcr.io/stargz-containers/centos:8-test registry2:5000/centos:8-test-esgz
75 ```
76
77 You can optimize GPU-based images using the `gpu` flag. The flag expects a comma separated list of integers.
78
79 ```console
80 # ctr-remote i optimize --oci --gpus "0" <src> <target>
81 ```
82
83 `--oci` option is highly recommended to add when you create eStargz image.
84 If the source image is [Docker image](https://github.com/moby/moby/blob/master/image/spec/v1.2.md) that doesn't allow us [content verification of eStargz](/docs/verification.md), `ctr-remote` converts this image into the [OCI starndard compliant image](https://github.com/opencontainers/image-spec/).
85 OCI image also can run on most of modern container runtimes.
86
87 You can lazy-pull this image into other hosts with Stargz Snapshotter.
88 The following example lazily pulls this image to containerd, using `ctr-remote image rpull` command.
89
90 ```console
91 # ctr-remote image rpull --plain-http registry2:5000/golang:1.15.3-esgz
92 fetching sha256:9f9b5a43... application/vnd.oci.image.index.v1+json
93 fetching sha256:16debc17... application/vnd.oci.image.manifest.v1+json
94 fetching sha256:a610ec55... application/vnd.oci.image.config.v1+json
95 # ctr-remote run --rm -t --snapshotter=stargz registry2:5000/golang:1.15.3-esgz test echo hello
96 hello
97 ```
98
99 In the following examples, we omit `ctr-remote image pull` and `ctr-remote image push` from the example.
100
101 ## Optimizing an image with custom configuration
102
103 You can also specify the custom workload configuration that the image is optimized against.
104 The following example optimizes the image against the workload running `go version` on `/bin/bash`.
105
106 ```
107 ctr-remote image optimize --oci \
108 --entrypoint='[ "/bin/bash", "-c" ]' --args='[ "go version" ]' \
109 ghcr.io/stargz-containers/golang:1.15.3-buster-org \
110 registry2:5000/golang:1.15.3-esgz-go-version
111 ```
112
113 Other options are also available for configuring the workload.
114
115 |Option|Description|
116 ---|---
117 |`--entrypoint`|Entrypoint of the container (in JSON array)|
118 |`--args`|Arguments for the entrypoint (in JSON array)|
119 |`--env`|Environment variables in the container|
120 |`--user`|User name to run the process in the container|
121 |`--cwd`|Working directory|
122 |`--period`|The time seconds during profiling the file accesses|
123 |`-t`or`--terminal`|Attach terminal to the container. This flag must be specified with `-i`|
124 |`-i`|Attach stdin to the container|
125
126 ## Mounting files from the host
127
128 There are several cases where sharing files from host to the container during optimization is useful.
129 This includes when we want to optimize an image against building a binary using compilers.
130
131 For these use-cases, you can mount files on the hosts to the container using `--mount` option.
132 The following example optimizes the image against the workload where Go compiler compiles "hello world" program.
133
134 First, create the following Go source file at `/tmp/hello.go`.
135
136 ```golang
137 package main
138
139 import "fmt"
140
141 func main() {
142 fmt.Println("hello world")
143 }
144 ```
145
146 Then you can build it inside the container by bind-mounting the file `/tmp/hello.go` on the host to this container.
147
148 ```
149 ctr-remote image optimize --oci \
150 --mount=type=bind,source=/tmp/hello.go,destination=/hello.go,options=bind:ro \
151 --entrypoint='[ "/bin/bash", "-c" ]' --args='[ "go build -o /hello /hello.go && /hello" ]' \
152 ghcr.io/stargz-containers/golang:1.15.3-buster-org \
153 registry2:5000/golang:1.15.3-esgz-hello-world
154 ```
155
156 The syntax of the `--mount` option is compatible to containerd's `ctr` tool and [corresponds to the OCI Runtime Spec](https://github.com/opencontainers/runtime-spec/blob/v1.0.2/config.md#mounts).
157 You need to specify the following key-value pairs with comma separators.
158
159 |Field|Description|
160 ---|---
161 |`type`|The type of the filesystem to be mounted|
162 |`destination`|The absolute path to the destination mount point in the container|
163 |`source`|The source of the mount|
164 |`options`|Mount options (separated by `:`) of the filesystem|
165
166 ## Enabling CNI-based Networking
167
168 You can also gain network connection in the container during optimization, using CNI plugins.
169 Once you configure CNI plugins on the host, CNI-based networking can be enabled using `--cni` option.
170
171 The following example accesses to https://example.com from the container, using `bridge` CNI plugin installed in the demo environment.
172 ```
173 ctr-remote image optimize --oci \
174 --cni \
175 --entrypoint='[ "/bin/bash", "-c" ]' \
176 --args='[ "curl example.com" ]' \
177 ghcr.io/stargz-containers/golang:1.15.3-buster-org \
178 registry2:5000/golang:1.15.3-esgz-curl
179 ```
180
181 If CNI plugins and configurations are installed to locations other than well-known paths (/opt/cni/bin and /etc/cni/net.d), you can tell it to `ctr-remote` using the following options.
182
183 |Option|Description|
184 ---|---
185 |`--cni-plugin-config-dir`|Path to the directory where CNI plugins configurations are stored|
186 |`--cni-plugin-dir`|Path to the directory where CNI plugins binaries are installed|
187
188 By default, `/etc/hosts` and `/etc/resolv.conf` are configured as the following.
189
190 /etc/hosts
191
192 ```
193 127.0.0.1 localhost
194 ::1 localhost ip6-localhost ip6-loopback
195 fe00::0 ip6-localnet
196 ff00::0 ip6-mcastprefix
197 ff02::1 ip6-allnodes
198 ff02::2 ip6-allrouters
199 ```
200
201 /etc/resolv.conf
202
203 ```
204 nameserver 8.8.8.8
205 ```
206
207 If you want to customize the configuration, the following options are useful.
208
209 |Option|Description|
210 ---|---
211 |`--add-hosts`|Commma-separated list of hosts configuration (formatted as `hostname:IP`) for `/etc/hosts`|
212 |`--dns-nameservers`|Comma-separated `nameserver` configs added to the container's `/etc/resolv.conf`|
213 |`--dns-search-domains`|Comma-separated `search` configs added to the container's `/etc/resolv.conf`|
214 |`--dns-options`|Comma-separated `options` configs added to the container's `/etc/resolv.conf`|
215
216 ## Other useful features
217
218 ### Reusing already-converted layers
219
220 If the source image is large, its conversion takes accordingly long time.
221 But you can skip conversion for layers that are already converted to eStargz.
222 For enabling this feature, add `--reuse` option to `ctr-remote`.
223
224 The following example re-converts already converted eStargz image (`ghcr.io/stargz-containers/golang:1.15.3-buster-esgz`) with `--reuse` feature.
225
226 ```
227 ctr-remote image optimize --oci \
228 --reuse \
229 ghcr.io/stargz-containers/golang:1.15.3-buster-esgz \
230 registry2:5000/golang:1.15.3-esgz
231 ```
232
233 You will see `ctr-remote` skips converting some layers with printing `copying without conversion` log messages as the following.
234
235 ```
236 (... omit ...)
237 WARN[0036] reusing "sha256:4416ecf7e2787af750fe3b1988f36a2c47edc2d3162739c6eedd739d6d5a14d1" without conversion
238 WARN[0036] reusing "sha256:976cc0da952505fede3abe08c0ff0c5277416828c4dff8bd01b306c5b4e5c6f5" without conversion
239 WARN[0036] reusing "sha256:59cf7266511a915072804370a3083a1007c4fb757d800ceef848032ac4a5b605" without conversion
240 WARN[0036] reusing "sha256:ca2a1da2dee341a3b87a14d56603e9c29c66721056a47bec156f9b04ee0b1e5e" without conversion
241 WARN[0036] reusing "sha256:79d28aed10b15d548b63eea4cc59e518c4939f9c8fb8498100ec658fc7e0baca" without conversion
242 WARN[0036] reusing "sha256:6aedf0c74720e30b9093dc0d2b39c2dd88f35ead14e2087bb49c1608bb151e61" without conversion
243 (... omit ...)
244 ```
245
246 When optimizing an image, `ctr-remote` tries to avoid layer conversion as much as possible.
247 The layers that meet the following conditions are skipped to converting.
248
249 - layers that are already formatted as eStargz
250 - layers that no file access occurred during optimization
251
252 ### Converting multi-platform images
253
254 You can also convert multi-platform images.
255 If you want to convert all images contained in a multi-platform image, use `--all-platform` option.
256 If you want to convert an image corresponding to a specific platform, tell it using `--platform` option.
257 The format of the `--platform` option is `<os>|<arch>|<os>/<arch>[/<variant>]`, please refer to containerd's [godoc](https://godoc.org/github.com/containerd/containerd/platforms#hdr-Platform_Specifiers) for more details.
258
259 The following example converts all images contained in `ghcr.io/stargz-containers/golang:1.15.3-buster-org`.
260
261 ```
262 ctr-remote image optimize --oci \
263 --all-platforms \
264 ghcr.io/stargz-containers/golang:1.15.3-buster-org \
265 registry2:5000/golang:1.15.3-esgz-fat
266 ```
267
268 By default, when the source image is a multi-platform image, `ctr-remote` converts the image corresponding to the platform where `ctr-remote` runs.
269
270 Note that though the images specified by `--all-platform` and `--platform` are converted to eStargz, images that don't correspond to the current platform aren't *optimized*. That is, these images are lazily pulled but without prefetch.