Does build context size affect the image size?
Asked Answered
L

3

7

When I build images with docker build the output contains the context size, e.g.:

Sending build context to Docker daemon  1.315GB
[...]
Successfully built a9ec4d33e12e

Does this size affect the resulting image size? Should I seek to reduce it?

Lazaro answered 9/1, 2020 at 13:20 Comment(0)
P
4

Does this size affect the resulting image size?

No

Should I seek to reduce it?

Yes. It saves time building the image.

source:

https://medium.com/better-programming/docker-tips-about-the-build-context-dbc76505e178

Pilotage answered 9/1, 2020 at 13:24 Comment(0)
T
7

When you run docker build it sends all of the files in the Dockerfile's directory and all subdirectories over to the Docker daemon. It does this under the assumption that your Dockerfile is probably going to reference all of those files, but note that it doesn't check your Dockerfile to figure out what files to send. It simply sends everything.

There are a couple ways to reduce the build context size:

  • Put your Dockerfile in its own directory with only the files it needs. For instance, instead of having a Dockerfile at the top level of your project, put it in its own subdirectory.

  • Write a .dockerignore file that lists files to exclude from the build context. When you can't move files out of the way this is the next best thing.

Does this size affect the resulting image size?

Yes and no. An overly large build context won't increase the image size. Extra files in the build context that aren't used by the Dockerfile are simply thrown away.

However, if you use .dockerignore to ignore files that the Dockerfile does use that can reduce the image size (and potentially break it). Ignoring files will cause COPY . /app to copy fewer files, for instance.

Should I seek to reduce it?

Yes. It'll speed up the build process.

Thermotaxis answered 9/1, 2020 at 13:43 Comment(0)
F
5

The build context can in some cases impact the size of the image, yes.

If you do:

COPY . /app

Then everything in the context will be copied to /app in the image.

If you're only copying explicit subsets of the context, e.g. COPY mydir/ app/, then the context size won't impact image size, only the contents of mydir will impact image size.

You should create a .dockerignore that omits files you don't care about.

Fiesole answered 9/1, 2020 at 13:32 Comment(0)
P
4

Does this size affect the resulting image size?

No

Should I seek to reduce it?

Yes. It saves time building the image.

source:

https://medium.com/better-programming/docker-tips-about-the-build-context-dbc76505e178

Pilotage answered 9/1, 2020 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.