Debian Codegraph @

docs/ipfs.md

raw
1 # Running containers on IPFS (experimental)
2
3 :information_source: This document isn't for Kubernetes environemnt. For information about node-to-node image sharing on Kubernetes, please refer to [the docs in nerdctl project](https://github.com/containerd/nerdctl/tree/main/examples/nerdctl-ipfs-registry-kubernetes).
4
5 You can run OCI-compatible container images on IPFS with lazy pulling.
6
7 To enable this feature, add the following configuration to `config.toml` of Stargz Snapsohtter (typically located at `/etc/containerd-stargz-grpc/config.toml`).
8
9 ```toml
10 ipfs = true
11 ```
12
13 > NOTE: containerd-stargz-grpc tries to connect to IPFS API written in `~/.ipfs/api` (or the file under `$IPFS_PATH` if configured) via HTTP (not HTTPS).
14
15 ## IPFS-enabled OCI Image
16
17 For obtaining IPFS-enabled OCI Image, each descriptor in an OCI image must contain the following [IPFS URL](https://docs.ipfs.io/how-to/address-ipfs-on-web/#native-urls) in `urls` field.
18
19 ```
20 ipfs://<CID>
21 ```
22
23 `<CID>` is the Base32 case-insensitive CIDv1 of the blob that the descriptor points to.
24
25 An image is represented as a CID pointing to the OCI descriptor of the top-level blob of the image (i.e. image index).
26
27 The following is an example OCI descriptor pointing to the image index of an IPFS-enabled image:
28
29 ```console
30 # ipfs cat bafkreie7754qk7fl56ebauawdgfuqqa3kdd7sotvuhsm6wbz3qin6ssw3a | jq
31 {
32 "mediaType": "application/vnd.oci.image.index.v1+json",
33 "digest": "sha256:80d6aec48c0a74635a5f3dc106328c1673afaa21ed6e1270a9a44de66e8ffa55",
34 "size": 314,
35 "urls": [
36 "ipfs://bafkreiea22xmjdakorrvuxz5yeddfdawoox2uipnnyjhbknejxtg5d72ku"
37 ]
38 }
39 ```
40
41 ## Lazy pulling with Stargz Snapshotter
42
43 If layer descriptors of an image contain the URLs described above and these blobs are formatted as eStargz, Stargz Snapshotter mounts them from IPFS to the container's rootfs using FUSE with lazy pulling support.
44 Thus container can startup without waiting for the entire image contents being locally available.
45 Necessary chunks of contents (e.g. each file in the rootfs) are fetched from IPFS on-demand.
46
47 If the container image isn't eStargz or the snapshotter isn't Stargz Snapshotter (e.g. overlayfs snapshotter), containerd fetches the entire image contents from IPFS and unpacks it to the local directory before starting the container.
48 Thus possibly you'll see slow container cold-start.
49
50 ## Examples
51
52 This section describes some examples of storing images to IPFS and running them as containers.
53 Make sure IPFS daemon runs on your node.
54 For example, you can run an IPFS daemon using the following command.
55
56 ```
57 ipfs daemon
58 ```
59
60 :information_source: If you don't want IPFS to communicate with nodes on the internet, you can run IPFS daemon in offline mode using `--offline` flag or you can create a private IPFS network as described in Appendix 1.
61
62 ### Running a container with lazy pulling
63
64 `ctr-remote image ipfs-push` command converts an image to IPFS-enabled eStargz and stores it to IPFS.
65
66 ```console
67 # ctr-remote i pull ghcr.io/stargz-containers/python:3.9-org
68 # ctr-remote i ipfs-push ghcr.io/stargz-containers/python:3.9-org
69 bafkreie7754qk7fl56ebauawdgfuqqa3kdd7sotvuhsm6wbz3qin6ssw3a
70 ```
71
72 The printed IPFS CID (`bafkreie7754qk7fl56ebauawdgfuqqa3kdd7sotvuhsm6wbz3qin6ssw3a`) points to an OCI descriptor which points to the image index of the added image.
73
74 ```console
75 # ipfs cat bafkreie7754qk7fl56ebauawdgfuqqa3kdd7sotvuhsm6wbz3qin6ssw3a | jq
76 {
77 "mediaType": "application/vnd.oci.image.index.v1+json",
78 "digest": "sha256:80d6aec48c0a74635a5f3dc106328c1673afaa21ed6e1270a9a44de66e8ffa55",
79 "size": 314,
80 "urls": [
81 "ipfs://bafkreiea22xmjdakorrvuxz5yeddfdawoox2uipnnyjhbknejxtg5d72ku"
82 ]
83 }
84 ```
85
86 You can run this image from IPFS using that CID as an image reference for `ctr-remote image rpull`.
87 `--ipfs` option is needed for enabling this.
88
89 Note that `ctr-remote` accepts an IPFS CID as the image reference but doesn't support `/ipfs`-prefixed path as of now.
90 We're working on eliminating this limitation.
91
92 ```console
93 # time ( ctr-remote i rpull --ipfs bafkreie7754qk7fl56ebauawdgfuqqa3kdd7sotvuhsm6wbz3qin6ssw3a && \
94 ctr-remote run --snapshotter=stargz --rm -t bafkreie7754qk7fl56ebauawdgfuqqa3kdd7sotvuhsm6wbz3qin6ssw3a foo python -c 'print("Hello, World!")' )
95 fetching sha256:80d6aec4... application/vnd.oci.image.index.v1+json
96 fetching sha256:16d36f86... application/vnd.oci.image.manifest.v1+json
97 fetching sha256:236b4bd7... application/vnd.oci.image.config.v1+json
98 Hello, World!
99
100 real 0m1.099s
101 user 0m0.047s
102 sys 0m0.037s
103 ```
104
105 ### Running a container without lazy pulling
106
107 Though eStargz-based lazy pulling is highly recommended for speeding up the container startup time, you can store and run non-eStargz images with IPFS as well.
108 In this case, containerd fetches the entire image contents from IPFS and unpacks it to the local directory before starting the container.
109
110 You can add a non-eStargz image to IPFS using `--estargz=false` option.
111
112 ```console
113 # ctr-remote i pull ghcr.io/stargz-containers/python:3.9-org
114 # ctr-remote i ipfs-push --estargz=false ghcr.io/stargz-containers/python:3.9-org
115 bafkreienbir4knaofs3o5f57kqw2the2v7zdhdlzpkq346mipuopwvqhty
116 ```
117
118 You don't need FUSE nor stargz snapshotter for running this image but will see slow container cold-start.
119 This example uses overlayfs snapshotter of containerd.
120
121 ```console
122 # time ( ctr-remote i rpull --snapshotter=overlayfs --ipfs bafkreienbir4knaofs3o5f57kqw2the2v7zdhdlzpkq346mipuopwvqhty && \
123 ctr-remote run --snapshotter=overlayfs --rm -t bafkreienbir4knaofs3o5f57kqw2the2v7zdhdlzpkq346mipuopwvqhty foo python -c 'print("Hello, World!")' )
124 fetching sha256:7240ac9f... application/vnd.oci.image.index.v1+json
125 fetching sha256:17dc54f4... application/vnd.oci.image.manifest.v1+json
126 fetching sha256:6f1289b1... application/vnd.oci.image.config.v1+json
127 fetching sha256:9476e460... application/vnd.oci.image.layer.v1.tar+gzip
128 fetching sha256:64c0f10e... application/vnd.oci.image.layer.v1.tar+gzip
129 fetching sha256:4c25b309... application/vnd.oci.image.layer.v1.tar+gzip
130 fetching sha256:942374d5... application/vnd.oci.image.layer.v1.tar+gzip
131 fetching sha256:3fff52a3... application/vnd.oci.image.layer.v1.tar+gzip
132 fetching sha256:5cf06daf... application/vnd.oci.image.layer.v1.tar+gzip
133 fetching sha256:419e258e... application/vnd.oci.image.layer.v1.tar+gzip
134 fetching sha256:1acf5650... application/vnd.oci.image.layer.v1.tar+gzip
135 fetching sha256:b95c0dd0... application/vnd.oci.image.layer.v1.tar+gzip
136 Hello, World!
137
138 real 0m11.320s
139 user 0m0.556s
140 sys 0m0.280s
141 ```
142
143 ## Appendix 1: Creating IPFS private network
144
145 You can create a private IPFS network as described in the official docs.
146
147 - https://github.com/ipfs/go-ipfs/blob/v0.10.0/docs/experimental-features.md#private-networks
148
149 The following is the summary.
150
151 First, generate a key and save it to `~/.ipfs/swarm.key` (or under `$IPFS_PATH` if configured) of nodes you want to have in the network.
152 IPFS only connects to peers having this key.
153
154 ```
155 go install github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen@latest
156 ~/go/bin/ipfs-swarm-key-gen > ~/.ipfs/swarm.key
157 ```
158
159 Select nodes as a bootstrap nodes.
160 IPFS daemons learn about the peers on the private network from them.
161 Configure all non-bootstrap nodes to recognize only our bootstrap nodes instead of public ones like the following example.
162
163 ```
164 ipfs bootstrap rm --all
165 ipfs bootstrap add /ip4/<ip address of bootstrap node>/tcp/4001/ipfs/<Peer ID of the bootstrap node>
166 ```
167
168 :information_source: You can get Peer ID of a node by `ipfs config show | grep "PeerID"`.
169
170 Finally, start all nodes in the private network.
171
172 ```
173 export LIBP2P_FORCE_PNET=1
174 ipfs daemon
175 ```
176
177 `LIBP2P_FORCE_PNET=1` makes sure that the daemon uses the private network and fails if the private network isn't configured.