Home > Net >  How to reduce size of docker image by removing files and comitting
How to reduce size of docker image by removing files and comitting

Time:07-13

I have created a docker image and started installing many packages I needed for some tests.

However, after I was done, I realized I could remove some folders in order to reduce image size.

So that's what I did, and I commited those changes to a new image.

However, the image size remained the same. I saw someone with a similar issue here in SO but it seems there is an answer that explains that docker uses layers for its storage, so the image size only increases.

So my question is if it is possible to reduce image size by deleting folders and files or should I start from scratch?

docker image ls output for reference:

REPOSITORY                                                               TAG        IMAGE ID       CREATED         SIZE
abacate                                                                  melancia   9c1b3acdf62c   3 seconds ago   34.8GB
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   v2         68f6862f8371   9 minutes ago   34.8GB
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  v1         2090d0a74e9d   5 days ago      34.8GB

CodePudding user response:

yes, you should start from scratch. The initial layers are indeed adding content to your image, as the existing answer notes.

If you kept your Dockerfile, you should be able to just replay the changes you made to your original image. This is a key way of working with Docker. The outcome of a build process isn't very valuable, when the process is repeatable. Keep the Dockerfile under source control, and Docker images become almost as ephemeral as Docker containers.

  • Related