builder.rst 17 KB

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