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