builder.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. :title: 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. 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 to a new image if necessary, before finally outputting the
  31. ID of your new image. The Docker daemon will automatically clean
  32. up the context you sent.
  33. Note that each instruction is run independently, and causes a new image
  34. to be created - so ``RUN cd /tmp`` will not have any effect on the next
  35. instructions.
  36. Whenever possible, Docker will re-use the intermediate images,
  37. accelerating ``docker build`` significantly (indicated by ``Using cache``:
  38. .. code-block:: bash
  39. $ docker build -t SvenDowideit/ambassador .
  40. Uploading context 10.24 kB
  41. Uploading context
  42. Step 1 : FROM docker-ut
  43. ---> cbba202fe96b
  44. Step 2 : MAINTAINER SvenDowideit@home.org.au
  45. ---> Using cache
  46. ---> 51182097be13
  47. Step 3 : CMD env | grep _TCP= | sed 's/.*_PORT_\([0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/' | sh && top
  48. ---> Using cache
  49. ---> 1a5ffc17324d
  50. Successfully built 1a5ffc17324d
  51. When you're done with your build, you're ready to look into
  52. :ref:`image_push`.
  53. .. _dockerfile_format:
  54. 2. Format
  55. =========
  56. Here is the format of the Dockerfile:
  57. ::
  58. # Comment
  59. INSTRUCTION arguments
  60. The Instruction is not case-sensitive, however convention is for them to be
  61. UPPERCASE in order to distinguish them from arguments more easily.
  62. Docker evaluates the instructions in a Dockerfile in order. **The
  63. first instruction must be `FROM`** in order to specify the
  64. :ref:`base_image_def` from which you are building.
  65. Docker will treat lines that *begin* with ``#`` as a comment. A ``#``
  66. marker anywhere else in the line will be treated as an argument. This
  67. allows statements like:
  68. ::
  69. # Comment
  70. RUN echo 'we are running some # of cool things'
  71. .. _dockerfile_instructions:
  72. 3. Instructions
  73. ===============
  74. Here is the set of instructions you can use in a ``Dockerfile`` for
  75. building images.
  76. .. _dockerfile_from:
  77. 3.1 FROM
  78. --------
  79. ``FROM <image>``
  80. Or
  81. ``FROM <image>:<tag>``
  82. The ``FROM`` instruction sets the :ref:`base_image_def` for subsequent
  83. instructions. As such, a valid Dockerfile must have ``FROM`` as its
  84. first instruction. The image can be any valid image -- it is
  85. especially easy to start by **pulling an image** from the
  86. :ref:`using_public_repositories`.
  87. ``FROM`` must be the first non-comment instruction in the
  88. ``Dockerfile``.
  89. ``FROM`` can appear multiple times within a single Dockerfile in order
  90. to create multiple images. Simply make a note of the last image id
  91. output by the commit before each new ``FROM`` command.
  92. If no ``tag`` is given to the ``FROM`` instruction, ``latest`` is
  93. assumed. If the used tag does not exist, an error will be returned.
  94. .. _dockerfile_maintainer:
  95. 3.2 MAINTAINER
  96. --------------
  97. ``MAINTAINER <name>``
  98. The ``MAINTAINER`` instruction allows you to set the *Author* field of
  99. the generated images.
  100. .. _dockerfile_run:
  101. 3.3 RUN
  102. -------
  103. RUN has 2 forms:
  104. * ``RUN <command>`` (the command is run in a shell - ``/bin/sh -c``)
  105. * ``RUN ["executable", "param1", "param2"]`` (*exec* form)
  106. The ``RUN`` instruction will execute any commands in a new layer on top
  107. of the current image and commit the results. The resulting committed image
  108. will be used for the next step in the Dockerfile.
  109. Layering ``RUN`` instructions and generating commits conforms to the
  110. core concepts of Docker where commits are cheap and containers can be
  111. created from any point in an image's history, much like source
  112. control.
  113. The *exec* form makes it possible to avoid shell string munging, and to ``RUN``
  114. commands using a base image that does not contain ``/bin/sh``.
  115. Known Issues (RUN)
  116. ..................
  117. * :issue:`783` is about file permissions problems that can occur when
  118. using the AUFS file system. You might notice it during an attempt to
  119. ``rm`` a file, for example. The issue describes a workaround.
  120. * :issue:`2424` Locale will not be set automatically.
  121. .. _dockerfile_cmd:
  122. 3.4 CMD
  123. -------
  124. CMD has three forms:
  125. * ``CMD ["executable","param1","param2"]`` (like an *exec*, preferred form)
  126. * ``CMD ["param1","param2"]`` (as *default parameters to ENTRYPOINT*)
  127. * ``CMD command param1 param2`` (as a *shell*)
  128. There can only be one CMD in a Dockerfile. If you list more than one
  129. CMD then only the last CMD will take effect.
  130. **The main purpose of a CMD is to provide defaults for an executing
  131. container.** These defaults can include an executable, or they can
  132. omit the executable, in which case you must specify an ENTRYPOINT as
  133. well.
  134. When used in the shell or exec formats, the ``CMD`` instruction sets
  135. the command to be executed when running the image. This is
  136. functionally equivalent to running ``docker commit -run '{"Cmd":
  137. <command>}'`` outside the builder.
  138. If you use the *shell* form of the CMD, then the ``<command>`` will
  139. execute in ``/bin/sh -c``:
  140. .. code-block:: bash
  141. FROM ubuntu
  142. CMD echo "This is a test." | wc -
  143. If you want to **run your** ``<command>`` **without a shell** then you
  144. must express the command as a JSON array and give the full path to the
  145. executable. **This array form is the preferred format of CMD.** Any
  146. additional parameters must be individually expressed as strings in the
  147. array:
  148. .. code-block:: bash
  149. FROM ubuntu
  150. CMD ["/usr/bin/wc","--help"]
  151. If you would like your container to run the same executable every
  152. time, then you should consider using ``ENTRYPOINT`` in combination
  153. with ``CMD``. See :ref:`dockerfile_entrypoint`.
  154. If the user specifies arguments to ``docker run`` then they will
  155. override the default specified in CMD.
  156. .. note::
  157. Don't confuse ``RUN`` with ``CMD``. ``RUN`` actually runs a
  158. command and commits the result; ``CMD`` does not execute anything at
  159. build time, but specifies the intended command for the image.
  160. .. _dockerfile_expose:
  161. 3.5 EXPOSE
  162. ----------
  163. ``EXPOSE <port> [<port>...]``
  164. The ``EXPOSE`` instruction exposes ports for use within links. This is
  165. functionally equivalent to running ``docker commit -run '{"PortSpecs":
  166. ["<port>", "<port2>"]}'`` outside the builder. Refer to
  167. :ref:`port_redirection` for detailed information.
  168. .. _dockerfile_env:
  169. 3.6 ENV
  170. -------
  171. ``ENV <key> <value>``
  172. The ``ENV`` instruction sets the environment variable ``<key>`` to the
  173. value ``<value>``. This value will be passed to all future ``RUN``
  174. instructions. This is functionally equivalent to prefixing the command
  175. with ``<key>=<value>``
  176. The environment variables set using ``ENV`` will persist when a container is run
  177. from the resulting image. You can view the values using ``docker inspect``, and change them using ``docker run --env <key>=<value>``.
  178. .. note::
  179. One example where this can cause unexpected consequenses, is setting
  180. ``ENV DEBIAN_FRONTEND noninteractive``.
  181. Which will persist when the container is run interactively; for example:
  182. ``docker run -t -i image bash``
  183. .. _dockerfile_add:
  184. 3.7 ADD
  185. -------
  186. ``ADD <src> <dest>``
  187. The ``ADD`` instruction will copy new files from <src> and add them to
  188. the container's filesystem at path ``<dest>``.
  189. ``<src>`` must be the path to a file or directory relative to the
  190. source directory being built (also called the *context* of the build) or
  191. a remote file URL.
  192. ``<dest>`` is the absolute path to which the source will be copied inside the
  193. destination container.
  194. All new files and directories are created with mode 0755, uid and gid
  195. 0.
  196. .. note::
  197. if you build using STDIN (``docker build - < somefile``), there is no build
  198. context, so the Dockerfile can only contain an URL based ADD statement.
  199. .. note::
  200. if your URL files are protected using authentication, you will need to use
  201. an ``RUN wget`` , ``RUN curl`` or other tool from within the container as
  202. ADD does not support authentication.
  203. The copy obeys the following rules:
  204. * The ``<src>`` path must be inside the *context* of the build; you cannot
  205. ``ADD ../something /something``, because the first step of a
  206. ``docker build`` is to send the context directory (and subdirectories) to
  207. the docker daemon.
  208. * If ``<src>`` is a URL and ``<dest>`` does not end with a trailing slash,
  209. then a file is downloaded from the URL and copied to ``<dest>``.
  210. * If ``<src>`` is a URL and ``<dest>`` does end with a trailing slash,
  211. then the filename is inferred from the URL and the file is downloaded to
  212. ``<dest>/<filename>``. For instance, ``ADD http://example.com/foobar /``
  213. would create the file ``/foobar``. The URL must have a nontrivial path
  214. so that an appropriate filename can be discovered in this case
  215. (``http://example.com`` will not work).
  216. * If ``<src>`` is a directory, the entire directory is copied,
  217. including filesystem metadata.
  218. * If ``<src>`` is a *local* tar archive in a recognized compression
  219. format (identity, gzip, bzip2 or xz) then it is unpacked as a
  220. directory. Resources from *remote* URLs are **not** decompressed.
  221. When a directory is copied or unpacked, it has the same behavior as
  222. ``tar -x``: the result is the union of
  223. 1. whatever existed at the destination path and
  224. 2. the contents of the source tree,
  225. with conflicts resolved in favor of "2." on a file-by-file basis.
  226. * If ``<src>`` is any other kind of file, it is copied individually
  227. along with its metadata. In this case, if ``<dest>`` ends with a
  228. trailing slash ``/``, it will be considered a directory and the
  229. contents of ``<src>`` will be written at ``<dest>/base(<src>)``.
  230. * If ``<dest>`` does not end with a trailing slash, it will be
  231. considered a regular file and the contents of ``<src>`` will be
  232. written at ``<dest>``.
  233. * If ``<dest>`` doesn't exist, it is created along with all missing
  234. directories in its path.
  235. .. _dockerfile_entrypoint:
  236. 3.8 ENTRYPOINT
  237. --------------
  238. ENTRYPOINT has two forms:
  239. * ``ENTRYPOINT ["executable", "param1", "param2"]`` (like an *exec*,
  240. preferred form)
  241. * ``ENTRYPOINT command param1 param2`` (as a *shell*)
  242. There can only be one ``ENTRYPOINT`` in a Dockerfile. If you have more
  243. than one ``ENTRYPOINT``, then only the last one in the Dockerfile will
  244. have an effect.
  245. An ``ENTRYPOINT`` helps you to configure a container that you can run
  246. as an executable. That is, when you specify an ``ENTRYPOINT``, then
  247. the whole container runs as if it was just that executable.
  248. The ``ENTRYPOINT`` instruction adds an entry command that will **not**
  249. be overwritten when arguments are passed to ``docker run``, unlike the
  250. behavior of ``CMD``. This allows arguments to be passed to the
  251. entrypoint. i.e. ``docker run <image> -d`` will pass the "-d"
  252. argument to the ENTRYPOINT.
  253. You can specify parameters either in the ENTRYPOINT JSON array (as in
  254. "like an exec" above), or by using a CMD statement. Parameters in the
  255. ENTRYPOINT will not be overridden by the ``docker run`` arguments, but
  256. parameters specified via CMD will be overridden by ``docker run``
  257. arguments.
  258. Like a ``CMD``, you can specify a plain string for the ENTRYPOINT and
  259. it will execute in ``/bin/sh -c``:
  260. .. code-block:: bash
  261. FROM ubuntu
  262. ENTRYPOINT wc -l -
  263. For example, that Dockerfile's image will *always* take stdin as input
  264. ("-") and print the number of lines ("-l"). If you wanted to make
  265. this optional but default, you could use a CMD:
  266. .. code-block:: bash
  267. FROM ubuntu
  268. CMD ["-l", "-"]
  269. ENTRYPOINT ["/usr/bin/wc"]
  270. .. _dockerfile_volume:
  271. 3.9 VOLUME
  272. ----------
  273. ``VOLUME ["/data"]``
  274. The ``VOLUME`` instruction will create a mount point with the specified name and mark it
  275. as holding externally mounted volumes from native host or other containers. For more information/examples
  276. and mounting instructions via docker client, refer to :ref:`volume_def` documentation.
  277. .. _dockerfile_user:
  278. 3.10 USER
  279. ---------
  280. ``USER daemon``
  281. The ``USER`` instruction sets the username or UID to use when running
  282. the image.
  283. .. _dockerfile_workdir:
  284. 3.11 WORKDIR
  285. ------------
  286. ``WORKDIR /path/to/workdir``
  287. The ``WORKDIR`` instruction sets the working directory for the ``RUN``, ``CMD`` and
  288. ``ENTRYPOINT`` Dockerfile commands that follow it.
  289. It can be used multiple times in the one Dockerfile.
  290. 3.11 ONBUILD
  291. ------------
  292. ``ONBUILD [INSTRUCTION]``
  293. The ``ONBUILD`` instruction adds to the image a "trigger" instruction to be
  294. executed at a later time, when the image is used as the base for another build.
  295. The trigger will be executed in the context of the downstream build, as if it
  296. had been inserted immediately after the *FROM* instruction in the downstream
  297. Dockerfile.
  298. Any build instruction can be registered as a trigger.
  299. This is useful if you are building an image which will be used as a base to build
  300. other images, for example an application build environment or a daemon which may be
  301. customized with user-specific configuration.
  302. For example, if your image is a reusable python application builder, it will require
  303. application source code to be added in a particular directory, and it might require
  304. a build script to be called *after* that. You can't just call *ADD* and *RUN* now,
  305. because you don't yet have access to the application source code, and it will be
  306. different for each application build. You could simply provide application developers
  307. with a boilerplate Dockerfile to copy-paste into their application, but that is
  308. inefficient, error-prone and difficult to update because it mixes with
  309. application-specific code.
  310. The solution is to use *ONBUILD* to register in advance instructions to run later,
  311. during the next build stage.
  312. Here's how it works:
  313. 1. When it encounters an *ONBUILD* instruction, the builder adds a trigger to
  314. the metadata of the image being built.
  315. The instruction does not otherwise affect the current build.
  316. 2. At the end of the build, a list of all triggers is stored in the image manifest,
  317. under the key *OnBuild*. They can be inspected with *docker inspect*.
  318. 3. Later the image may be used as a base for a new build, using the *FROM* instruction.
  319. As part of processing the *FROM* instruction, the downstream builder looks for *ONBUILD*
  320. triggers, and executes them in the same order they were registered. If any of the
  321. triggers fail, the *FROM* instruction is aborted which in turn causes the build
  322. to fail. If all triggers succeed, the FROM instruction completes and the build
  323. continues as usual.
  324. 4. Triggers are cleared from the final image after being executed. In other words
  325. they are not inherited by "grand-children" builds.
  326. For example you might add something like this:
  327. .. code-block:: bash
  328. [...]
  329. ONBUILD ADD . /app/src
  330. ONBUILD RUN /usr/local/bin/python-build --dir /app/src
  331. [...]
  332. .. warning:: Chaining ONBUILD instructions using `ONBUILD ONBUILD` isn't allowed.
  333. .. warning:: ONBUILD may not trigger FROM or MAINTAINER instructions.
  334. .. _dockerfile_examples:
  335. 4. Dockerfile Examples
  336. ======================
  337. .. code-block:: bash
  338. # Nginx
  339. #
  340. # VERSION 0.0.1
  341. FROM ubuntu
  342. MAINTAINER Guillaume J. Charmes <guillaume@dotcloud.com>
  343. # make sure the package repository is up to date
  344. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  345. RUN apt-get update
  346. RUN apt-get install -y inotify-tools nginx apache2 openssh-server
  347. .. code-block:: bash
  348. # Firefox over VNC
  349. #
  350. # VERSION 0.3
  351. FROM ubuntu
  352. # make sure the package repository is up to date
  353. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  354. RUN apt-get update
  355. # Install vnc, xvfb in order to create a 'fake' display and firefox
  356. RUN apt-get install -y x11vnc xvfb firefox
  357. RUN mkdir /.vnc
  358. # Setup a password
  359. RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
  360. # Autostart firefox (might not be the best way, but it does the trick)
  361. RUN bash -c 'echo "firefox" >> /.bashrc'
  362. EXPOSE 5900
  363. CMD ["x11vnc", "-forever", "-usepw", "-create"]
  364. .. code-block:: bash
  365. # Multiple images example
  366. #
  367. # VERSION 0.1
  368. FROM ubuntu
  369. RUN echo foo > bar
  370. # Will output something like ===> 907ad6c2736f
  371. FROM ubuntu
  372. RUN echo moo > oink
  373. # Will output something like ===> 695d7793cbe4
  374. # You'll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
  375. # /oink.