Reading list Switch to dark mode

    Check docker image efficiency and reduce image size

    Updated 23 November 2022

    The major concept of docker is that it uses an isolated filesystem known as a docker image while all docker containers run as a layer on these images. Any change in container does not affect the docker image at all, unless the container commits to image and makes the changes permanent. As these commits increase, the docker image size also increases exponentially. We will discuss here how to check docker image efficiency and reduce image size for better performance and save space as well.

    The reason behind the image size increasing exponentially on each commit is that the docker image keeps track of the changes in each commit i.e. layer. For example, if you change permissions for files in container and commit to image, the layer will have both files to keep track of the changes. This results in heavy size of the image. In this case, the best way to reduce image size is to remove these unnecessary files history from image commits by merging all commits into one. Let’s discuss how to do this:

    Check image efficiency

    First of all we need to check the image efficiency and how much space we can save. There is a way to check potential space to save in an image, as well as check each layer content. We will use wagoodman/dive docker image for this purpose. Open terminal and run this command:

    docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive:latest {your_image_id}

    This command will take sometime according to image size. After the given image is loaded in this container, it will display image details and layers like this:

    Searching for an experienced
    Prestashop Company ?
    Find out More
    image-140

    As you can see there is a possibility to save 377 MB in this image. You can also navigate through each layer and check changes in each layer of this image.

    Reduce image size

    By performing last step, we know that the image has multiple layers and we can save some space by merging these layers. We can do this by docker export/import commands. These commands already merge all commits into single layer. We need to perform 3 steps to reduce image size:

    • Export image container to tar file
    • Check old image history to collect docker instructions
    • Import image with docker commands

    1. Export image container to tar file

    Since docker export works on container, we need to run a container with our target image. Then run this command to export this container to a tar file:

    docker export {container_id} > {image_zip_name}.tar

    2. Check old image history to collect docker instructions

    When we export docker image, the filesystem is exported but the docker instructions such as “ENTRYPOINT” are lost. Hence we need to accumulate all these instructions and mention while importing the image. For example, in following image history:

    image-141

    The docker instructions are:

    • ENTRYPOINT[“docker-php-entrypoint”]
    • WORKDIR /var/www/html
    • EXPOSE 80
    • CMD [“apache2-foreground”]

    We will configure these instructions while importing the docker image file.

    3. Import image with docker commands

    Once you have collected all docker instructions from history, run below command to import image again:

    docker import \
    --change 'ENTRYPOINT ["docker-php-entrypoint"]' \
    --change 'WORKDIR /var/www/html' \
    --change 'EXPOSE 80' \
    --change 'CMD ["apache2-foreground"]' \
    image_zip_name.tar {image_name}:{tag_name}

    As you can see, we have imported the image tar file and changed the docker instructions to match with old image file. Now, you can see the reduced docker image size and almost all potential space is saved.

    That’s all about how to check docker image efficiency and reduce image size. If any issue or doubt in the above process, please feel free to let us know in the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected].

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home