| ... | ... |
@@ -88,31 +88,65 @@ Examples: |
| 88 | 88 |
|
| 89 | 89 |
Usage: docker build [OPTIONS] PATH | URL | - |
| 90 | 90 |
Build a new container image from the source code at PATH |
| 91 |
- -t="": Repository name (and optionally a tag) to be applied to the resulting image in case of success. |
|
| 91 |
+ -t="": Repository name (and optionally a tag) to be applied |
|
| 92 |
+ to the resulting image in case of success. |
|
| 92 | 93 |
-q=false: Suppress verbose build output. |
| 93 | 94 |
-no-cache: Do not use the cache when building the image. |
| 94 | 95 |
-rm: Remove intermediate containers after a successful build |
| 95 |
- When a single Dockerfile is given as URL, then no context is set. When a git repository is set as URL, the repository is used as context |
|
| 96 |
+ |
|
| 97 |
+The files at PATH or URL are called the "context" of the build. The |
|
| 98 |
+build process may refer to any of the files in the context, for |
|
| 99 |
+example when using an :ref:`ADD <dockerfile_add>` instruction. When a |
|
| 100 |
+single ``Dockerfile`` is given as URL, then no context is set. When a |
|
| 101 |
+git repository is set as URL, then the repository is used as the |
|
| 102 |
+context |
|
| 96 | 103 |
|
| 97 | 104 |
.. _cli_build_examples: |
| 98 | 105 |
|
| 106 |
+.. seealso:: :ref:`dockerbuilder`. |
|
| 107 |
+ |
|
| 99 | 108 |
Examples: |
| 100 | 109 |
~~~~~~~~~ |
| 101 | 110 |
|
| 102 | 111 |
.. code-block:: bash |
| 103 | 112 |
|
| 104 | 113 |
sudo docker build . |
| 114 |
+ Uploading context 10240 bytes |
|
| 115 |
+ Step 1 : FROM busybox |
|
| 116 |
+ Pulling repository busybox |
|
| 117 |
+ ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/ |
|
| 118 |
+ Step 2 : RUN ls -lh / |
|
| 119 |
+ ---> Running in 9c9e81692ae9 |
|
| 120 |
+ total 24 |
|
| 121 |
+ drwxr-xr-x 2 root root 4.0K Mar 12 2013 bin |
|
| 122 |
+ drwxr-xr-x 5 root root 4.0K Oct 19 00:19 dev |
|
| 123 |
+ drwxr-xr-x 2 root root 4.0K Oct 19 00:19 etc |
|
| 124 |
+ drwxr-xr-x 2 root root 4.0K Nov 15 23:34 lib |
|
| 125 |
+ lrwxrwxrwx 1 root root 3 Mar 12 2013 lib64 -> lib |
|
| 126 |
+ dr-xr-xr-x 116 root root 0 Nov 15 23:34 proc |
|
| 127 |
+ lrwxrwxrwx 1 root root 3 Mar 12 2013 sbin -> bin |
|
| 128 |
+ dr-xr-xr-x 13 root root 0 Nov 15 23:34 sys |
|
| 129 |
+ drwxr-xr-x 2 root root 4.0K Mar 12 2013 tmp |
|
| 130 |
+ drwxr-xr-x 2 root root 4.0K Nov 15 23:34 usr |
|
| 131 |
+ ---> b35f4035db3f |
|
| 132 |
+ Step 3 : CMD echo Hello World |
|
| 133 |
+ ---> Running in 02071fceb21b |
|
| 134 |
+ ---> f52f38b7823e |
|
| 135 |
+ Successfully built f52f38b7823e |
|
| 136 |
+ |
|
| 137 |
+This example specifies that the PATH is ``.``, and so all the files in |
|
| 138 |
+the local directory get tar'd and sent to the Docker daemon. The PATH |
|
| 139 |
+specifies where to find the files for the "context" of the build on |
|
| 140 |
+the Docker daemon. Remember that the daemon could be running on a |
|
| 141 |
+remote machine and that no parsing of the Dockerfile happens at the |
|
| 142 |
+client side (where you're running ``docker build``). That means that |
|
| 143 |
+*all* the files at PATH get sent, not just the ones listed to |
|
| 144 |
+:ref:`ADD <dockerfile_add>` in the ``Dockerfile``. |
|
| 145 |
+ |
|
| 146 |
+The transfer of context from the local machine to the Docker daemon is |
|
| 147 |
+what the ``docker`` client means when you see the "Uploading context" |
|
| 148 |
+message. |
|
| 105 | 149 |
|
| 106 |
-This will read the ``Dockerfile`` from the current directory. It will |
|
| 107 |
-also send any other files and directories found in the current |
|
| 108 |
-directory to the ``docker`` daemon. |
|
| 109 |
- |
|
| 110 |
-The contents of this directory would be used by ``ADD`` commands found |
|
| 111 |
-within the ``Dockerfile``. This will send a lot of data to the |
|
| 112 |
-``docker`` daemon if the current directory contains a lot of data. If |
|
| 113 |
-the absolute path is provided instead of ``.`` then only the files and |
|
| 114 |
-directories required by the ADD commands from the ``Dockerfile`` will be |
|
| 115 |
-added to the context and transferred to the ``docker`` daemon. |
|
| 116 | 150 |
|
| 117 | 151 |
.. code-block:: bash |
| 118 | 152 |
|
| ... | ... |
@@ -129,16 +163,15 @@ tag will be ``2.0`` |
| 129 | 129 |
|
| 130 | 130 |
This will read a ``Dockerfile`` from *stdin* without context. Due to |
| 131 | 131 |
the lack of a context, no contents of any local directory will be sent |
| 132 |
-to the ``docker`` daemon. ``ADD`` doesn't work when running in this |
|
| 133 |
-mode because the absence of the context provides no source files to |
|
| 134 |
-copy to the container. |
|
| 132 |
+to the ``docker`` daemon. Since there is no context, a Dockerfile |
|
| 133 |
+``ADD`` only works if it refers to a remote URL. |
|
| 135 | 134 |
|
| 136 | 135 |
.. code-block:: bash |
| 137 | 136 |
|
| 138 | 137 |
sudo docker build github.com/creack/docker-firefox |
| 139 | 138 |
|
| 140 |
-This will clone the Github repository and use it as context. The |
|
| 141 |
-``Dockerfile`` at the root of the repository is used as |
|
| 139 |
+This will clone the Github repository and use the cloned repository as |
|
| 140 |
+context. The ``Dockerfile`` at the root of the repository is used as |
|
| 142 | 141 |
``Dockerfile``. Note that you can specify an arbitrary git repository |
| 143 | 142 |
by using the ``git://`` schema. |
| 144 | 143 |
|
| ... | ... |
@@ -15,27 +15,39 @@ commit them along the way, giving you a final image. |
| 15 | 15 |
|
| 16 | 16 |
.. contents:: Table of Contents |
| 17 | 17 |
|
| 18 |
+.. _dockerfile_usage: |
|
| 19 |
+ |
|
| 18 | 20 |
1. Usage |
| 19 | 21 |
======== |
| 20 | 22 |
|
| 21 |
-To build an image from a source repository, create a description file |
|
| 22 |
-called ``Dockerfile`` at the root of your repository. This file will |
|
| 23 |
-describe the steps to assemble the image. |
|
| 23 |
+To :ref:`build <cli_build>` an image from a source repository, create |
|
| 24 |
+a description file called ``Dockerfile`` at the root of your |
|
| 25 |
+repository. This file will describe the steps to assemble the image. |
|
| 24 | 26 |
|
| 25 | 27 |
Then call ``docker build`` with the path of your source repository as |
| 26 |
-argument: |
|
| 28 |
+argument (for example, ``.``): |
|
| 27 | 29 |
|
| 28 | 30 |
``sudo docker build .`` |
| 29 | 31 |
|
| 32 |
+The path to the source repository defines where to find the *context* |
|
| 33 |
+of the build. The build is run by the Docker daemon, not by the CLI, |
|
| 34 |
+so the whole context must be transferred to the daemon. The Docker CLI |
|
| 35 |
+reports "Uploading context" when the context is sent to the daemon. |
|
| 36 |
+ |
|
| 30 | 37 |
You can specify a repository and tag at which to save the new image if the |
| 31 | 38 |
build succeeds: |
| 32 | 39 |
|
| 33 | 40 |
``sudo docker build -t shykes/myapp .`` |
| 34 | 41 |
|
| 35 |
-Docker will run your steps one-by-one, committing the result if necessary, |
|
| 36 |
-before finally outputting the ID of your new image. |
|
| 42 |
+The Docker daemon will run your steps one-by-one, committing the |
|
| 43 |
+result if necessary, before finally outputting the ID of your new |
|
| 44 |
+image. The Docker daemon will automatically clean up the context you |
|
| 45 |
+sent. |
|
| 37 | 46 |
|
| 38 |
-When you're done with your build, you're ready to look into :ref:`image_push`. |
|
| 47 |
+When you're done with your build, you're ready to look into |
|
| 48 |
+:ref:`image_push`. |
|
| 49 |
+ |
|
| 50 |
+.. _dockerfile_format: |
|
| 39 | 51 |
|
| 40 | 52 |
2. Format |
| 41 | 53 |
========= |
| ... | ... |
@@ -63,12 +75,16 @@ allows statements like: |
| 63 | 63 |
# Comment |
| 64 | 64 |
RUN echo 'we are running some # of cool things' |
| 65 | 65 |
|
| 66 |
+.. _dockerfile_instructions: |
|
| 67 |
+ |
|
| 66 | 68 |
3. Instructions |
| 67 | 69 |
=============== |
| 68 | 70 |
|
| 69 | 71 |
Here is the set of instructions you can use in a ``Dockerfile`` for |
| 70 | 72 |
building images. |
| 71 | 73 |
|
| 74 |
+.. _dockerfile_from: |
|
| 75 |
+ |
|
| 72 | 76 |
3.1 FROM |
| 73 | 77 |
-------- |
| 74 | 78 |
|
| ... | ... |
@@ -94,6 +110,8 @@ output by the commit before each new ``FROM`` command. |
| 94 | 94 |
If no ``tag`` is given to the ``FROM`` instruction, ``latest`` is |
| 95 | 95 |
assumed. If the used tag does not exist, an error will be returned. |
| 96 | 96 |
|
| 97 |
+.. _dockerfile_maintainer: |
|
| 98 |
+ |
|
| 97 | 99 |
3.2 MAINTAINER |
| 98 | 100 |
-------------- |
| 99 | 101 |
|
| ... | ... |
@@ -102,6 +120,8 @@ assumed. If the used tag does not exist, an error will be returned. |
| 102 | 102 |
The ``MAINTAINER`` instruction allows you to set the *Author* field of |
| 103 | 103 |
the generated images. |
| 104 | 104 |
|
| 105 |
+.. _dockerfile_run: |
|
| 106 |
+ |
|
| 105 | 107 |
3.3 RUN |
| 106 | 108 |
------- |
| 107 | 109 |
|
| ... | ... |
@@ -124,7 +144,7 @@ Known Issues (RUN) |
| 124 | 124 |
``rm`` a file, for example. The issue describes a workaround. |
| 125 | 125 |
* :issue:`2424` Locale will not be set automatically. |
| 126 | 126 |
|
| 127 |
- |
|
| 127 |
+.. _dockerfile_cmd: |
|
| 128 | 128 |
|
| 129 | 129 |
3.4 CMD |
| 130 | 130 |
------- |
| ... | ... |
@@ -169,7 +189,7 @@ array: |
| 169 | 169 |
|
| 170 | 170 |
If you would like your container to run the same executable every |
| 171 | 171 |
time, then you should consider using ``ENTRYPOINT`` in combination |
| 172 |
-with ``CMD``. See :ref:`entrypoint_def`. |
|
| 172 |
+with ``CMD``. See :ref:`dockerfile_entrypoint`. |
|
| 173 | 173 |
|
| 174 | 174 |
If the user specifies arguments to ``docker run`` then they will |
| 175 | 175 |
override the default specified in CMD. |
| ... | ... |
@@ -179,6 +199,8 @@ override the default specified in CMD. |
| 179 | 179 |
command and commits the result; ``CMD`` does not execute anything at |
| 180 | 180 |
build time, but specifies the intended command for the image. |
| 181 | 181 |
|
| 182 |
+.. _dockerfile_expose: |
|
| 183 |
+ |
|
| 182 | 184 |
3.5 EXPOSE |
| 183 | 185 |
---------- |
| 184 | 186 |
|
| ... | ... |
@@ -189,6 +211,8 @@ functionally equivalent to running ``docker commit -run '{"PortSpecs":
|
| 189 | 189 |
["<port>", "<port2>"]}'`` outside the builder. Refer to |
| 190 | 190 |
:ref:`port_redirection` for detailed information. |
| 191 | 191 |
|
| 192 |
+.. _dockerfile_env: |
|
| 193 |
+ |
|
| 192 | 194 |
3.6 ENV |
| 193 | 195 |
------- |
| 194 | 196 |
|
| ... | ... |
@@ -203,6 +227,8 @@ with ``<key>=<value>`` |
| 203 | 203 |
The environment variables will persist when a container is run |
| 204 | 204 |
from the resulting image. |
| 205 | 205 |
|
| 206 |
+.. _dockerfile_add: |
|
| 207 |
+ |
|
| 206 | 208 |
3.7 ADD |
| 207 | 209 |
------- |
| 208 | 210 |
|
| ... | ... |
@@ -263,7 +289,7 @@ The copy obeys the following rules: |
| 263 | 263 |
* If ``<dest>`` doesn't exist, it is created along with all missing |
| 264 | 264 |
directories in its path. |
| 265 | 265 |
|
| 266 |
-.. _entrypoint_def: |
|
| 266 |
+.. _dockerfile_entrypoint: |
|
| 267 | 267 |
|
| 268 | 268 |
3.8 ENTRYPOINT |
| 269 | 269 |
-------------- |
| ... | ... |
@@ -312,6 +338,7 @@ this optional but default, you could use a CMD: |
| 312 | 312 |
CMD ["-l", "-"] |
| 313 | 313 |
ENTRYPOINT ["/usr/bin/wc"] |
| 314 | 314 |
|
| 315 |
+.. _dockerfile_volume: |
|
| 315 | 316 |
|
| 316 | 317 |
3.9 VOLUME |
| 317 | 318 |
---------- |
| ... | ... |
@@ -322,6 +349,8 @@ The ``VOLUME`` instruction will create a mount point with the specified name and |
| 322 | 322 |
as holding externally mounted volumes from native host or other containers. For more information/examples |
| 323 | 323 |
and mounting instructions via docker client, refer to :ref:`volume_def` documentation. |
| 324 | 324 |
|
| 325 |
+.. _dockerfile_user: |
|
| 326 |
+ |
|
| 325 | 327 |
3.10 USER |
| 326 | 328 |
--------- |
| 327 | 329 |
|
| ... | ... |
@@ -330,6 +359,8 @@ and mounting instructions via docker client, refer to :ref:`volume_def` document |
| 330 | 330 |
The ``USER`` instruction sets the username or UID to use when running |
| 331 | 331 |
the image. |
| 332 | 332 |
|
| 333 |
+.. _dockerfile_workdir: |
|
| 334 |
+ |
|
| 333 | 335 |
3.11 WORKDIR |
| 334 | 336 |
------------ |
| 335 | 337 |
|
| ... | ... |
@@ -338,6 +369,7 @@ the image. |
| 338 | 338 |
The ``WORKDIR`` instruction sets the working directory in which |
| 339 | 339 |
the command given by ``CMD`` is executed. |
| 340 | 340 |
|
| 341 |
+.. _dockerfile_examples: |
|
| 341 | 342 |
|
| 342 | 343 |
4. Dockerfile Examples |
| 343 | 344 |
====================== |