builder.rst 13 KB

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