github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/docs/reference/commandline/build.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "build"
     4  description = "The build command description and usage"
     5  keywords = ["build, docker, image"]
     6  [menu.main]
     7  parent = "smn_cli"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # build
    12  
    13  ```markdown
    14  Usage:  docker build [OPTIONS] PATH | URL | -
    15  
    16  Build an image from a Dockerfile
    17  
    18  Options:
    19        --build-arg value         Set build-time variables (default [])
    20        --cache-from value        Images to consider as cache sources (default [])
    21        --cgroup-parent string    Optional parent cgroup for the container
    22        --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
    23        --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
    24    -c, --cpu-shares int          CPU shares (relative weight)
    25        --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
    26        --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
    27        --disable-content-trust   Skip image verification (default true)
    28    -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
    29        --force-rm                Always remove intermediate containers
    30        --help                    Print usage
    31        --isolation string        Container isolation technology
    32        --label value             Set metadata for an image (default [])
    33    -m, --memory string           Memory limit
    34        --memory-swap string      Swap limit equal to memory plus swap: '-1' to enable unlimited swap
    35        --no-cache                Do not use cache when building the image
    36        --pull                    Always attempt to pull a newer version of the image
    37    -q, --quiet                   Suppress the build output and print image ID on success
    38        --rm                      Remove intermediate containers after a successful build (default true)
    39        --shm-size string         Size of /dev/shm, default value is 64MB.
    40                                  The format is `<number><unit>`. `number` must be greater than `0`.
    41                                  Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes),
    42                                  or `g` (gigabytes). If you omit the unit, the system uses bytes.
    43    -t, --tag value               Name and optionally a tag in the 'name:tag' format (default [])
    44        --ulimit value            Ulimit options (default [])
    45  ```
    46  
    47  Builds Docker images from a Dockerfile and a "context". A build's context is
    48  the files located in the specified `PATH` or `URL`. The build process can refer
    49  to any of the files in the context. For example, your build can use an
    50  [*ADD*](../builder.md#add) instruction to reference a file in the
    51  context.
    52  
    53  The `URL` parameter can refer to three kinds of resources: Git repositories,
    54  pre-packaged tarball contexts and plain text files.
    55  
    56  ### Git repositories
    57  
    58  When the `URL` parameter points to the location of a Git repository, the
    59  repository acts as the build context. The system recursively clones the
    60  repository and its submodules using a `git clone --depth 1 --recursive`
    61  command. This command runs in a temporary directory on your local host. After
    62  the command succeeds, the directory is sent to the Docker daemon as the
    63  context. Local clones give you the ability to access private repositories using
    64  local user credentials, VPN's, and so forth.
    65  
    66  Git URLs accept context configuration in their fragment section, separated by a
    67  colon `:`.  The first part represents the reference that Git will check out,
    68  this can be either a branch, a tag, or a commit SHA. The second part represents
    69  a subdirectory inside the repository that will be used as a build context.
    70  
    71  For example, run this command to use a directory called `docker` in the branch
    72  `container`:
    73  
    74  ```bash
    75  $ docker build https://github.com/docker/rootfs.git#container:docker
    76  ```
    77  
    78  The following table represents all the valid suffixes with their build
    79  contexts:
    80  
    81  Build Syntax Suffix             | Commit Used           | Build Context Used
    82  --------------------------------|-----------------------|-------------------
    83  `myrepo.git`                    | `refs/heads/master`   | `/`
    84  `myrepo.git#mytag`              | `refs/tags/mytag`     | `/`
    85  `myrepo.git#mybranch`           | `refs/heads/mybranch` | `/`
    86  `myrepo.git#abcdef`             | `sha1 = abcdef`       | `/`
    87  `myrepo.git#:myfolder`          | `refs/heads/master`   | `/myfolder`
    88  `myrepo.git#master:myfolder`    | `refs/heads/master`   | `/myfolder`
    89  `myrepo.git#mytag:myfolder`     | `refs/tags/mytag`     | `/myfolder`
    90  `myrepo.git#mybranch:myfolder`  | `refs/heads/mybranch` | `/myfolder`
    91  `myrepo.git#abcdef:myfolder`    | `sha1 = abcdef`       | `/myfolder`
    92  
    93  
    94  ### Tarball contexts
    95  
    96  If you pass an URL to a remote tarball, the URL itself is sent to the daemon:
    97  
    98  Instead of specifying a context, you can pass a single Dockerfile in the `URL`
    99  or pipe the file in via `STDIN`. To pipe a Dockerfile from `STDIN`:
   100  
   101  ```bash
   102  $ docker build http://server/context.tar.gz
   103  
   104  The download operation will be performed on the host the Docker daemon is
   105  running on, which is not necessarily the same host from which the build command
   106  is being issued. The Docker daemon will fetch `context.tar.gz` and use it as the
   107  build context. Tarball contexts must be tar archives conforming to the standard
   108  `tar` UNIX format and can be compressed with any one of the 'xz', 'bzip2',
   109  'gzip' or 'identity' (no compression) formats.
   110  
   111  ### Text files
   112  
   113  Instead of specifying a context, you can pass a single `Dockerfile` in the
   114  `URL` or pipe the file in via `STDIN`. To pipe a `Dockerfile` from `STDIN`:
   115  
   116  ```bash
   117  $ docker build - < Dockerfile
   118  ```
   119  
   120  With Powershell on Windows, you can run:
   121  
   122  ```powershell
   123  Get-Content Dockerfile | docker build -
   124  ```
   125  
   126  If you use `STDIN` or specify a `URL` pointing to a plain text file, the system
   127  places the contents into a file called `Dockerfile`, and any `-f`, `--file`
   128  option is ignored. In this scenario, there is no context.
   129  
   130  By default the `docker build` command will look for a `Dockerfile` at the root
   131  of the build context. The `-f`, `--file`, option lets you specify the path to
   132  an alternative file to use instead. This is useful in cases where the same set
   133  of files are used for multiple builds. The path must be to a file within the
   134  build context. If a relative path is specified then it is interpreted as
   135  relative to the root of the context.
   136  
   137  In most cases, it's best to put each Dockerfile in an empty directory. Then,
   138  add to that directory only the files needed for building the Dockerfile. To
   139  increase the build's performance, you can exclude files and directories by
   140  adding a `.dockerignore` file to that directory as well. For information on
   141  creating one, see the [.dockerignore file](../builder.md#dockerignore-file).
   142  
   143  If the Docker client loses connection to the daemon, the build is canceled.
   144  This happens if you interrupt the Docker client with `CTRL-c` or if the Docker
   145  client is killed for any reason. If the build initiated a pull which is still
   146  running at the time the build is cancelled, the pull is cancelled as well.
   147  
   148  ## Return code
   149  
   150  On a successful build, a return code of success `0` will be returned.  When the
   151  build fails, a non-zero failure code will be returned.
   152  
   153  There should be informational output of the reason for failure output to
   154  `STDERR`:
   155  
   156  ```bash
   157  $ docker build -t fail .
   158  
   159  Sending build context to Docker daemon 2.048 kB
   160  Sending build context to Docker daemon
   161  Step 1 : FROM busybox
   162   ---> 4986bf8c1536
   163  Step 2 : RUN exit 13
   164   ---> Running in e26670ec7a0a
   165  INFO[0000] The command [/bin/sh -c exit 13] returned a non-zero code: 13
   166  $ echo $?
   167  1
   168  ```
   169  
   170  See also:
   171  
   172  [*Dockerfile Reference*](../builder.md).
   173  
   174  ## Examples
   175  
   176  ### Build with PATH
   177  
   178  ```bash
   179  $ docker build .
   180  
   181  Uploading context 10240 bytes
   182  Step 1 : FROM busybox
   183  Pulling repository busybox
   184   ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/
   185  Step 2 : RUN ls -lh /
   186   ---> Running in 9c9e81692ae9
   187  total 24
   188  drwxr-xr-x    2 root     root        4.0K Mar 12  2013 bin
   189  drwxr-xr-x    5 root     root        4.0K Oct 19 00:19 dev
   190  drwxr-xr-x    2 root     root        4.0K Oct 19 00:19 etc
   191  drwxr-xr-x    2 root     root        4.0K Nov 15 23:34 lib
   192  lrwxrwxrwx    1 root     root           3 Mar 12  2013 lib64 -> lib
   193  dr-xr-xr-x  116 root     root           0 Nov 15 23:34 proc
   194  lrwxrwxrwx    1 root     root           3 Mar 12  2013 sbin -> bin
   195  dr-xr-xr-x   13 root     root           0 Nov 15 23:34 sys
   196  drwxr-xr-x    2 root     root        4.0K Mar 12  2013 tmp
   197  drwxr-xr-x    2 root     root        4.0K Nov 15 23:34 usr
   198   ---> b35f4035db3f
   199  Step 3 : CMD echo Hello world
   200   ---> Running in 02071fceb21b
   201   ---> f52f38b7823e
   202  Successfully built f52f38b7823e
   203  Removing intermediate container 9c9e81692ae9
   204  Removing intermediate container 02071fceb21b
   205  ```
   206  
   207  This example specifies that the `PATH` is `.`, and so all the files in the
   208  local directory get `tar`d and sent to the Docker daemon. The `PATH` specifies
   209  where to find the files for the "context" of the build on the Docker daemon.
   210  Remember that the daemon could be running on a remote machine and that no
   211  parsing of the Dockerfile happens at the client side (where you're running
   212  `docker build`). That means that *all* the files at `PATH` get sent, not just
   213  the ones listed to [*ADD*](../builder.md#add) in the Dockerfile.
   214  
   215  The transfer of context from the local machine to the Docker daemon is what the
   216  `docker` client means when you see the "Sending build context" message.
   217  
   218  If you wish to keep the intermediate containers after the build is complete,
   219  you must use `--rm=false`. This does not affect the build cache.
   220  
   221  ### Build with URL
   222  
   223  ```bash
   224  $ docker build github.com/creack/docker-firefox
   225  ```
   226  
   227  This will clone the GitHub repository and use the cloned repository as context.
   228  The Dockerfile at the root of the repository is used as Dockerfile. You can
   229  specify an arbitrary Git repository by using the `git://` or `git@` scheme.
   230  
   231  ```bash
   232  $ docker build -f ctx/Dockerfile http://server/ctx.tar.gz
   233  
   234  Downloading context: http://server/ctx.tar.gz [===================>]    240 B/240 B
   235  Step 1 : FROM busybox
   236   ---> 8c2e06607696
   237  Step 2 : ADD ctx/container.cfg /
   238   ---> e7829950cee3
   239  Removing intermediate container b35224abf821
   240  Step 3 : CMD /bin/ls
   241   ---> Running in fbc63d321d73
   242   ---> 3286931702ad
   243  Removing intermediate container fbc63d321d73
   244  Successfully built 377c409b35e4
   245  ```
   246  
   247  This sends the URL `http://server/ctx.tar.gz` to the Docker daemon, which
   248  downloads and extracts the referenced tarball. The `-f ctx/Dockerfile`
   249  parameter specifies a path inside `ctx.tar.gz` to the `Dockerfile` that is used
   250  to build the image. Any `ADD` commands in that `Dockerfile` that refer to local
   251  paths must be relative to the root of the contents inside `ctx.tar.gz`. In the
   252  example above, the tarball contains a directory `ctx/`, so the `ADD
   253  ctx/container.cfg /` operation works as expected.
   254  
   255  ### Build with -
   256  
   257  ```bash
   258  $ docker build - < Dockerfile
   259  ```
   260  
   261  This will read a Dockerfile from `STDIN` without context. Due to the lack of a
   262  context, no contents of any local directory will be sent to the Docker daemon.
   263  Since there is no context, a Dockerfile `ADD` only works if it refers to a
   264  remote URL.
   265  
   266  ```bash
   267  $ docker build - < context.tar.gz
   268  ```
   269  
   270  This will build an image for a compressed context read from `STDIN`.  Supported
   271  formats are: bzip2, gzip and xz.
   272  
   273  ### Usage of .dockerignore
   274  
   275  ```bash
   276  $ docker build .
   277  
   278  Uploading context 18.829 MB
   279  Uploading context
   280  Step 1 : FROM busybox
   281   ---> 769b9341d937
   282  Step 2 : CMD echo Hello world
   283   ---> Using cache
   284   ---> 99cc1ad10469
   285  Successfully built 99cc1ad10469
   286  $ echo ".git" > .dockerignore
   287  $ docker build .
   288  Uploading context  6.76 MB
   289  Uploading context
   290  Step 1 : FROM busybox
   291   ---> 769b9341d937
   292  Step 2 : CMD echo Hello world
   293   ---> Using cache
   294   ---> 99cc1ad10469
   295  Successfully built 99cc1ad10469
   296  ```
   297  
   298  This example shows the use of the `.dockerignore` file to exclude the `.git`
   299  directory from the context. Its effect can be seen in the changed size of the
   300  uploaded context. The builder reference contains detailed information on
   301  [creating a .dockerignore file](../builder.md#dockerignore-file)
   302  
   303  ### Tag image (-t)
   304  
   305  ```bash
   306  $ docker build -t vieux/apache:2.0 .
   307  ```
   308  
   309  This will build like the previous example, but it will then tag the resulting
   310  image. The repository name will be `vieux/apache` and the tag will be `2.0`.
   311  [Read more about valid tags](tag.md).
   312  
   313  You can apply multiple tags to an image. For example, you can apply the `latest`
   314  tag to a newly built image and add another tag that references a specific
   315  version.
   316  For example, to tag an image both as `whenry/fedora-jboss:latest` and
   317  `whenry/fedora-jboss:v2.1`, use the following:
   318  
   319  ```bash
   320  $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
   321  ```
   322  ### Specify Dockerfile (-f)
   323  
   324  ```bash
   325  $ docker build -f Dockerfile.debug .
   326  ```
   327  
   328  This will use a file called `Dockerfile.debug` for the build instructions
   329  instead of `Dockerfile`.
   330  
   331  ```bash
   332  $ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug .
   333  $ docker build -f dockerfiles/Dockerfile.prod  -t myapp_prod .
   334  ```
   335  
   336  The above commands will build the current build context (as specified by the
   337  `.`) twice, once using a debug version of a `Dockerfile` and once using a
   338  production version.
   339  
   340  ```bash
   341  $ cd /home/me/myapp/some/dir/really/deep
   342  $ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp
   343  $ docker build -f ../../../../dockerfiles/debug /home/me/myapp
   344  ```
   345  
   346  These two `docker build` commands do the exact same thing. They both use the
   347  contents of the `debug` file instead of looking for a `Dockerfile` and will use
   348  `/home/me/myapp` as the root of the build context. Note that `debug` is in the
   349  directory structure of the build context, regardless of how you refer to it on
   350  the command line.
   351  
   352  > **Note:**
   353  > `docker build` will return a `no such file or directory` error if the
   354  > file or directory does not exist in the uploaded context. This may
   355  > happen if there is no context, or if you specify a file that is
   356  > elsewhere on the Host system. The context is limited to the current
   357  > directory (and its children) for security reasons, and to ensure
   358  > repeatable builds on remote Docker hosts. This is also the reason why
   359  > `ADD ../file` will not work.
   360  
   361  ### Optional parent cgroup (--cgroup-parent)
   362  
   363  When `docker build` is run with the `--cgroup-parent` option the containers
   364  used in the build will be run with the [corresponding `docker run`
   365  flag](../run.md#specifying-custom-cgroups).
   366  
   367  ### Set ulimits in container (--ulimit)
   368  
   369  Using the `--ulimit` option with `docker build` will cause each build step's
   370  container to be started using those [`--ulimit`
   371  flag values](./run.md#set-ulimits-in-container-ulimit).
   372  
   373  ### Set build-time variables (--build-arg)
   374  
   375  You can use `ENV` instructions in a Dockerfile to define variable
   376  values. These values persist in the built image. However, often
   377  persistence is not what you want. Users want to specify variables differently
   378  depending on which host they build an image on.
   379  
   380  A good example is `http_proxy` or source versions for pulling intermediate
   381  files. The `ARG` instruction lets Dockerfile authors define values that users
   382  can set at build-time using the  `--build-arg` flag:
   383  
   384  ```bash
   385  $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .
   386  ```
   387  
   388  This flag allows you to pass the build-time variables that are
   389  accessed like regular environment variables in the `RUN` instruction of the
   390  Dockerfile. Also, these values don't persist in the intermediate or final images
   391  like `ENV` values do.
   392  
   393  Using this flag will not alter the output you see when the `ARG` lines from the
   394  Dockerfile are echoed during the build process.
   395  
   396  For detailed information on using `ARG` and `ENV` instructions, see the
   397  [Dockerfile reference](../builder.md).
   398  
   399  ### Specify isolation technology for container (--isolation)
   400  
   401  This option is useful in situations where you are running Docker containers on
   402  Windows. The `--isolation=<value>` option sets a container's isolation
   403  technology. On Linux, the only supported is the `default` option which uses
   404  Linux namespaces. On Microsoft Windows, you can specify these values:
   405  
   406  
   407  | Value     | Description                                                                                                                                                   |
   408  |-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
   409  | `default` | Use the value specified by the Docker daemon's `--exec-opt` . If the `daemon` does not specify an isolation technology, Microsoft Windows uses `process` as its default value.  |
   410  | `process` | Namespace isolation only.                                                                                                                                     |
   411  | `hyperv`  | Hyper-V hypervisor partition-based isolation.                                                                                                                 |
   412  
   413  Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"`.