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