@@ -13,567 +13,5 @@
1313 <xi:include href="functions/debug.xml" />
1414 <xi:include href="functions/fhs-environments.xml" />
1515 <xi:include href="shell.section.xml" />
16− <section xml:id="sec-pkgs-dockerTools">
17− <title>pkgs.dockerTools</title>
18−
19− <para>
20− <varname>pkgs.dockerTools</varname> is a set of functions for creating and
21− manipulating Docker images according to the
22− <link xlink:href="https://github.com/moby/moby/blob/master/image/spec/v1.2.md#docker-image-specification-v120">
23− Docker Image Specification v1.2.0 </link>. Docker itself is not used to
24− perform any of the operations done by these functions.
25− </para>
26−
27− <warning>
28− <para>
29− The <varname>dockerTools</varname> API is unstable and may be subject to
30− backwards-incompatible changes in the future.
31− </para>
32− </warning>
33−
34− <section xml:id="ssec-pkgs-dockerTools-buildImage">
35− <title>buildImage</title>
36−
37− <para>
38− This function is analogous to the <command>docker build</command> command,
39− in that can used to build a Docker-compatible repository tarball containing
40− a single image with one or multiple layers. As such, the result is suitable
41− for being loaded in Docker with <command>docker load</command>.
42− </para>
43−
44− <para>
45− The parameters of <varname>buildImage</varname> with relative example
46− values are described below:
47− </para>
48−
49− <example xml:id='ex-dockerTools-buildImage'>
50− <title>Docker build</title>
51−<programlisting>
52−buildImage {
53− name = "redis"; <co xml:id='ex-dockerTools-buildImage-1' />
54− tag = "latest"; <co xml:id='ex-dockerTools-buildImage-2' />
55−
56− fromImage = someBaseImage; <co xml:id='ex-dockerTools-buildImage-3' />
57− fromImageName = null; <co xml:id='ex-dockerTools-buildImage-4' />
58− fromImageTag = "latest"; <co xml:id='ex-dockerTools-buildImage-5' />
59−
60− contents = pkgs.redis; <co xml:id='ex-dockerTools-buildImage-6' />
61− runAsRoot = '' <co xml:id='ex-dockerTools-buildImage-runAsRoot' />
62− #!${stdenv.shell}
63− mkdir -p /data
64− '';
65−
66− config = { <co xml:id='ex-dockerTools-buildImage-8' />
67− Cmd = [ "/bin/redis-server" ];
68− WorkingDir = "/data";
69− Volumes = {
70− "/data" = {};
71− };
72− };
73−}
74−</programlisting>
75− </example>
76−
77− <para>
78− The above example will build a Docker image <literal>redis/latest</literal>
79− from the given base image. Loading and running this image in Docker results
80− in <literal>redis-server</literal> being started automatically.
81− </para>
82−
83− <calloutlist>
84− <callout arearefs='ex-dockerTools-buildImage-1'>
85− <para>
86− <varname>name</varname> specifies the name of the resulting image. This
87− is the only required argument for <varname>buildImage</varname>.
88− </para>
89− </callout>
90− <callout arearefs='ex-dockerTools-buildImage-2'>
91− <para>
92− <varname>tag</varname> specifies the tag of the resulting image. By
93− default it's <literal>null</literal>, which indicates that the nix output
94− hash will be used as tag.
95− </para>
96− </callout>
97− <callout arearefs='ex-dockerTools-buildImage-3'>
98− <para>
99− <varname>fromImage</varname> is the repository tarball containing the
100− base image. It must be a valid Docker image, such as exported by
101− <command>docker save</command>. By default it's <literal>null</literal>,
102− which can be seen as equivalent to <literal>FROM scratch</literal> of a
103− <filename>Dockerfile</filename>.
104− </para>
105− </callout>
106− <callout arearefs='ex-dockerTools-buildImage-4'>
107− <para>
108− <varname>fromImageName</varname> can be used to further specify the base
109− image within the repository, in case it contains multiple images. By
110− default it's <literal>null</literal>, in which case
111− <varname>buildImage</varname> will peek the first image available in the
112− repository.
113− </para>
114− </callout>
115− <callout arearefs='ex-dockerTools-buildImage-5'>
116− <para>
117− <varname>fromImageTag</varname> can be used to further specify the tag of
118− the base image within the repository, in case an image contains multiple
119− tags. By default it's <literal>null</literal>, in which case
120− <varname>buildImage</varname> will peek the first tag available for the
121− base image.
122− </para>
123− </callout>
124− <callout arearefs='ex-dockerTools-buildImage-6'>
125− <para>
126− <varname>contents</varname> is a derivation that will be copied in the
127− new layer of the resulting image. This can be similarly seen as
128− <command>ADD contents/ /</command> in a <filename>Dockerfile</filename>.
129− By default it's <literal>null</literal>.
130− </para>
131− </callout>
132− <callout arearefs='ex-dockerTools-buildImage-runAsRoot'>
133− <para>
134− <varname>runAsRoot</varname> is a bash script that will run as root in an
135− environment that overlays the existing layers of the base image with the
136− new resulting layer, including the previously copied
137− <varname>contents</varname> derivation. This can be similarly seen as
138− <command>RUN ...</command> in a <filename>Dockerfile</filename>.
139− <note>
140− <para>
141− Using this parameter requires the <literal>kvm</literal> device to be
142− available.
143− </para>
144− </note>
145− </para>
146− </callout>
147− <callout arearefs='ex-dockerTools-buildImage-8'>
148− <para>
149− <varname>config</varname> is used to specify the configuration of the
150− containers that will be started off the built image in Docker. The
151− available options are listed in the
152− <link xlink:href="https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions">
153− Docker Image Specification v1.2.0 </link>.
154− </para>
155− </callout>
156− </calloutlist>
157−
158− <para>
159− After the new layer has been created, its closure (to which
160− <varname>contents</varname>, <varname>config</varname> and
161− <varname>runAsRoot</varname> contribute) will be copied in the layer
162− itself. Only new dependencies that are not already in the existing layers
163− will be copied.
164− </para>
165−
166− <para>
167− At the end of the process, only one new single layer will be produced and
168− added to the resulting image.
169− </para>
170−
171− <para>
172− The resulting repository will only list the single image
173− <varname>image/tag</varname>. In the case of
174− <xref linkend='ex-dockerTools-buildImage'/> it would be
175− <varname>redis/latest</varname>.
176− </para>
177−
178− <para>
179− It is possible to inspect the arguments with which an image was built using
180− its <varname>buildArgs</varname> attribute.
181− </para>
182−
183− <note>
184− <para>
185− If you see errors similar to <literal>getProtocolByName: does not exist
186− (no such protocol name: tcp)</literal> you may need to add
187− <literal>pkgs.iana-etc</literal> to <varname>contents</varname>.
188− </para>
189− </note>
190−
191− <note>
192− <para>
193− If you see errors similar to <literal>Error_Protocol ("certificate has
194− unknown CA",True,UnknownCa)</literal> you may need to add
195− <literal>pkgs.cacert</literal> to <varname>contents</varname>.
196− </para>
197− </note>
198−
199− <example xml:id="example-pkgs-dockerTools-buildImage-creation-date">
200− <title>Impurely Defining a Docker Layer's Creation Date</title>
201− <para>
202− By default <function>buildImage</function> will use a static
203− date of one second past the UNIX Epoch. This allows
204− <function>buildImage</function> to produce binary reproducible
205− images. When listing images with <command>docker list
206− images</command>, the newly created images will be listed like
207− this:
208− </para>
209− <screen><![CDATA[
210−$ docker image list
211−REPOSITORY TAG IMAGE ID CREATED SIZE
212−hello latest 08c791c7846e 48 years ago 25.2MB
213−]]></screen>
214− <para>
215− You can break binary reproducibility but have a sorted,
216− meaningful <literal>CREATED</literal> column by setting
217− <literal>created</literal> to <literal>now</literal>.
218− </para>
219− <programlisting><![CDATA[
220−pkgs.dockerTools.buildImage {
221− name = "hello";
222− tag = "latest";
223− created = "now";
224− contents = pkgs.hello;
225−
226− config.Cmd = [ "/bin/hello" ];
227−}
228−]]></programlisting>
229− <para>
230− and now the Docker CLI will display a reasonable date and
231− sort the images as expected:
232− <screen><![CDATA[
233−$ docker image list
234−REPOSITORY TAG IMAGE ID CREATED SIZE
235−hello latest de2bf4786de6 About a minute ago 25.2MB
236−]]></screen>
237− however, the produced images will not be binary reproducible.
238− </para>
239− </example>
240− </section>
241−
242− <section xml:id="ssec-pkgs-dockerTools-buildLayeredImage">
243− <title>buildLayeredImage</title>
244−
245− <para>
246− Create a Docker image with many of the store paths being on their own layer
247− to improve sharing between images.
248− </para>
249−
250− <variablelist>
251− <varlistentry>
252− <term>
253− <varname>name</varname>
254− </term>
255− <listitem>
256− <para>
257− The name of the resulting image.
258− </para>
259− </listitem>
260− </varlistentry>
261− <varlistentry>
262− <term>
263− <varname>tag</varname> <emphasis>optional</emphasis>
264− </term>
265− <listitem>
266− <para>
267− Tag of the generated image.
268− </para>
269− <para>
270− <emphasis>Default:</emphasis> the output path's hash
271− </para>
272− </listitem>
273− </varlistentry>
274− <varlistentry>
275− <term>
276− <varname>contents</varname> <emphasis>optional</emphasis>
277− </term>
278− <listitem>
279− <para>
280− Top level paths in the container. Either a single derivation, or a list
281− of derivations.
282− </para>
283− <para>
284− <emphasis>Default:</emphasis> <literal>[]</literal>
285− </para>
286− </listitem>
287− </varlistentry>
288− <varlistentry>
289− <term>
290− <varname>config</varname> <emphasis>optional</emphasis>
291− </term>
292− <listitem>
293− <para>
294− Run-time configuration of the container. A full list of the options are
295− available at in the
296− <link xlink:href="https://github.com/moby/moby/blob/master/image/spec/v1.2.md#image-json-field-descriptions">
297− Docker Image Specification v1.2.0 </link>.
298− </para>
299− <para>
300− <emphasis>Default:</emphasis> <literal>{}</literal>
301− </para>
302− </listitem>
303− </varlistentry>
304− <varlistentry>
305− <term>
306− <varname>created</varname> <emphasis>optional</emphasis>
307− </term>
308− <listitem>
309− <para>
310− Date and time the layers were created. Follows the same
311− <literal>now</literal> exception supported by
312− <literal>buildImage</literal>.
313− </para>
314− <para>
315− <emphasis>Default:</emphasis> <literal>1970-01-01T00:00:01Z</literal>
316− </para>
317− </listitem>
318− </varlistentry>
319− <varlistentry>
320− <term>
321− <varname>maxLayers</varname> <emphasis>optional</emphasis>
322− </term>
323− <listitem>
324− <para>
325− Maximum number of layers to create.
326− </para>
327− <para>
328− <emphasis>Default:</emphasis> <literal>24</literal>
329− </para>
330− </listitem>
331− </varlistentry>
332− </variablelist>
333−
334− <section xml:id="dockerTools-buildLayeredImage-arg-contents">
335− <title>Behavior of <varname>contents</varname> in the final image</title>
336−
337− <para>
338− Each path directly listed in <varname>contents</varname> will have a
339− symlink in the root of the image.
340− </para>
341−
342− <para>
343− For example:
344−<programlisting><![CDATA[
345−pkgs.dockerTools.buildLayeredImage {
346− name = "hello";
347− contents = [ pkgs.hello ];
348−}
349−]]></programlisting>
350− will create symlinks for all the paths in the <literal>hello</literal>
351− package:
352−<screen><![CDATA[
353−/bin/hello -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/bin/hello
354−/share/info/hello.info -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/info/hello.info
355−/share/locale/bg/LC_MESSAGES/hello.mo -> /nix/store/h1zb1padqbbb7jicsvkmrym3r6snphxg-hello-2.10/share/locale/bg/LC_MESSAGES/hello.mo
356−]]></screen>
357− </para>
358− </section>
359−
360− <section xml:id="dockerTools-buildLayeredImage-arg-config">
361− <title>Automatic inclusion of <varname>config</varname> references</title>
362−
363− <para>
364− The closure of <varname>config</varname> is automatically included in the
365− closure of the final image.
366− </para>
367−
368− <para>
369− This allows you to make very simple Docker images with very little code.
370− This container will start up and run <command>hello</command>:
371−<programlisting><![CDATA[
372−pkgs.dockerTools.buildLayeredImage {
373− name = "hello";
374− config.Cmd = [ "${pkgs.hello}/bin/hello" ];
375−}
376−]]></programlisting>
377− </para>
378− </section>
379−
380− <section xml:id="dockerTools-buildLayeredImage-arg-maxLayers">
381− <title>Adjusting <varname>maxLayers</varname></title>
382−
383− <para>
384− Increasing the <varname>maxLayers</varname> increases the number of layers
385− which have a chance to be shared between different images.
386− </para>
387−
388− <para>
389− Modern Docker installations support up to 128 layers, however older
390− versions support as few as 42.
391− </para>
392−
393− <para>
394− If the produced image will not be extended by other Docker builds, it is
395− safe to set <varname>maxLayers</varname> to <literal>128</literal>.
396− However it will be impossible to extend the image further.
397− </para>
398−
399− <para>
400− The first (<literal>maxLayers-2</literal>) most "popular" paths will have
401− their own individual layers, then layer #<literal>maxLayers-1</literal>
402− will contain all the remaining "unpopular" paths, and finally layer
403− #<literal>maxLayers</literal> will contain the Image configuration.
404− </para>
405−
406− <para>
407− Docker's Layers are not inherently ordered, they are content-addressable
408− and are not explicitly layered until they are composed in to an Image.
409− </para>
410− </section>
411− </section>
412−
413− <section xml:id="ssec-pkgs-dockerTools-fetchFromRegistry">
414− <title>pullImage</title>
415−
416− <para>
417− This function is analogous to the <command>docker pull</command> command,
418− in that can be used to pull a Docker image from a Docker registry. By
419− default <link xlink:href="https://hub.docker.com/">Docker Hub</link> is
420− used to pull images.
421− </para>
422−
423− <para>
424− Its parameters are described in the example below:
425− </para>
426−
427− <example xml:id='ex-dockerTools-pullImage'>
428− <title>Docker pull</title>
429−<programlisting>
430−pullImage {
431− imageName = "nixos/nix"; <co xml:id='ex-dockerTools-pullImage-1' />
432− imageDigest = "sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b"; <co xml:id='ex-dockerTools-pullImage-2' />
433− finalImageTag = "1.11"; <co xml:id='ex-dockerTools-pullImage-3' />
434− sha256 = "0mqjy3zq2v6rrhizgb9nvhczl87lcfphq9601wcprdika2jz7qh8"; <co xml:id='ex-dockerTools-pullImage-4' />
435− os = "linux"; <co xml:id='ex-dockerTools-pullImage-5' />
436− arch = "x86_64"; <co xml:id='ex-dockerTools-pullImage-6' />
437−}
438−</programlisting>
439− </example>
440−
441− <calloutlist>
442− <callout arearefs='ex-dockerTools-pullImage-1'>
443− <para>
444− <varname>imageName</varname> specifies the name of the image to be
445− downloaded, which can also include the registry namespace (e.g.
446− <literal>nixos</literal>). This argument is required.
447− </para>
448− </callout>
449− <callout arearefs='ex-dockerTools-pullImage-2'>
450− <para>
451− <varname>imageDigest</varname> specifies the digest of the image to be
452− downloaded. Skopeo can be used to get the digest of an image, with its
453− <varname>inspect</varname> subcommand. Since a given
454− <varname>imageName</varname> may transparently refer to a manifest list
455− of images which support multiple architectures and/or operating systems,
456− supply the `--override-os` and `--override-arch` arguments to specify
457− exactly which image you want. By default it will match the OS and
458− architecture of the host the command is run on.
459−<programlisting>
460−$ nix-shell --packages skopeo jq --command "skopeo --override-os linux --override-arch x86_64 inspect docker://docker.io/nixos/nix:1.11 | jq -r '.Digest'"
461−sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b
462−</programlisting>
463− This argument is required.
464− </para>
465− </callout>
466− <callout arearefs='ex-dockerTools-pullImage-3'>
467− <para>
468− <varname>finalImageTag</varname>, if specified, this is the tag of the
469− image to be created. Note it is never used to fetch the image since we
470− prefer to rely on the immutable digest ID. By default it's
471− <literal>latest</literal>.
472− </para>
473− </callout>
474− <callout arearefs='ex-dockerTools-pullImage-4'>
475− <para>
476− <varname>sha256</varname> is the checksum of the whole fetched image.
477− This argument is required.
478− </para>
479− </callout>
480− <callout arearefs='ex-dockerTools-pullImage-5'>
481− <para>
482− <varname>os</varname>, if specified, is the operating system of the
483− fetched image. By default it's <literal>linux</literal>.
484− </para>
485− </callout>
486− <callout arearefs='ex-dockerTools-pullImage-6'>
487− <para>
488− <varname>arch</varname>, if specified, is the cpu architecture of the
489− fetched image. By default it's <literal>x86_64</literal>.
490− </para>
491− </callout>
492− </calloutlist>
493− </section>
494−
495− <section xml:id="ssec-pkgs-dockerTools-exportImage">
496− <title>exportImage</title>
497−
498− <para>
499− This function is analogous to the <command>docker export</command> command,
500− in that can used to flatten a Docker image that contains multiple layers.
501− It is in fact the result of the merge of all the layers of the image. As
502− such, the result is suitable for being imported in Docker with
503− <command>docker import</command>.
504− </para>
505−
506− <note>
507− <para>
508− Using this function requires the <literal>kvm</literal> device to be
509− available.
510− </para>
511− </note>
512−
513− <para>
514− The parameters of <varname>exportImage</varname> are the following:
515− </para>
516−
517− <example xml:id='ex-dockerTools-exportImage'>
518− <title>Docker export</title>
519−<programlisting>
520−exportImage {
521− fromImage = someLayeredImage;
522− fromImageName = null;
523− fromImageTag = null;
524−
525− name = someLayeredImage.name;
526−}
527− </programlisting>
528− </example>
529−
530− <para>
531− The parameters relative to the base image have the same synopsis as
532− described in <xref linkend='ssec-pkgs-dockerTools-buildImage'/>, except
533− that <varname>fromImage</varname> is the only required argument in this
534− case.
535− </para>
536−
537− <para>
538− The <varname>name</varname> argument is the name of the derivation output,
539− which defaults to <varname>fromImage.name</varname>.
540− </para>
541− </section>
542−
543− <section xml:id="ssec-pkgs-dockerTools-shadowSetup">
544− <title>shadowSetup</title>
545−
546− <para>
547− This constant string is a helper for setting up the base files for managing
548− users and groups, only if such files don't exist already. It is suitable
549− for being used in a <varname>runAsRoot</varname>
550− <xref linkend='ex-dockerTools-buildImage-runAsRoot'/> script for cases like
551− in the example below:
552− </para>
553−
554− <example xml:id='ex-dockerTools-shadowSetup'>
555− <title>Shadow base files</title>
556−<programlisting>
557−buildImage {
558− name = "shadow-basic";
559−
560− runAsRoot = ''
561− #!${stdenv.shell}
562− ${shadowSetup}
563− groupadd -r redis
564− useradd -r -g redis redis
565− mkdir /data
566− chown redis:redis /data
567− '';
568−}
569−</programlisting>
570− </example>
571−
572− <para>
573− Creating base files like <literal>/etc/passwd</literal> or
574− <literal>/etc/login.defs</literal> are necessary for shadow-utils to
575− manipulate users and groups.
576− </para>
577− </section>
578− </section>
16+ <xi:include href="functions/dockertools.xml" />
57917</chapter>