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