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