github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/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 weight=1 9 +++ 10 <![end-metadata]--> 11 12 # build 13 14 Usage: docker build [OPTIONS] PATH | URL | - 15 16 Build a new image from the source code at PATH 17 18 -f, --file="" Name of the Dockerfile (Default is 'PATH/Dockerfile') 19 --force-rm=false Always remove intermediate containers 20 --build-arg=[] Set build-time variables 21 --no-cache=false Do not use cache when building the image 22 --pull=false Always attempt to pull a newer version of the image 23 -q, --quiet=false Suppress the verbose output generated by the containers 24 --rm=true Remove intermediate containers after a successful build 25 -t, --tag="" Repository name (and optionally a tag) for the image 26 -m, --memory="" Memory limit for all build containers 27 --memory-swap="" Total memory (memory + swap), `-1` to disable swap 28 -c, --cpu-shares CPU Shares (relative weight) 29 --cpuset-mems="" MEMs in which to allow execution, e.g. `0-3`, `0,1` 30 --cpuset-cpus="" CPUs in which to allow execution, e.g. `0-3`, `0,1` 31 --cgroup-parent="" Optional parent cgroup for the container 32 --ulimit=[] Ulimit options 33 34 Builds Docker images from a Dockerfile and a "context". A build's context is 35 the files located in the specified `PATH` or `URL`. The build process can refer 36 to any of the files in the context. For example, your build can use an 37 [*ADD*](/reference/builder/#add) instruction to reference a file in the 38 context. 39 40 The `URL` parameter can specify the location of a Git repository; the repository 41 acts as the build context. The system recursively clones the repository and its 42 submodules using a `git clone --depth 1 --recursive` command. This command runs 43 in a temporary directory on your local host. After the command succeeds, the 44 directory is sent to the Docker daemon as the context. Local clones give you the 45 ability to access private repositories using local user credentials, VPNs, and 46 so forth. 47 48 Git URLs accept context configuration in their fragment section, separated by a 49 colon `:`. The first part represents the reference that Git will check out, 50 this can be either a branch, a tag, or a commit SHA. The second part represents 51 a subdirectory inside the repository that will be used as a build context. 52 53 For example, run this command to use a directory called `docker` in the branch 54 `container`: 55 56 $ docker build https://github.com/docker/rootfs.git#container:docker 57 58 The following table represents all the valid suffixes with their build 59 contexts: 60 61 Build Syntax Suffix | Commit Used | Build Context Used 62 --------------------|-------------|------------------- 63 `myrepo.git` | `refs/heads/master` | `/` 64 `myrepo.git#mytag` | `refs/tags/mytag` | `/` 65 `myrepo.git#mybranch` | `refs/heads/mybranch` | `/` 66 `myrepo.git#abcdef` | `sha1 = abcdef` | `/` 67 `myrepo.git#:myfolder` | `refs/heads/master` | `/myfolder` 68 `myrepo.git#master:myfolder` | `refs/heads/master` | `/myfolder` 69 `myrepo.git#mytag:myfolder` | `refs/tags/mytag` | `/myfolder` 70 `myrepo.git#mybranch:myfolder` | `refs/heads/mybranch` | `/myfolder` 71 `myrepo.git#abcdef:myfolder` | `sha1 = abcdef` | `/myfolder` 72 73 Instead of specifying a context, you can pass a single Dockerfile in the `URL` 74 or pipe the file in via `STDIN`. To pipe a Dockerfile from `STDIN`: 75 76 docker build - < Dockerfile 77 78 If you use STDIN or specify a `URL`, the system places the contents into a file 79 called `Dockerfile`, and any `-f`, `--file` option is ignored. In this 80 scenario, there is no context. 81 82 By default the `docker build` command will look for a `Dockerfile` at the root 83 of the build context. The `-f`, `--file`, option lets you specify the path to 84 an alternative file to use instead. This is useful in cases where the same set 85 of files are used for multiple builds. The path must be to a file within the 86 build context. If a relative path is specified then it must to be relative to 87 the current directory. 88 89 In most cases, it's best to put each Dockerfile in an empty directory. Then, 90 add to that directory only the files needed for building the Dockerfile. To 91 increase the build's performance, you can exclude files and directories by 92 adding a `.dockerignore` file to that directory as well. For information on 93 creating one, see the [.dockerignore file](/reference/builder#dockerignore-file). 94 95 If the Docker client loses connection to the daemon, the build is canceled. 96 This happens if you interrupt the Docker client with `ctrl-c` or if the Docker 97 client is killed for any reason. 98 99 > **Note:** 100 > Currently only the "run" phase of the build can be canceled until pull 101 > cancellation is implemented). 102 103 ## Return code 104 105 On a successful build, a return code of success `0` will be returned. When the 106 build fails, a non-zero failure code will be returned. 107 108 There should be informational output of the reason for failure output to 109 `STDERR`: 110 111 $ docker build -t fail . 112 Sending build context to Docker daemon 2.048 kB 113 Sending build context to Docker daemon 114 Step 1 : FROM busybox 115 ---> 4986bf8c1536 116 Step 2 : RUN exit 13 117 ---> Running in e26670ec7a0a 118 INFO[0000] The command [/bin/sh -c exit 13] returned a non-zero code: 13 119 $ echo $? 120 1 121 122 See also: 123 124 [*Dockerfile Reference*](/reference/builder). 125 126 ## Examples 127 128 $ docker build . 129 Uploading context 10240 bytes 130 Step 1 : FROM busybox 131 Pulling repository busybox 132 ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/ 133 Step 2 : RUN ls -lh / 134 ---> Running in 9c9e81692ae9 135 total 24 136 drwxr-xr-x 2 root root 4.0K Mar 12 2013 bin 137 drwxr-xr-x 5 root root 4.0K Oct 19 00:19 dev 138 drwxr-xr-x 2 root root 4.0K Oct 19 00:19 etc 139 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 lib 140 lrwxrwxrwx 1 root root 3 Mar 12 2013 lib64 -> lib 141 dr-xr-xr-x 116 root root 0 Nov 15 23:34 proc 142 lrwxrwxrwx 1 root root 3 Mar 12 2013 sbin -> bin 143 dr-xr-xr-x 13 root root 0 Nov 15 23:34 sys 144 drwxr-xr-x 2 root root 4.0K Mar 12 2013 tmp 145 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 usr 146 ---> b35f4035db3f 147 Step 3 : CMD echo Hello world 148 ---> Running in 02071fceb21b 149 ---> f52f38b7823e 150 Successfully built f52f38b7823e 151 Removing intermediate container 9c9e81692ae9 152 Removing intermediate container 02071fceb21b 153 154 This example specifies that the `PATH` is `.`, and so all the files in the 155 local directory get `tar`d and sent to the Docker daemon. The `PATH` specifies 156 where to find the files for the "context" of the build on the Docker daemon. 157 Remember that the daemon could be running on a remote machine and that no 158 parsing of the Dockerfile happens at the client side (where you're running 159 `docker build`). That means that *all* the files at `PATH` get sent, not just 160 the ones listed to [*ADD*](/reference/builder/#add) in the Dockerfile. 161 162 The transfer of context from the local machine to the Docker daemon is what the 163 `docker` client means when you see the "Sending build context" message. 164 165 If you wish to keep the intermediate containers after the build is complete, 166 you must use `--rm=false`. This does not affect the build cache. 167 168 $ docker build . 169 Uploading context 18.829 MB 170 Uploading context 171 Step 1 : FROM busybox 172 ---> 769b9341d937 173 Step 2 : CMD echo Hello world 174 ---> Using cache 175 ---> 99cc1ad10469 176 Successfully built 99cc1ad10469 177 $ echo ".git" > .dockerignore 178 $ docker build . 179 Uploading context 6.76 MB 180 Uploading context 181 Step 1 : FROM busybox 182 ---> 769b9341d937 183 Step 2 : CMD echo Hello world 184 ---> Using cache 185 ---> 99cc1ad10469 186 Successfully built 99cc1ad10469 187 188 This example shows the use of the `.dockerignore` file to exclude the `.git` 189 directory from the context. Its effect can be seen in the changed size of the 190 uploaded context. The builder reference contains detailed information on 191 [creating a .dockerignore file](../../builder/#dockerignore-file) 192 193 $ docker build -t vieux/apache:2.0 . 194 195 This will build like the previous example, but it will then tag the resulting 196 image. The repository name will be `vieux/apache` and the tag will be `2.0` 197 198 $ docker build - < Dockerfile 199 200 This will read a Dockerfile from `STDIN` without context. Due to the lack of a 201 context, no contents of any local directory will be sent to the Docker daemon. 202 Since there is no context, a Dockerfile `ADD` only works if it refers to a 203 remote URL. 204 205 $ docker build - < context.tar.gz 206 207 This will build an image for a compressed context read from `STDIN`. Supported 208 formats are: bzip2, gzip and xz. 209 210 $ docker build github.com/creack/docker-firefox 211 212 This will clone the GitHub repository and use the cloned repository as context. 213 The Dockerfile at the root of the repository is used as Dockerfile. Note that 214 you can specify an arbitrary Git repository by using the `git://` or `git@` 215 schema. 216 217 $ docker build -f Dockerfile.debug . 218 219 This will use a file called `Dockerfile.debug` for the build instructions 220 instead of `Dockerfile`. 221 222 $ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug . 223 $ docker build -f dockerfiles/Dockerfile.prod -t myapp_prod . 224 225 The above commands will build the current build context (as specified by the 226 `.`) twice, once using a debug version of a `Dockerfile` and once using a 227 production version. 228 229 $ cd /home/me/myapp/some/dir/really/deep 230 $ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp 231 $ docker build -f ../../../../dockerfiles/debug /home/me/myapp 232 233 These two `docker build` commands do the exact same thing. They both use the 234 contents of the `debug` file instead of looking for a `Dockerfile` and will use 235 `/home/me/myapp` as the root of the build context. Note that `debug` is in the 236 directory structure of the build context, regardless of how you refer to it on 237 the command line. 238 239 > **Note:** 240 > `docker build` will return a `no such file or directory` error if the 241 > file or directory does not exist in the uploaded context. This may 242 > happen if there is no context, or if you specify a file that is 243 > elsewhere on the Host system. The context is limited to the current 244 > directory (and its children) for security reasons, and to ensure 245 > repeatable builds on remote Docker hosts. This is also the reason why 246 > `ADD ../file` will not work. 247 248 When `docker build` is run with the `--cgroup-parent` option the containers 249 used in the build will be run with the [corresponding `docker run` 250 flag](/reference/run/#specifying-custom-cgroups). 251 252 Using the `--ulimit` option with `docker build` will cause each build step's 253 container to be started using those [`--ulimit` 254 flag values](/reference/run/#setting-ulimits-in-a-container). 255 256 You can use `ENV` instructions in a Dockerfile to define variable 257 values. These values persist in the built image. However, often 258 persistence is not what you want. Users want to specify variables differently 259 depending on which host they build an image on. 260 261 A good example is `http_proxy` or source versions for pulling intermediate 262 files. The `ARG` instruction lets Dockerfile authors define values that users 263 can set at build-time using the `---build-arg` flag: 264 265 $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 . 266 267 This flag allows you to pass the build-time variables that are 268 accessed like regular environment variables in the `RUN` instruction of the 269 Dockerfile. Also, these values don't persist in the intermediate or final images 270 like `ENV` values do. 271 272 For detailed information on using `ARG` and `ENV` instructions, see the 273 [Dockerfile reference](/reference/builder).