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