Why does the image size increase when the same file is copied to the same location multiple times in the Dockerfile?

Asked 1 years ago, Updated 1 years ago, 504 views

Why is the image size of a container larger than a single copy of a docker build when the same file is COPYed multiple times in the same location in a docker file?

For Dockerfile as shown below.

 FROM python: 3.9.13

WORKDIR/app

COPY./app
COPY./app

In the current directory, test.txt has several MB of text files in addition to Dockerfile.

 through /test$ls
Dockerfile test.txt

docker build is performed as follows:

~/test$docker build-t test.

docker docker-compose

2023-02-01 08:38

2 Answers

In jhashimoto's answer, "Why is the image size of the container larger than it would have been if I copied it once?" so I will add a little more.

Best Practices for Dockerfile Description

Each command generates one layer at a time.
(omitted)
Once the image is run to generate a container, a writable layer ("container layer") is added over the previous layer. Changes to running containers, such as creating new files, editing existing files, and deleting files, are all written to this write layer.

In other words, whatever the layer below is, all writes made in COPY will be represented on the new layer.
I don't understand the expectation that the difference will be detected, but it hasn't happened.This is the same with the regular cp command, which determines whether it can be overwritten, but always overwrites without comparing source and destination content.


2023-02-01 08:42

I think it is because layers are generated every time the COPY command is issued.

Best Practices for Dockerfile Description|Docker Documents

Each command generates one layer at a time.

caching-Docker have the same file in multiple layers-Stack Overflow

I had a similar QA in my home.


2023-02-01 09:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.