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