github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/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        --label=[]                      Set metadata for an image
    30        -m, --memory=""                 Memory limit for all build containers
    31        --memory-swap=""                A positive integer equal to memory plus swap. Specify -1 to enable unlimited swap.
    32        --no-cache                      Do not use cache when building the image
    33        --pull                          Always attempt to pull a newer version of the image
    34        -q, --quiet                     Suppress the build output and print image ID on success
    35        --rm=true                       Remove intermediate containers after a successful build
    36        --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`.
    37        -t, --tag=[]                    Name and optionally a tag in the 'name:tag' format
    38        --ulimit=[]                     Ulimit options
    39  
    40  Builds Docker images from a Dockerfile and a "context". A build's context is
    41  the files located in the specified `PATH` or `URL`. The build process can refer
    42  to any of the files in the context. For example, your build can use an
    43  [*ADD*](../builder.md#add) instruction to reference a file in the
    44  context.
    45  
    46  The `URL` parameter can specify the location of a Git repository; the repository
    47  acts as the build context. The system recursively clones the repository and its
    48  submodules using a `git clone --depth 1 --recursive` command. This command runs
    49  in a temporary directory on your local host. After the command succeeds, the
    50  directory is sent to the Docker daemon as the context. Local clones give you the
    51  ability to access private repositories using local user credentials, VPNs, and
    52  so forth.
    53  
    54  Git URLs accept context configuration in their fragment section, separated by a
    55  colon `:`.  The first part represents the reference that Git will check out,
    56  this can be either a branch, a tag, or a commit SHA. The second part represents
    57  a subdirectory inside the repository that will be used as a build context.
    58  
    59  For example, run this command to use a directory called `docker` in the branch
    60  `container`:
    61  
    62        $ docker build https://github.com/docker/rootfs.git#container:docker
    63  
    64  The following table represents all the valid suffixes with their build
    65  contexts:
    66  
    67  Build Syntax Suffix | Commit Used | Build Context Used
    68  --------------------|-------------|-------------------
    69  `myrepo.git` | `refs/heads/master` | `/`
    70  `myrepo.git#mytag` | `refs/tags/mytag` | `/`
    71  `myrepo.git#mybranch` | `refs/heads/mybranch` | `/`
    72  `myrepo.git#abcdef` | `sha1 = abcdef` | `/`
    73  `myrepo.git#:myfolder` | `refs/heads/master` | `/myfolder`
    74  `myrepo.git#master:myfolder` | `refs/heads/master` | `/myfolder`
    75  `myrepo.git#mytag:myfolder` | `refs/tags/mytag` | `/myfolder`
    76  `myrepo.git#mybranch:myfolder` | `refs/heads/mybranch` | `/myfolder`
    77  `myrepo.git#abcdef:myfolder` | `sha1 = abcdef` | `/myfolder`
    78  
    79  Instead of specifying a context, you can pass a single Dockerfile in the `URL`
    80  or pipe the file in via `STDIN`. To pipe a Dockerfile from `STDIN`:
    81  
    82      docker build - < Dockerfile
    83  
    84  If you use STDIN or specify a `URL`, the system places the contents into a file
    85  called `Dockerfile`, and any `-f`, `--file` option is ignored. In this
    86  scenario, there is no context.
    87  
    88  By default the `docker build` command will look for a `Dockerfile` at the root
    89  of the build context. The `-f`, `--file`, option lets you specify the path to
    90  an alternative file to use instead. This is useful in cases where the same set
    91  of files are used for multiple builds. The path must be to a file within the
    92  build context. If a relative path is specified then it must to be relative to
    93  the current directory.
    94  
    95  In most cases, it's best to put each Dockerfile in an empty directory. Then,
    96  add to that directory only the files needed for building the Dockerfile. To
    97  increase the build's performance, you can exclude files and directories by
    98  adding a `.dockerignore` file to that directory as well. For information on
    99  creating one, see the [.dockerignore file](../builder.md#dockerignore-file).
   100  
   101  If the Docker client loses connection to the daemon, the build is canceled.
   102  This happens if you interrupt the Docker client with `CTRL-c` or if the Docker
   103  client is killed for any reason. If the build initiated a pull which is still
   104  running at the time the build is cancelled, the pull is cancelled as well.
   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  scheme.
   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  You can apply multiple tags to an image. For example, you can apply the `latest`
   231  tag to a newly built image and add another tag that references a specific
   232  version.
   233  For example, to tag an image both as `whenry/fedora-jboss:latest` and
   234  `whenry/fedora-jboss:v2.1`, use the following:
   235  
   236      $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
   237  
   238  ### Specify Dockerfile (-f)
   239  
   240      $ docker build -f Dockerfile.debug .
   241  
   242  This will use a file called `Dockerfile.debug` for the build instructions
   243  instead of `Dockerfile`.
   244  
   245      $ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug .
   246      $ docker build -f dockerfiles/Dockerfile.prod  -t myapp_prod .
   247  
   248  The above commands will build the current build context (as specified by the
   249  `.`) twice, once using a debug version of a `Dockerfile` and once using a
   250  production version.
   251  
   252      $ cd /home/me/myapp/some/dir/really/deep
   253      $ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp
   254      $ docker build -f ../../../../dockerfiles/debug /home/me/myapp
   255  
   256  These two `docker build` commands do the exact same thing. They both use the
   257  contents of the `debug` file instead of looking for a `Dockerfile` and will use
   258  `/home/me/myapp` as the root of the build context. Note that `debug` is in the
   259  directory structure of the build context, regardless of how you refer to it on
   260  the command line.
   261  
   262  > **Note:**
   263  > `docker build` will return a `no such file or directory` error if the
   264  > file or directory does not exist in the uploaded context. This may
   265  > happen if there is no context, or if you specify a file that is
   266  > elsewhere on the Host system. The context is limited to the current
   267  > directory (and its children) for security reasons, and to ensure
   268  > repeatable builds on remote Docker hosts. This is also the reason why
   269  > `ADD ../file` will not work.
   270  
   271  ### Optional parent cgroup (--cgroup-parent)
   272  
   273  When `docker build` is run with the `--cgroup-parent` option the containers
   274  used in the build will be run with the [corresponding `docker run`
   275  flag](../run.md#specifying-custom-cgroups).
   276  
   277  ### Set ulimits in container (--ulimit)
   278  
   279  Using the `--ulimit` option with `docker build` will cause each build step's
   280  container to be started using those [`--ulimit`
   281  flag values](./run.md#set-ulimits-in-container-ulimit).
   282  
   283  ### Set build-time variables (--build-arg)
   284  
   285  You can use `ENV` instructions in a Dockerfile to define variable
   286  values. These values persist in the built image. However, often
   287  persistence is not what you want. Users want to specify variables differently
   288  depending on which host they build an image on.
   289  
   290  A good example is `http_proxy` or source versions for pulling intermediate
   291  files. The `ARG` instruction lets Dockerfile authors define values that users
   292  can set at build-time using the  `--build-arg` flag:
   293  
   294      $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .
   295  
   296  This flag allows you to pass the build-time variables that are
   297  accessed like regular environment variables in the `RUN` instruction of the
   298  Dockerfile. Also, these values don't persist in the intermediate or final images
   299  like `ENV` values do.
   300  
   301  For detailed information on using `ARG` and `ENV` instructions, see the
   302  [Dockerfile reference](../builder.md).
   303  
   304  ### Specify isolation technology for container (--isolation)
   305  
   306  This option is useful in situations where you are running Docker containers on
   307  Windows. The `--isolation=<value>` option sets a container's isolation
   308  technology. On Linux, the only supported is the `default` option which uses
   309  Linux namespaces. On Microsoft Windows, you can specify these values:
   310  
   311  
   312  | Value     | Description                                                                                                                                                   |
   313  |-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
   314  | `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.  |
   315  | `process` | Namespace isolation only.                                                                                                                                     |
   316  | `hyperv`   | Hyper-V hypervisor partition-based isolation.                                                                                                                  |
   317  
   318  Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"`.