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