github.com/robertojrojas/docker@v1.9.1/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=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  ### Build with PATH
   132  
   133      $ docker build .
   134      Uploading context 10240 bytes
   135      Step 1 : FROM busybox
   136      Pulling repository busybox
   137       ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/
   138      Step 2 : RUN ls -lh /
   139       ---> Running in 9c9e81692ae9
   140      total 24
   141      drwxr-xr-x    2 root     root        4.0K Mar 12  2013 bin
   142      drwxr-xr-x    5 root     root        4.0K Oct 19 00:19 dev
   143      drwxr-xr-x    2 root     root        4.0K Oct 19 00:19 etc
   144      drwxr-xr-x    2 root     root        4.0K Nov 15 23:34 lib
   145      lrwxrwxrwx    1 root     root           3 Mar 12  2013 lib64 -> lib
   146      dr-xr-xr-x  116 root     root           0 Nov 15 23:34 proc
   147      lrwxrwxrwx    1 root     root           3 Mar 12  2013 sbin -> bin
   148      dr-xr-xr-x   13 root     root           0 Nov 15 23:34 sys
   149      drwxr-xr-x    2 root     root        4.0K Mar 12  2013 tmp
   150      drwxr-xr-x    2 root     root        4.0K Nov 15 23:34 usr
   151       ---> b35f4035db3f
   152      Step 3 : CMD echo Hello world
   153       ---> Running in 02071fceb21b
   154       ---> f52f38b7823e
   155      Successfully built f52f38b7823e
   156      Removing intermediate container 9c9e81692ae9
   157      Removing intermediate container 02071fceb21b
   158  
   159  This example specifies that the `PATH` is `.`, and so all the files in the
   160  local directory get `tar`d and sent to the Docker daemon. The `PATH` specifies
   161  where to find the files for the "context" of the build on the Docker daemon.
   162  Remember that the daemon could be running on a remote machine and that no
   163  parsing of the Dockerfile happens at the client side (where you're running
   164  `docker build`). That means that *all* the files at `PATH` get sent, not just
   165  the ones listed to [*ADD*](../builder.md#add) in the Dockerfile.
   166  
   167  The transfer of context from the local machine to the Docker daemon is what the
   168  `docker` client means when you see the "Sending build context" message.
   169  
   170  If you wish to keep the intermediate containers after the build is complete,
   171  you must use `--rm=false`. This does not affect the build cache.
   172  
   173  ### Build with URL
   174  
   175      $ docker build github.com/creack/docker-firefox
   176  
   177  This will clone the GitHub repository and use the cloned repository as context.
   178  The Dockerfile at the root of the repository is used as Dockerfile. Note that
   179  you can specify an arbitrary Git repository by using the `git://` or `git@`
   180  schema.
   181  
   182  ### Build with -
   183  
   184      $ docker build - < Dockerfile
   185  
   186  This will read a Dockerfile from `STDIN` without context. Due to the lack of a
   187  context, no contents of any local directory will be sent to the Docker daemon.
   188  Since there is no context, a Dockerfile `ADD` only works if it refers to a
   189  remote URL.
   190  
   191      $ docker build - < context.tar.gz
   192  
   193  This will build an image for a compressed context read from `STDIN`.  Supported
   194  formats are: bzip2, gzip and xz.
   195  
   196  ### Usage of .dockerignore
   197  
   198      $ docker build .
   199      Uploading context 18.829 MB
   200      Uploading context
   201      Step 1 : FROM busybox
   202       ---> 769b9341d937
   203      Step 2 : CMD echo Hello world
   204       ---> Using cache
   205       ---> 99cc1ad10469
   206      Successfully built 99cc1ad10469
   207      $ echo ".git" > .dockerignore
   208      $ docker build .
   209      Uploading context  6.76 MB
   210      Uploading context
   211      Step 1 : FROM busybox
   212       ---> 769b9341d937
   213      Step 2 : CMD echo Hello world
   214       ---> Using cache
   215       ---> 99cc1ad10469
   216      Successfully built 99cc1ad10469
   217  
   218  This example shows the use of the `.dockerignore` file to exclude the `.git`
   219  directory from the context. Its effect can be seen in the changed size of the
   220  uploaded context. The builder reference contains detailed information on
   221  [creating a .dockerignore file](../builder.md#dockerignore-file)
   222  
   223  ### Tag image (-t)
   224  
   225      $ docker build -t vieux/apache:2.0 .
   226  
   227  This will build like the previous example, but it will then tag the resulting
   228  image. The repository name will be `vieux/apache` and the tag will be `2.0`
   229  
   230  ### Specify Dockerfile (-f)
   231  
   232      $ docker build -f Dockerfile.debug .
   233  
   234  This will use a file called `Dockerfile.debug` for the build instructions
   235  instead of `Dockerfile`.
   236  
   237      $ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug .
   238      $ docker build -f dockerfiles/Dockerfile.prod  -t myapp_prod .
   239  
   240  The above commands will build the current build context (as specified by the
   241  `.`) twice, once using a debug version of a `Dockerfile` and once using a
   242  production version.
   243  
   244      $ cd /home/me/myapp/some/dir/really/deep
   245      $ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp
   246      $ docker build -f ../../../../dockerfiles/debug /home/me/myapp
   247  
   248  These two `docker build` commands do the exact same thing. They both use the
   249  contents of the `debug` file instead of looking for a `Dockerfile` and will use
   250  `/home/me/myapp` as the root of the build context. Note that `debug` is in the
   251  directory structure of the build context, regardless of how you refer to it on
   252  the command line.
   253  
   254  > **Note:**
   255  > `docker build` will return a `no such file or directory` error if the
   256  > file or directory does not exist in the uploaded context. This may
   257  > happen if there is no context, or if you specify a file that is
   258  > elsewhere on the Host system. The context is limited to the current
   259  > directory (and its children) for security reasons, and to ensure
   260  > repeatable builds on remote Docker hosts. This is also the reason why
   261  > `ADD ../file` will not work.
   262  
   263  ### Optional parent cgroup (--cgroup-parent)
   264  
   265  When `docker build` is run with the `--cgroup-parent` option the containers
   266  used in the build will be run with the [corresponding `docker run`
   267  flag](../run.md#specifying-custom-cgroups).
   268  
   269  ### Set ulimits in container (--ulimit)
   270  
   271  Using the `--ulimit` option with `docker build` will cause each build step's
   272  container to be started using those [`--ulimit`
   273  flag values](../run.md#setting-ulimits-in-a-container).
   274  
   275  ### Set build-time variables (--build-arg)
   276  
   277  You can use `ENV` instructions in a Dockerfile to define variable
   278  values. These values persist in the built image. However, often
   279  persistence is not what you want. Users want to specify variables differently
   280  depending on which host they build an image on.
   281  
   282  A good example is `http_proxy` or source versions for pulling intermediate
   283  files. The `ARG` instruction lets Dockerfile authors define values that users
   284  can set at build-time using the  `--build-arg` flag:
   285  
   286      $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .
   287  
   288  This flag allows you to pass the build-time variables that are
   289  accessed like regular environment variables in the `RUN` instruction of the
   290  Dockerfile. Also, these values don't persist in the intermediate or final images
   291  like `ENV` values do.
   292  
   293  For detailed information on using `ARG` and `ENV` instructions, see the
   294  [Dockerfile reference](../builder.md).