builder.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. :title: Dockerfiles for Images
  2. :description: Dockerfiles use a simple DSL which allows you to automate the steps you would normally manually take to create an image.
  3. :keywords: builder, docker, Dockerfile, automation, image creation
  4. ==================
  5. Dockerfile Builder
  6. ==================
  7. **Docker can act as a builder** and read instructions from a text
  8. Dockerfile to automate the steps you would otherwise make manually to
  9. create an image. Executing ``docker build`` will run your steps and
  10. commit them along the way, giving you a final image.
  11. .. contents:: Table of Contents
  12. 1. Usage
  13. ========
  14. To build an image from a source repository, create a description file
  15. called ``Dockerfile`` at the root of your repository. This file will
  16. describe the steps to assemble the image.
  17. Then call ``docker build`` with the path of your source repository as
  18. argument:
  19. ``docker build .``
  20. You can specify a repository and tag at which to save the new image if the
  21. build succeeds:
  22. ``docker build -t shykes/myapp .``
  23. Docker will run your steps one-by-one, committing the result if necessary,
  24. before finally outputting the ID of your new image.
  25. 2. Format
  26. =========
  27. The Dockerfile format is quite simple:
  28. ::
  29. # Comment
  30. INSTRUCTION arguments
  31. The Instruction is not case-sensitive, however convention is for them to be
  32. UPPERCASE in order to distinguish them from arguments more easily.
  33. Docker evaluates the instructions in a Dockerfile in order. **The first
  34. instruction must be `FROM`** in order to specify the base image from
  35. which you are building.
  36. Docker will ignore **comment lines** *beginning* with ``#``. A comment
  37. marker anywhere in the rest of the line will be treated as an argument.
  38. 3. Instructions
  39. ===============
  40. Here is the set of instructions you can use in a ``Dockerfile`` for
  41. building images.
  42. 3.1 FROM
  43. --------
  44. ``FROM <image>``
  45. The ``FROM`` instruction sets the :ref:`base_image_def` for subsequent
  46. instructions. As such, a valid Dockerfile must have ``FROM`` as its
  47. first instruction.
  48. ``FROM`` must be the first non-comment instruction in the
  49. ``Dockerfile``.
  50. ``FROM`` can appear multiple times within a single Dockerfile in order
  51. to create multiple images. Simply make a note of the last image id
  52. output by the commit before each new ``FROM`` command.
  53. 3.2 MAINTAINER
  54. --------------
  55. ``MAINTAINER <name>``
  56. The ``MAINTAINER`` instruction allows you to set the *Author* field of
  57. the generated images.
  58. 3.3 RUN
  59. -------
  60. ``RUN <command>``
  61. The ``RUN`` instruction will execute any commands on the current image
  62. and commit the results. The resulting committed image will be used for
  63. the next step in the Dockerfile.
  64. Layering ``RUN`` instructions and generating commits conforms to the
  65. core concepts of Docker where commits are cheap and containers can be
  66. created from any point in an image's history, much like source
  67. control.
  68. 3.4 CMD
  69. -------
  70. ``CMD <command>``
  71. The ``CMD`` instruction sets the command to be executed when running
  72. the image. This is functionally equivalent to running ``docker commit
  73. -run '{"Cmd": <command>}'`` outside the builder.
  74. .. note::
  75. Don't confuse `RUN` with `CMD`. `RUN` actually runs a
  76. command and commits the result; `CMD` does not execute anything at
  77. build time, but specifies the intended command for the image.
  78. 3.5 EXPOSE
  79. ----------
  80. ``EXPOSE <port> [<port>...]``
  81. The ``EXPOSE`` instruction sets ports to be publicly exposed when
  82. running the image. This is functionally equivalent to running ``docker
  83. commit -run '{"PortSpecs": ["<port>", "<port2>"]}'`` outside the
  84. builder.
  85. 3.6 ENV
  86. -------
  87. ``ENV <key> <value>``
  88. The ``ENV`` instruction sets the environment variable ``<key>`` to the
  89. value ``<value>``. This value will be passed to all future ``RUN``
  90. instructions. This is functionally equivalent to prefixing the command
  91. with ``<key>=<value>``
  92. .. note::
  93. The environment variables will persist when a container is run
  94. from the resulting image.
  95. 3.7 ADD
  96. -------
  97. ``ADD <src> <dest>``
  98. The ``ADD`` instruction will copy new files from <src> and add them to
  99. the container's filesystem at path ``<dest>``.
  100. ``<src>`` must be the path to a file or directory relative to the
  101. source directory being built (also called the *context* of the build) or
  102. a remote file URL.
  103. ``<dest>`` is the path at which the source will be copied in the
  104. destination container.
  105. The copy obeys the following rules:
  106. * If ``<src>`` is a URL and ``<dest>`` does not end with a trailing slash,
  107. then a file is downloaded from the URL and copied to ``<dest>``.
  108. * If ``<src>`` is a URL and ``<dest>`` does end with a trailing slash,
  109. then the filename is inferred from the URL and the file is downloaded to
  110. ``<dest>/<filename>``. For instance, ``ADD http://example.com/foobar /``
  111. would create the file ``/foobar``. The URL must have a nontrivial path
  112. so that an appropriate filename can be discovered in this case
  113. (``http://example.com`` will not work).
  114. * If ``<src>`` is a directory, the entire directory is copied,
  115. including filesystem metadata.
  116. * If ``<src>``` is a tar archive in a recognized compression format
  117. (identity, gzip, bzip2 or xz), it is unpacked as a directory.
  118. When a directory is copied or unpacked, it has the same behavior as
  119. ``tar -x``: the result is the union of
  120. 1. whatever existed at the destination path and
  121. 2. the contents of the source tree,
  122. with conflicts resolved in favor of 2) on a file-by-file basis.
  123. * If ``<src>`` is any other kind of file, it is copied individually
  124. along with its metadata. In this case, if ``<dst>`` ends with a
  125. trailing slash ``/``, it will be considered a directory and the
  126. contents of ``<src>`` will be written at ``<dst>/base(<src>)``.
  127. * If ``<dst>`` does not end with a trailing slash, it will be
  128. considered a regular file and the contents of ``<src>`` will be
  129. written at ``<dst>``.
  130. * If ``<dest>`` doesn't exist, it is created along with all missing
  131. directories in its path. All new files and directories are created
  132. with mode 0700, uid and gid 0.
  133. 3.8 ENTRYPOINT
  134. --------------
  135. ``ENTRYPOINT ["/bin/echo"]``
  136. The ``ENTRYPOINT`` instruction adds an entry command that will not be
  137. overwritten when arguments are passed to docker run, unlike the
  138. behavior of ``CMD``. This allows arguments to be passed to the
  139. entrypoint. i.e. ``docker run <image> -d`` will pass the "-d" argument
  140. to the entrypoint.
  141. 3.9 VOLUME
  142. ----------
  143. ``VOLUME ["/data"]``
  144. The ``VOLUME`` instruction will add one or more new volumes to any
  145. container created from the image.
  146. 4. Dockerfile Examples
  147. ======================
  148. .. code-block:: bash
  149. # Nginx
  150. #
  151. # VERSION 0.0.1
  152. FROM ubuntu
  153. MAINTAINER Guillaume J. Charmes "guillaume@dotcloud.com"
  154. # make sure the package repository is up to date
  155. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  156. RUN apt-get update
  157. RUN apt-get install -y inotify-tools nginx apache2 openssh-server
  158. .. code-block:: bash
  159. # Firefox over VNC
  160. #
  161. # VERSION 0.3
  162. FROM ubuntu
  163. # make sure the package repository is up to date
  164. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  165. RUN apt-get update
  166. # Install vnc, xvfb in order to create a 'fake' display and firefox
  167. RUN apt-get install -y x11vnc xvfb firefox
  168. RUN mkdir /.vnc
  169. # Setup a password
  170. RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
  171. # Autostart firefox (might not be the best way, but it does the trick)
  172. RUN bash -c 'echo "firefox" >> /.bashrc'
  173. EXPOSE 5900
  174. CMD ["x11vnc", "-forever", "-usepw", "-create"]
  175. .. code-block:: bash
  176. # Multiple images example
  177. #
  178. # VERSION 0.1
  179. FROM ubuntu
  180. RUN echo foo > bar
  181. # Will output something like ===> 907ad6c2736f
  182. FROM ubuntu
  183. RUN echo moo > oink
  184. # Will output something like ===> 695d7793cbe4
  185. # You'll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
  186. # /oink.