github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/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 bytes Size of /dev/shm 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 Instead of specifying a context, you can pass a single Dockerfile in the `URL` 115 or pipe the file in via `STDIN`. To pipe a Dockerfile from `STDIN`: 116 117 ```bash 118 $ docker build http://server/context.tar.gz 119 ``` 120 121 The download operation will be performed on the host the Docker daemon is 122 running on, which is not necessarily the same host from which the build command 123 is being issued. The Docker daemon will fetch `context.tar.gz` and use it as the 124 build context. Tarball contexts must be tar archives conforming to the standard 125 `tar` UNIX format and can be compressed with any one of the 'xz', 'bzip2', 126 'gzip' or 'identity' (no compression) formats. 127 128 ### Text files 129 130 Instead of specifying a context, you can pass a single `Dockerfile` in the 131 `URL` or pipe the file in via `STDIN`. To pipe a `Dockerfile` from `STDIN`: 132 133 ```bash 134 $ docker build - < Dockerfile 135 ``` 136 137 With Powershell on Windows, you can run: 138 139 ```powershell 140 Get-Content Dockerfile | docker build - 141 ``` 142 143 If you use `STDIN` or specify a `URL` pointing to a plain text file, the system 144 places the contents into a file called `Dockerfile`, and any `-f`, `--file` 145 option is ignored. In this scenario, there is no context. 146 147 By default the `docker build` command will look for a `Dockerfile` at the root 148 of the build context. The `-f`, `--file`, option lets you specify the path to 149 an alternative file to use instead. This is useful in cases where the same set 150 of files are used for multiple builds. The path must be to a file within the 151 build context. If a relative path is specified then it is interpreted as 152 relative to the root of the context. 153 154 In most cases, it's best to put each Dockerfile in an empty directory. Then, 155 add to that directory only the files needed for building the Dockerfile. To 156 increase the build's performance, you can exclude files and directories by 157 adding a `.dockerignore` file to that directory as well. For information on 158 creating one, see the [.dockerignore file](../builder.md#dockerignore-file). 159 160 If the Docker client loses connection to the daemon, the build is canceled. 161 This happens if you interrupt the Docker client with `CTRL-c` or if the Docker 162 client is killed for any reason. If the build initiated a pull which is still 163 running at the time the build is cancelled, the pull is cancelled as well. 164 165 ## Return code 166 167 On a successful build, a return code of success `0` will be returned. When the 168 build fails, a non-zero failure code will be returned. 169 170 There should be informational output of the reason for failure output to 171 `STDERR`: 172 173 ```bash 174 $ docker build -t fail . 175 176 Sending build context to Docker daemon 2.048 kB 177 Sending build context to Docker daemon 178 Step 1/3 : FROM busybox 179 ---> 4986bf8c1536 180 Step 2/3 : RUN exit 13 181 ---> Running in e26670ec7a0a 182 INFO[0000] The command [/bin/sh -c exit 13] returned a non-zero code: 13 183 $ echo $? 184 1 185 ``` 186 187 See also: 188 189 [*Dockerfile Reference*](../builder.md). 190 191 ## Examples 192 193 ### Build with PATH 194 195 ```bash 196 $ docker build . 197 198 Uploading context 10240 bytes 199 Step 1/3 : FROM busybox 200 Pulling repository busybox 201 ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/ 202 Step 2/3 : RUN ls -lh / 203 ---> Running in 9c9e81692ae9 204 total 24 205 drwxr-xr-x 2 root root 4.0K Mar 12 2013 bin 206 drwxr-xr-x 5 root root 4.0K Oct 19 00:19 dev 207 drwxr-xr-x 2 root root 4.0K Oct 19 00:19 etc 208 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 lib 209 lrwxrwxrwx 1 root root 3 Mar 12 2013 lib64 -> lib 210 dr-xr-xr-x 116 root root 0 Nov 15 23:34 proc 211 lrwxrwxrwx 1 root root 3 Mar 12 2013 sbin -> bin 212 dr-xr-xr-x 13 root root 0 Nov 15 23:34 sys 213 drwxr-xr-x 2 root root 4.0K Mar 12 2013 tmp 214 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 usr 215 ---> b35f4035db3f 216 Step 3/3 : CMD echo Hello world 217 ---> Running in 02071fceb21b 218 ---> f52f38b7823e 219 Successfully built f52f38b7823e 220 Removing intermediate container 9c9e81692ae9 221 Removing intermediate container 02071fceb21b 222 ``` 223 224 This example specifies that the `PATH` is `.`, and so all the files in the 225 local directory get `tar`d and sent to the Docker daemon. The `PATH` specifies 226 where to find the files for the "context" of the build on the Docker daemon. 227 Remember that the daemon could be running on a remote machine and that no 228 parsing of the Dockerfile happens at the client side (where you're running 229 `docker build`). That means that *all* the files at `PATH` get sent, not just 230 the ones listed to [*ADD*](../builder.md#add) in the Dockerfile. 231 232 The transfer of context from the local machine to the Docker daemon is what the 233 `docker` client means when you see the "Sending build context" message. 234 235 If you wish to keep the intermediate containers after the build is complete, 236 you must use `--rm=false`. This does not affect the build cache. 237 238 ### Build with URL 239 240 ```bash 241 $ docker build github.com/creack/docker-firefox 242 ``` 243 244 This will clone the GitHub repository and use the cloned repository as context. 245 The Dockerfile at the root of the repository is used as Dockerfile. You can 246 specify an arbitrary Git repository by using the `git://` or `git@` scheme. 247 248 ```bash 249 $ docker build -f ctx/Dockerfile http://server/ctx.tar.gz 250 251 Downloading context: http://server/ctx.tar.gz [===================>] 240 B/240 B 252 Step 1/3 : FROM busybox 253 ---> 8c2e06607696 254 Step 2/3 : ADD ctx/container.cfg / 255 ---> e7829950cee3 256 Removing intermediate container b35224abf821 257 Step 3/3 : CMD /bin/ls 258 ---> Running in fbc63d321d73 259 ---> 3286931702ad 260 Removing intermediate container fbc63d321d73 261 Successfully built 377c409b35e4 262 ``` 263 264 This sends the URL `http://server/ctx.tar.gz` to the Docker daemon, which 265 downloads and extracts the referenced tarball. The `-f ctx/Dockerfile` 266 parameter specifies a path inside `ctx.tar.gz` to the `Dockerfile` that is used 267 to build the image. Any `ADD` commands in that `Dockerfile` that refers to local 268 paths must be relative to the root of the contents inside `ctx.tar.gz`. In the 269 example above, the tarball contains a directory `ctx/`, so the `ADD 270 ctx/container.cfg /` operation works as expected. 271 272 ### Build with - 273 274 ```bash 275 $ docker build - < Dockerfile 276 ``` 277 278 This will read a Dockerfile from `STDIN` without context. Due to the lack of a 279 context, no contents of any local directory will be sent to the Docker daemon. 280 Since there is no context, a Dockerfile `ADD` only works if it refers to a 281 remote URL. 282 283 ```bash 284 $ docker build - < context.tar.gz 285 ``` 286 287 This will build an image for a compressed context read from `STDIN`. Supported 288 formats are: bzip2, gzip and xz. 289 290 ### Use a .dockerignore file 291 292 ```bash 293 $ docker build . 294 295 Uploading context 18.829 MB 296 Uploading context 297 Step 1/2 : FROM busybox 298 ---> 769b9341d937 299 Step 2/2 : CMD echo Hello world 300 ---> Using cache 301 ---> 99cc1ad10469 302 Successfully built 99cc1ad10469 303 $ echo ".git" > .dockerignore 304 $ docker build . 305 Uploading context 6.76 MB 306 Uploading context 307 Step 1/2 : FROM busybox 308 ---> 769b9341d937 309 Step 2/2 : CMD echo Hello world 310 ---> Using cache 311 ---> 99cc1ad10469 312 Successfully built 99cc1ad10469 313 ``` 314 315 This example shows the use of the `.dockerignore` file to exclude the `.git` 316 directory from the context. Its effect can be seen in the changed size of the 317 uploaded context. The builder reference contains detailed information on 318 [creating a .dockerignore file](../builder.md#dockerignore-file) 319 320 ### Tag an image (-t) 321 322 ```bash 323 $ docker build -t vieux/apache:2.0 . 324 ``` 325 326 This will build like the previous example, but it will then tag the resulting 327 image. The repository name will be `vieux/apache` and the tag will be `2.0`. 328 [Read more about valid tags](tag.md). 329 330 You can apply multiple tags to an image. For example, you can apply the `latest` 331 tag to a newly built image and add another tag that references a specific 332 version. 333 For example, to tag an image both as `whenry/fedora-jboss:latest` and 334 `whenry/fedora-jboss:v2.1`, use the following: 335 336 ```bash 337 $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 . 338 ``` 339 ### Specify a Dockerfile (-f) 340 341 ```bash 342 $ docker build -f Dockerfile.debug . 343 ``` 344 345 This will use a file called `Dockerfile.debug` for the build instructions 346 instead of `Dockerfile`. 347 348 ```bash 349 $ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug . 350 $ docker build -f dockerfiles/Dockerfile.prod -t myapp_prod . 351 ``` 352 353 The above commands will build the current build context (as specified by the 354 `.`) twice, once using a debug version of a `Dockerfile` and once using a 355 production version. 356 357 ```bash 358 $ cd /home/me/myapp/some/dir/really/deep 359 $ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp 360 $ docker build -f ../../../../dockerfiles/debug /home/me/myapp 361 ``` 362 363 These two `docker build` commands do the exact same thing. They both use the 364 contents of the `debug` file instead of looking for a `Dockerfile` and will use 365 `/home/me/myapp` as the root of the build context. Note that `debug` is in the 366 directory structure of the build context, regardless of how you refer to it on 367 the command line. 368 369 > **Note:** 370 > `docker build` will return a `no such file or directory` error if the 371 > file or directory does not exist in the uploaded context. This may 372 > happen if there is no context, or if you specify a file that is 373 > elsewhere on the Host system. The context is limited to the current 374 > directory (and its children) for security reasons, and to ensure 375 > repeatable builds on remote Docker hosts. This is also the reason why 376 > `ADD ../file` will not work. 377 378 ### Use a custom parent cgroup (--cgroup-parent) 379 380 When `docker build` is run with the `--cgroup-parent` option the containers 381 used in the build will be run with the [corresponding `docker run` 382 flag](../run.md#specifying-custom-cgroups). 383 384 ### Set ulimits in container (--ulimit) 385 386 Using the `--ulimit` option with `docker build` will cause each build step's 387 container to be started using those [`--ulimit` 388 flag values](./run.md#set-ulimits-in-container-ulimit). 389 390 ### Set build-time variables (--build-arg) 391 392 You can use `ENV` instructions in a Dockerfile to define variable 393 values. These values persist in the built image. However, often 394 persistence is not what you want. Users want to specify variables differently 395 depending on which host they build an image on. 396 397 A good example is `http_proxy` or source versions for pulling intermediate 398 files. The `ARG` instruction lets Dockerfile authors define values that users 399 can set at build-time using the `--build-arg` flag: 400 401 ```bash 402 $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 . 403 ``` 404 405 This flag allows you to pass the build-time variables that are 406 accessed like regular environment variables in the `RUN` instruction of the 407 Dockerfile. Also, these values don't persist in the intermediate or final images 408 like `ENV` values do. 409 410 Using this flag will not alter the output you see when the `ARG` lines from the 411 Dockerfile are echoed during the build process. 412 413 For detailed information on using `ARG` and `ENV` instructions, see the 414 [Dockerfile reference](../builder.md). 415 416 ### Optional security options (--security-opt) 417 418 This flag is only supported on a daemon running on Windows, and only supports 419 the `credentialspec` option. The `credentialspec` must be in the format 420 `file://spec.txt` or `registry://keyname`. 421 422 ### Specify isolation technology for container (--isolation) 423 424 This option is useful in situations where you are running Docker containers on 425 Windows. The `--isolation=<value>` option sets a container's isolation 426 technology. On Linux, the only supported is the `default` option which uses 427 Linux namespaces. On Microsoft Windows, you can specify these values: 428 429 430 | Value | Description | 431 |-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| 432 | `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. | 433 | `process` | Namespace isolation only. | 434 | `hyperv` | Hyper-V hypervisor partition-based isolation. | 435 436 Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"`. 437 438 439 ### Squash an image's layers (--squash) **Experimental Only** 440 441 Once the image is built, squash the new layers into a new image with a single 442 new layer. Squashing does not destroy any existing image, rather it creates a new 443 image with the content of the squashed layers. This effectively makes it look 444 like all `Dockerfile` commands were created with a single layer. The build 445 cache is preserved with this method. 446 447 **Note**: using this option means the new image will not be able to take 448 advantage of layer sharing with other images and may use significantly more 449 space. 450 451 **Note**: using this option you may see significantly more space used due to 452 storing two copies of the image, one for the build cache with all the cache 453 layers in tact, and one for the squashed version.