builder.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. .. _dockerbuilder:
  5. ==================
  6. Dockerfile Builder
  7. ==================
  8. **Docker can act as a builder** and read instructions from a text
  9. Dockerfile to automate the steps you would otherwise make manually to
  10. create an image. Executing ``docker build`` will run your steps and
  11. commit them along the way, giving you a final image.
  12. .. contents:: Table of Contents
  13. 1. Usage
  14. ========
  15. To build an image from a source repository, create a description file
  16. called ``Dockerfile`` at the root of your repository. This file will
  17. describe the steps to assemble the image.
  18. Then call ``docker build`` with the path of your source repository as
  19. argument:
  20. ``sudo docker build .``
  21. You can specify a repository and tag at which to save the new image if the
  22. build succeeds:
  23. ``sudo docker build -t shykes/myapp .``
  24. Docker will run your steps one-by-one, committing the result if necessary,
  25. before finally outputting the ID of your new image.
  26. 2. Format
  27. =========
  28. The Dockerfile format is quite simple:
  29. ::
  30. # Comment
  31. INSTRUCTION arguments
  32. The Instruction is not case-sensitive, however convention is for them to be
  33. UPPERCASE in order to distinguish them from arguments more easily.
  34. Docker evaluates the instructions in a Dockerfile in order. **The first
  35. instruction must be `FROM`** in order to specify the base image from
  36. which you are building.
  37. Docker will ignore **comment lines** *beginning* with ``#``. A comment
  38. marker anywhere in the rest of the line will be treated as an argument.
  39. 3. Instructions
  40. ===============
  41. Here is the set of instructions you can use in a ``Dockerfile`` for
  42. building images.
  43. 3.1 FROM
  44. --------
  45. ``FROM <image>``
  46. The ``FROM`` instruction sets the :ref:`base_image_def` for subsequent
  47. instructions. As such, a valid Dockerfile must have ``FROM`` as its
  48. first instruction.
  49. ``FROM`` must be the first non-comment instruction in the
  50. ``Dockerfile``.
  51. ``FROM`` can appear multiple times within a single Dockerfile in order
  52. to create multiple images. Simply make a note of the last image id
  53. output by the commit before each new ``FROM`` command.
  54. 3.2 MAINTAINER
  55. --------------
  56. ``MAINTAINER <name>``
  57. The ``MAINTAINER`` instruction allows you to set the *Author* field of
  58. the generated images.
  59. 3.3 RUN
  60. -------
  61. ``RUN <command>``
  62. The ``RUN`` instruction will execute any commands on the current image
  63. and commit the results. The resulting committed image will be used for
  64. the next step in the Dockerfile.
  65. Layering ``RUN`` instructions and generating commits conforms to the
  66. core concepts of Docker where commits are cheap and containers can be
  67. created from any point in an image's history, much like source
  68. control.
  69. 3.4 CMD
  70. -------
  71. CMD has three forms:
  72. * ``CMD ["executable","param1","param2"]`` (like an *exec*, preferred form)
  73. * ``CMD ["param1","param2"]`` (as *default parameters to ENTRYPOINT*)
  74. * ``CMD command param1 param2`` (as a *shell*)
  75. There can only be one CMD in a Dockerfile. If you list more than one
  76. CMD then only the last CMD will take effect.
  77. **The main purpose of a CMD is to provide defaults for an executing
  78. container.** These defaults can include an executable, or they can
  79. omit the executable, in which case you must specify an ENTRYPOINT as
  80. well.
  81. When used in the shell or exec formats, the ``CMD`` instruction sets
  82. the command to be executed when running the image. This is
  83. functionally equivalent to running ``docker commit -run '{"Cmd":
  84. <command>}'`` outside the builder.
  85. If you use the *shell* form of the CMD, then the ``<command>`` will
  86. execute in ``/bin/sh -c``:
  87. .. code-block:: bash
  88. FROM ubuntu
  89. CMD echo "This is a test." | wc -
  90. If you want to **run your** ``<command>`` **without a shell** then you
  91. must express the command as a JSON array and give the full path to the
  92. executable. **This array form is the preferred format of CMD.** Any
  93. additional parameters must be individually expressed as strings in the
  94. array:
  95. .. code-block:: bash
  96. FROM ubuntu
  97. CMD ["/usr/bin/wc","--help"]
  98. If you would like your container to run the same executable every
  99. time, then you should consider using ``ENTRYPOINT`` in combination
  100. with ``CMD``. See :ref:`entrypoint_def`.
  101. If the user specifies arguments to ``docker run`` then they will
  102. override the default specified in CMD.
  103. .. note::
  104. Don't confuse ``RUN`` with ``CMD``. ``RUN`` actually runs a
  105. command and commits the result; ``CMD`` does not execute anything at
  106. build time, but specifies the intended command for the image.
  107. 3.5 EXPOSE
  108. ----------
  109. ``EXPOSE <port> [<port>...]``
  110. The ``EXPOSE`` instruction sets ports to be publicly exposed when
  111. running the image. This is functionally equivalent to running ``docker
  112. commit -run '{"PortSpecs": ["<port>", "<port2>"]}'`` outside the
  113. builder.
  114. 3.6 ENV
  115. -------
  116. ``ENV <key> <value>``
  117. The ``ENV`` instruction sets the environment variable ``<key>`` to the
  118. value ``<value>``. This value will be passed to all future ``RUN``
  119. instructions. This is functionally equivalent to prefixing the command
  120. with ``<key>=<value>``
  121. .. note::
  122. The environment variables will persist when a container is run
  123. from the resulting image.
  124. 3.7 ADD
  125. -------
  126. ``ADD <src> <dest>``
  127. The ``ADD`` instruction will copy new files from <src> and add them to
  128. the container's filesystem at path ``<dest>``.
  129. ``<src>`` must be the path to a file or directory relative to the
  130. source directory being built (also called the *context* of the build) or
  131. a remote file URL.
  132. ``<dest>`` is the path at which the source will be copied in the
  133. destination container.
  134. The copy obeys the following rules:
  135. * If ``<src>`` is a URL and ``<dest>`` does not end with a trailing slash,
  136. then a file is downloaded from the URL and copied to ``<dest>``.
  137. * If ``<src>`` is a URL and ``<dest>`` does end with a trailing slash,
  138. then the filename is inferred from the URL and the file is downloaded to
  139. ``<dest>/<filename>``. For instance, ``ADD http://example.com/foobar /``
  140. would create the file ``/foobar``. The URL must have a nontrivial path
  141. so that an appropriate filename can be discovered in this case
  142. (``http://example.com`` will not work).
  143. * If ``<src>`` is a directory, the entire directory is copied,
  144. including filesystem metadata.
  145. * If ``<src>``` is a tar archive in a recognized compression format
  146. (identity, gzip, bzip2 or xz), it is unpacked as a directory.
  147. When a directory is copied or unpacked, it has the same behavior as
  148. ``tar -x``: the result is the union of
  149. 1. whatever existed at the destination path and
  150. 2. the contents of the source tree,
  151. with conflicts resolved in favor of 2) on a file-by-file basis.
  152. * If ``<src>`` is any other kind of file, it is copied individually
  153. along with its metadata. In this case, if ``<dst>`` ends with a
  154. trailing slash ``/``, it will be considered a directory and the
  155. contents of ``<src>`` will be written at ``<dst>/base(<src>)``.
  156. * If ``<dst>`` does not end with a trailing slash, it will be
  157. considered a regular file and the contents of ``<src>`` will be
  158. written at ``<dst>``.
  159. * If ``<dest>`` doesn't exist, it is created along with all missing
  160. directories in its path. All new files and directories are created
  161. with mode 0755, uid and gid 0.
  162. .. _entrypoint_def:
  163. 3.8 ENTRYPOINT
  164. --------------
  165. ENTRYPOINT has two forms:
  166. * ``ENTRYPOINT ["executable", "param1", "param2"]`` (like an *exec*,
  167. preferred form)
  168. * ``ENTRYPOINT command param1 param2`` (as a *shell*)
  169. There can only be one ``ENTRYPOINT`` in a Dockerfile. If you have more
  170. than one ``ENTRYPOINT``, then only the last one in the Dockerfile will
  171. have an effect.
  172. An ``ENTRYPOINT`` helps you to configure a container that you can run
  173. as an executable. That is, when you specify an ``ENTRYPOINT``, then
  174. the whole container runs as if it was just that executable.
  175. The ``ENTRYPOINT`` instruction adds an entry command that will **not**
  176. be overwritten when arguments are passed to ``docker run``, unlike the
  177. behavior of ``CMD``. This allows arguments to be passed to the
  178. entrypoint. i.e. ``docker run <image> -d`` will pass the "-d"
  179. argument to the ENTRYPOINT.
  180. You can specify parameters either in the ENTRYPOINT JSON array (as in
  181. "like an exec" above), or by using a CMD statement. Parameters in the
  182. ENTRYPOINT will not be overridden by the ``docker run`` arguments, but
  183. parameters specified via CMD will be overridden by ``docker run``
  184. arguments.
  185. Like a ``CMD``, you can specify a plain string for the ENTRYPOINT and
  186. it will execute in ``/bin/sh -c``:
  187. .. code-block:: bash
  188. FROM ubuntu
  189. ENTRYPOINT wc -l -
  190. For example, that Dockerfile's image will *always* take stdin as input
  191. ("-") and print the number of lines ("-l"). If you wanted to make
  192. this optional but default, you could use a CMD:
  193. .. code-block:: bash
  194. FROM ubuntu
  195. CMD ["-l", "-"]
  196. ENTRYPOINT ["/usr/bin/wc"]
  197. 3.9 VOLUME
  198. ----------
  199. ``VOLUME ["/data"]``
  200. The ``VOLUME`` instruction will add one or more new volumes to any
  201. container created from the image.
  202. 3.10 USER
  203. ---------
  204. ``USER daemon``
  205. The ``USER`` instruction sets the username or UID to use when running
  206. the image.
  207. 3.11 WORKDIR
  208. ------------
  209. ``WORKDIR /path/to/workdir``
  210. The ``WORKDIR`` instruction sets the working directory in which
  211. the command given by ``CMD`` is executed.
  212. 4. Dockerfile Examples
  213. ======================
  214. .. code-block:: bash
  215. # Nginx
  216. #
  217. # VERSION 0.0.1
  218. FROM ubuntu
  219. MAINTAINER Guillaume J. Charmes "guillaume@dotcloud.com"
  220. # make sure the package repository is up to date
  221. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  222. RUN apt-get update
  223. RUN apt-get install -y inotify-tools nginx apache2 openssh-server
  224. .. code-block:: bash
  225. # Firefox over VNC
  226. #
  227. # VERSION 0.3
  228. FROM ubuntu
  229. # make sure the package repository is up to date
  230. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  231. RUN apt-get update
  232. # Install vnc, xvfb in order to create a 'fake' display and firefox
  233. RUN apt-get install -y x11vnc xvfb firefox
  234. RUN mkdir /.vnc
  235. # Setup a password
  236. RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
  237. # Autostart firefox (might not be the best way, but it does the trick)
  238. RUN bash -c 'echo "firefox" >> /.bashrc'
  239. EXPOSE 5900
  240. CMD ["x11vnc", "-forever", "-usepw", "-create"]
  241. .. code-block:: bash
  242. # Multiple images example
  243. #
  244. # VERSION 0.1
  245. FROM ubuntu
  246. RUN echo foo > bar
  247. # Will output something like ===> 907ad6c2736f
  248. FROM ubuntu
  249. RUN echo moo > oink
  250. # Will output something like ===> 695d7793cbe4
  251. # You'll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
  252. # /oink.