github.com/kobeld/docker@v1.12.0-rc1/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 With Powershell on Windows, you can run: 85 86 Get-Content Dockerfile | docker build - 87 88 If you use STDIN or specify a `URL`, the system places the contents into a file 89 called `Dockerfile`, and any `-f`, `--file` option is ignored. In this 90 scenario, there is no context. 91 92 By default the `docker build` command will look for a `Dockerfile` at the root 93 of the build context. The `-f`, `--file`, option lets you specify the path to 94 an alternative file to use instead. This is useful in cases where the same set 95 of files are used for multiple builds. The path must be to a file within the 96 build context. If a relative path is specified then it must to be relative to 97 the current directory. 98 99 In most cases, it's best to put each Dockerfile in an empty directory. Then, 100 add to that directory only the files needed for building the Dockerfile. To 101 increase the build's performance, you can exclude files and directories by 102 adding a `.dockerignore` file to that directory as well. For information on 103 creating one, see the [.dockerignore file](../builder.md#dockerignore-file). 104 105 If the Docker client loses connection to the daemon, the build is canceled. 106 This happens if you interrupt the Docker client with `CTRL-c` or if the Docker 107 client is killed for any reason. If the build initiated a pull which is still 108 running at the time the build is cancelled, the pull is cancelled as well. 109 110 ## Return code 111 112 On a successful build, a return code of success `0` will be returned. When the 113 build fails, a non-zero failure code will be returned. 114 115 There should be informational output of the reason for failure output to 116 `STDERR`: 117 118 $ docker build -t fail . 119 Sending build context to Docker daemon 2.048 kB 120 Sending build context to Docker daemon 121 Step 1 : FROM busybox 122 ---> 4986bf8c1536 123 Step 2 : RUN exit 13 124 ---> Running in e26670ec7a0a 125 INFO[0000] The command [/bin/sh -c exit 13] returned a non-zero code: 13 126 $ echo $? 127 1 128 129 See also: 130 131 [*Dockerfile Reference*](../builder.md). 132 133 ## Examples 134 135 ### Build with PATH 136 137 $ docker build . 138 Uploading context 10240 bytes 139 Step 1 : FROM busybox 140 Pulling repository busybox 141 ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/ 142 Step 2 : RUN ls -lh / 143 ---> Running in 9c9e81692ae9 144 total 24 145 drwxr-xr-x 2 root root 4.0K Mar 12 2013 bin 146 drwxr-xr-x 5 root root 4.0K Oct 19 00:19 dev 147 drwxr-xr-x 2 root root 4.0K Oct 19 00:19 etc 148 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 lib 149 lrwxrwxrwx 1 root root 3 Mar 12 2013 lib64 -> lib 150 dr-xr-xr-x 116 root root 0 Nov 15 23:34 proc 151 lrwxrwxrwx 1 root root 3 Mar 12 2013 sbin -> bin 152 dr-xr-xr-x 13 root root 0 Nov 15 23:34 sys 153 drwxr-xr-x 2 root root 4.0K Mar 12 2013 tmp 154 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 usr 155 ---> b35f4035db3f 156 Step 3 : CMD echo Hello world 157 ---> Running in 02071fceb21b 158 ---> f52f38b7823e 159 Successfully built f52f38b7823e 160 Removing intermediate container 9c9e81692ae9 161 Removing intermediate container 02071fceb21b 162 163 This example specifies that the `PATH` is `.`, and so all the files in the 164 local directory get `tar`d and sent to the Docker daemon. The `PATH` specifies 165 where to find the files for the "context" of the build on the Docker daemon. 166 Remember that the daemon could be running on a remote machine and that no 167 parsing of the Dockerfile happens at the client side (where you're running 168 `docker build`). That means that *all* the files at `PATH` get sent, not just 169 the ones listed to [*ADD*](../builder.md#add) in the Dockerfile. 170 171 The transfer of context from the local machine to the Docker daemon is what the 172 `docker` client means when you see the "Sending build context" message. 173 174 If you wish to keep the intermediate containers after the build is complete, 175 you must use `--rm=false`. This does not affect the build cache. 176 177 ### Build with URL 178 179 $ docker build github.com/creack/docker-firefox 180 181 This will clone the GitHub repository and use the cloned repository as context. 182 The Dockerfile at the root of the repository is used as Dockerfile. Note that 183 you can specify an arbitrary Git repository by using the `git://` or `git@` 184 scheme. 185 186 ### Build with - 187 188 $ docker build - < Dockerfile 189 190 This will read a Dockerfile from `STDIN` without context. Due to the lack of a 191 context, no contents of any local directory will be sent to the Docker daemon. 192 Since there is no context, a Dockerfile `ADD` only works if it refers to a 193 remote URL. 194 195 $ docker build - < context.tar.gz 196 197 This will build an image for a compressed context read from `STDIN`. Supported 198 formats are: bzip2, gzip and xz. 199 200 ### Usage of .dockerignore 201 202 $ docker build . 203 Uploading context 18.829 MB 204 Uploading context 205 Step 1 : FROM busybox 206 ---> 769b9341d937 207 Step 2 : CMD echo Hello world 208 ---> Using cache 209 ---> 99cc1ad10469 210 Successfully built 99cc1ad10469 211 $ echo ".git" > .dockerignore 212 $ docker build . 213 Uploading context 6.76 MB 214 Uploading context 215 Step 1 : FROM busybox 216 ---> 769b9341d937 217 Step 2 : CMD echo Hello world 218 ---> Using cache 219 ---> 99cc1ad10469 220 Successfully built 99cc1ad10469 221 222 This example shows the use of the `.dockerignore` file to exclude the `.git` 223 directory from the context. Its effect can be seen in the changed size of the 224 uploaded context. The builder reference contains detailed information on 225 [creating a .dockerignore file](../builder.md#dockerignore-file) 226 227 ### Tag image (-t) 228 229 $ docker build -t vieux/apache:2.0 . 230 231 This will build like the previous example, but it will then tag the resulting 232 image. The repository name will be `vieux/apache` and the tag will be `2.0`. 233 [Read more about valid tags](tag.md). 234 235 You can apply multiple tags to an image. For example, you can apply the `latest` 236 tag to a newly built image and add another tag that references a specific 237 version. 238 For example, to tag an image both as `whenry/fedora-jboss:latest` and 239 `whenry/fedora-jboss:v2.1`, use the following: 240 241 $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 . 242 243 ### Specify Dockerfile (-f) 244 245 $ docker build -f Dockerfile.debug . 246 247 This will use a file called `Dockerfile.debug` for the build instructions 248 instead of `Dockerfile`. 249 250 $ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug . 251 $ docker build -f dockerfiles/Dockerfile.prod -t myapp_prod . 252 253 The above commands will build the current build context (as specified by the 254 `.`) twice, once using a debug version of a `Dockerfile` and once using a 255 production version. 256 257 $ cd /home/me/myapp/some/dir/really/deep 258 $ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp 259 $ docker build -f ../../../../dockerfiles/debug /home/me/myapp 260 261 These two `docker build` commands do the exact same thing. They both use the 262 contents of the `debug` file instead of looking for a `Dockerfile` and will use 263 `/home/me/myapp` as the root of the build context. Note that `debug` is in the 264 directory structure of the build context, regardless of how you refer to it on 265 the command line. 266 267 > **Note:** 268 > `docker build` will return a `no such file or directory` error if the 269 > file or directory does not exist in the uploaded context. This may 270 > happen if there is no context, or if you specify a file that is 271 > elsewhere on the Host system. The context is limited to the current 272 > directory (and its children) for security reasons, and to ensure 273 > repeatable builds on remote Docker hosts. This is also the reason why 274 > `ADD ../file` will not work. 275 276 ### Optional parent cgroup (--cgroup-parent) 277 278 When `docker build` is run with the `--cgroup-parent` option the containers 279 used in the build will be run with the [corresponding `docker run` 280 flag](../run.md#specifying-custom-cgroups). 281 282 ### Set ulimits in container (--ulimit) 283 284 Using the `--ulimit` option with `docker build` will cause each build step's 285 container to be started using those [`--ulimit` 286 flag values](./run.md#set-ulimits-in-container-ulimit). 287 288 ### Set build-time variables (--build-arg) 289 290 You can use `ENV` instructions in a Dockerfile to define variable 291 values. These values persist in the built image. However, often 292 persistence is not what you want. Users want to specify variables differently 293 depending on which host they build an image on. 294 295 A good example is `http_proxy` or source versions for pulling intermediate 296 files. The `ARG` instruction lets Dockerfile authors define values that users 297 can set at build-time using the `--build-arg` flag: 298 299 $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 . 300 301 This flag allows you to pass the build-time variables that are 302 accessed like regular environment variables in the `RUN` instruction of the 303 Dockerfile. Also, these values don't persist in the intermediate or final images 304 like `ENV` values do. 305 306 Using this flag will not alter the output you see when the `ARG` lines from the 307 Dockerfile are echoed during the build process. 308 309 For detailed information on using `ARG` and `ENV` instructions, see the 310 [Dockerfile reference](../builder.md). 311 312 ### Specify isolation technology for container (--isolation) 313 314 This option is useful in situations where you are running Docker containers on 315 Windows. The `--isolation=<value>` option sets a container's isolation 316 technology. On Linux, the only supported is the `default` option which uses 317 Linux namespaces. On Microsoft Windows, you can specify these values: 318 319 320 | Value | Description | 321 |-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| 322 | `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. | 323 | `process` | Namespace isolation only. | 324 | `hyperv` | Hyper-V hypervisor partition-based isolation. | 325 326 Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"`.