Read More
Read More
Menu Close

    How to reduce the size of Docker container or images

    I recently played a lot with Docker, while setting up an Unique Demo per customer for our Odoo Modules like ODOO Marketplace Demo , ODOO Affliate Program Demo, etc. It was nice experience for me, I was able to create Docker images and containers very easily, thanks to the awesome & detailed documentation on Docker site. Everything was working right out of the box!

    Problem 1: But after few weeks I got a low disk space problem on that server, I realised that there are too many images present on that server (as I rebuild images when doing any updates/fixes) when I tried to remove unused images, I got an error message as they are the parent of new image. i.e I can`t remove those unused image anymore as they are linked to new images now 🙁

    Problem 2: Also, as I`m using overlay2 storage driver , I found that a few GB tied up at /var/lib/docker/overlay2:

    [email protected]:/# du -sh /var/lib/docker/overlay2/
    
    49G               /var/lib/docker/overlay2

    After spending few hours on this problem finally I got a fix for this, which today i`ll share with all via this blog.

    Follow the steps:

    Start your headless eCommerce
    now.
    Read More
    • STEP 1: Take backup of all your current(active) images as a tar file, using import/export method. Benefit of doing it – is it will not preserve the history(parent-images) & meta-data of container, i.e size of image will reduced around 40% (depending on the layers it have)
    # Create a new container using that image
    docker run -tid --name=<UNIQUE-CONTAINER-NAME> <IMAGE-NAME> bash
    
    # export the container to a tarball
    docker export <UNIQUE-CONTAINER-NAME> -o image-name.tar
    • STEP 2: Now, stop the docker service, and rm -rf /var/lib/docker. Alternatively, you can stop the docker service, move the directory (as a backup), and start the service again.
    # Stop docker service
    service docker stop
    
    # Remove docker folder
    rm -rf /var/lib/docker
    
    # Start docker service
    service docker start
    • STEP 3: Now, import back all your saved docker images in step 1 as:
    # import back the saved image tarball
    cat image-name.tar | docker import - image-name:latest

    That`s it.

    In this way, I saved few GBs of my server disk space, but still my task is not over I`m still trying to do more optimisation on docker`s image level, i`ll try to share that also if I found any good workaround/hack for that.

    What I learned while Working on Docker is – “Building docker images is pretty pretty easy but building small docker images is not always easy.

    TWEAK: To commit/build any image directly from container we can use following command to get small-sized image (when we don`t want any history/meta-data):

    docker export <CONTAINER-ID/NAME> | docker import - <UNIQUE-IMAGE-NAME>

    Thanks for reading this blog !!!
    I hope it will help someone in the future. And by someone I most likely mean by me ! 😉

    Your opinions, comments and suggestions are important to keep this page updated and more useful !!!

    
    

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top