builder.rst 12 KB

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