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