docker size is become too big after I commit

Viewed 27

When I commit the docker image it become 60G size.
so I tired to minimize the docker size then I delete some files inside of the docker
after that I recommit the image but it become bigger....
1. which space is affect to the docker commit size?
2. how should I minimize the size of the docker after I commit?

1 Answers

Docker doesn't just store the final state of the image. It stores every change you make. So if you add a 1 GB file, and then immediately delete it, it will add 2 GB to the size even though you have 0 GB of files. (I think in practice this example will be less than 2 GB because they compress it, but you get the idea)

If you want to keep your Docker images small, you should create as few files as possible. Don't add a 1 GB file if you will delete it later, just don't add it in the first place.

If you really need to create and delete a large file, the trick is to do it in a single layer. That means you have to do a single Docker command. You can do things like RUN ./create_big_file.sh && ./do_something.sh && rm big_file.bin. Or you can write a script like:

#!/bin/sh

./create_big_file.sh
./do_something.sh
rm big_file.bin

and then in your Dockerfile do:

COPY my_script.sh
RUN ./my_script.sh
Related