Skip to content

Commit 82e5cb7

Browse files
author
Misty Stanley-Jones
authored
Update info on dockerignore, add link to multistage builds (docker#4953)
1 parent a8677b3 commit 82e5cb7

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

engine/userguide/eng-image/dockerfile_best-practices.md

+34-12
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ specific set of instructions. You can learn the basics on the
1717
you’re new to writing `Dockerfile`s, you should start there.
1818

1919
This document covers the best practices and methods recommended by Docker,
20-
Inc. and the Docker community for creating easy-to-use, effective
21-
`Dockerfile`s. We strongly suggest you follow these recommendations (in fact,
22-
if you’re creating an Official Image, you *must* adhere to these practices).
20+
Inc. and the Docker community for building efficient images. To see many of
21+
these practices and recommendations in action, check out the Dockerfile for
22+
[buildpack-deps](https://github.com/docker-library/buildpack-deps/blob/master/jessie/Dockerfile).
2323

24-
You can see many of these practices and recommendations in action in the [buildpack-deps `Dockerfile`](https://github.com/docker-library/buildpack-deps/blob/master/jessie/Dockerfile).
25-
26-
> Note: for more detailed explanations of any of the Dockerfile commands
24+
> **Note**: for more detailed explanations of any of the Dockerfile commands
2725
>mentioned here, visit the [Dockerfile Reference](../../reference/builder.md) page.
2826
2927
## General guidelines and recommendations
@@ -40,12 +38,36 @@ stateless fashion.
4038

4139
### Use a .dockerignore file
4240

43-
In most cases, it's best to put each Dockerfile in an empty directory. Then,
44-
add to that directory only the files needed for building the Dockerfile. To
45-
increase the build's performance, you can exclude files and directories by
46-
adding a `.dockerignore` file to that directory as well. This file supports
47-
exclusion patterns similar to `.gitignore` files. For information on creating one,
48-
see the [.dockerignore file](../../reference/builder.md#dockerignore-file).
41+
The current working directory where you are located when you issue a
42+
`docker build` command is called the _build context_, and the `Dockerfile` must
43+
be somewhere within this build context. By default, it is assumed to be in the
44+
current directory, but you can specify a different location by using the `-f`
45+
flag. Regardless of where the `Dockerfile` actually lives, all of the recursive
46+
contents of files and directories in the current directory are sent to the
47+
Docker daemon as the _build context_. Inadvertently including files that are not
48+
necessary for building the image results in a larger build context and larger
49+
image size. These in turn can increase build time, time to pull and push the
50+
image, and the runtime size of containers. To see how big your build context
51+
is, look for a message like the following, when you build your `Dockerfile`.
52+
53+
```none
54+
Sending build context to Docker daemon 187.8MB
55+
```
56+
57+
To exclude files which are not relevant to the build, without restructuring your
58+
source repisotiry, use a `.dockerignore` file. This file supports
59+
exclusion patterns similar to `.gitignore` files. For information on creating
60+
one, see the [.dockerignore file](../../reference/builder.md#dockerignore-file).
61+
In addition to using a `.dockerignore` file, check out the information below
62+
on [multi-stage builds](#use-multi-stage-builds).
63+
64+
### Use multi-stage builds
65+
66+
If you use Docker 17.05 or higher, you can use
67+
[multi-stage builds](/engine/userguide/eng-image/multistage-build.md) to
68+
drastically reduce the size of your final image, without the need to
69+
jump through hoops to reduce the number of intermediate layers or remove
70+
intermediate files during the build.
4971

5072
### Avoid installing unnecessary packages
5173

0 commit comments

Comments
 (0)