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