How can i remove layers from my docker image?
Asked Answered
U

2

12

I have build a docker image by making incremental commits. This has led to the creation of a lot of layers in my docker image and subsequently the size of the image has gone very large.

Is there a way to remove the layers and as a result reduce the size of the image ?

Any help would be appreciated.

Upi answered 15/5, 2015 at 6:44 Comment(1)
You'll find the process much more efficient, repeatable, and controllable to build with a Dockerfile instead of incremental commits.Schriever
T
11

You could try to export the image and then import it again. By doing it this way all the layers will be lost and your image size will be lower.

sudo docker export red_panda > exampleimage.tar
cat exampleimage.tar | sudo docker import - exampleimagelocal:new

Note that this only works with containers, so you will need to launch one from the image and then do the trick.

Hope it helps.

Thromboplastin answered 15/5, 2015 at 7:9 Comment(4)
I exported the image, and then imported it on a different machine. I wasn't able to run that image on that machine. It said "unable to find image locally". What might be the issue ?Upi
Hi, which command did you use to import it? Did you use the same image name for the import and the run command? If you execute docker images does the image lists there?Thromboplastin
Can you execute docker images to see if the imported image is listed?Thromboplastin
I have a message: docker: Error response from daemon: pull access denied for xxxreduce, repository does not exist or may require 'docker login'.Aubade
I
9

You can squash layers with next trick

FROM oracle AS needs-squashing
ENV NEEDED_VAR some_value
COPY ./giant.zip ./somewhere/giant.zip
RUN echo "install giant in zip"
RUN rm ./somewhere/giant.zip

FROM scratch
COPY --from=needs-squashing / /
ENV NEEDED_VAR some_value
Inversely answered 15/4, 2019 at 17:58 Comment(1)
there's a one issue with this approach - if you have environment variables from the container from previous layer that are required for it to work, and you go with FROM scratch approach as you have presented here, the variables are not retained - for example you can lose $JAVA_HOME due to that - so use with care!Identification

© 2022 - 2024 — McMap. All rights reserved.