Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i remove layers from my docker image?

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.

like image 723
Aaquib Khwaja Avatar asked May 15 '15 06:05

Aaquib Khwaja


2 Answers

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.

like image 62
Jorge Marey Avatar answered Nov 10 '22 11:11

Jorge Marey


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
like image 32
Ryabchenko Alexander Avatar answered Nov 10 '22 11:11

Ryabchenko Alexander