Signup/Sign In

Make your Docker images slimmer!

Posted in Cloud   LAST UPDATED: MARCH 18, 2023

    Docker is a popular platform for containerizing applications, allowing developers to package their applications with all the necessary dependencies and configurations into a single unit that can be easily deployed across different environments. However, one common issue with Docker images is their large size, which can lead to slow build times and increased storage costs. This is where the Docker-slim tool comes in handy.

    Docker-slim is an popular open-source DevOps tool that helps create slim/minimal Docker images by optimizing the container size and removing unnecessary files, packages and dependencies. In this article, we will guide you on how to install Docker -slim and use it to create a slim Docker image for Golang compiler.

    Docker-Slim Installation

    Docker-slim can be installed in different ways depending on your platform, as shown below. But first we need to download executable binaries:

    Download Slim tool

    You can download the Docker-slim binary for your platform using the links provided below:

    Once downloaded, extract the archive and optionally move the executable binaries to your bin directory or any directory which is in $PATH. For Linux/Mac, use the following commands:

    tar -xvf ds.tar.gz
    mv ds/* /usr/local/bin/.

    You can use slim update to update slim to latest version after installation.

    Scripted Install

    Another way to install Docker-slim is by using a script on Linux and macOS platforms. Use the following command to start bash script which installs slim tool on your machine:

    curl -sL https://raw.githubusercontent.com/slimtoolkit/slim/master/scripts/install-slim.sh | sudo -E bash -

    Homebrew

    If you are using Homebrew package manager on macOS, you can install Docker-slim using the following command:

    brew install docker-slim

    Creating a Slim Docker Image for Go

    We have a custom Dockerfile for building Go compiler image as follows:

    FROM golang:1.16
    RUN apt-get -y install git
    WORKDIR /usr/src/app
    RUN git clone https://github.com/kr/godep.git
    COPY . .
    CMD ["tail", "-f",  "/dev/null"]

    We can see that few commands we need to keep for a functional Go compiler which are as follows:

    • go - Golang executable for compiling and executing programs.
    • apt-get - Installing git.
    • git - Download extra dependencies for Go.
    • tail - Output appended data to /dev/null file.
    • ls, cd, cp, rm, mv, bash - Optionals tools to do basic operations in Linux
    1. Build docker image using normal method. In this example, we will use the docker build command for building the image from Dockerfile file.
    docker build -t go-env:latest .

    Check the new image we built:

    $ docker images
    REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
    go-env       latest    c27eca3990ed   13 seconds ago   921MB
    1. Next command will analyse the target container image (in this case, Go with debian distro image), along with the go program, and build a new optimized image named go-env:slim.
    slim build --target go-env:latest --tag go-env:slim --http-probe=false --exec "go version"
    • The --http-probe=false option disables the HTTP probe that docker-slim uses to verify that the container is running correctly.
    • The --exec option specifies the command to run inside the container to determine the application's runtime dependencies. (makes sure that this package is not removed to make the image slim)

    You can check images using the command below:

    $ docker images
    REPOSITORY                TAG       IMAGE ID       CREATED          SIZE
    go-env                    slim      65d9cdf799a6   5 seconds ago    17.1MB
    docker-slim-empty-image   latest    3f098d6f3d22   9 seconds ago    0B
    go-env                    latest    c27eca3990ed   10 minutes ago   921MB

    Similarly, we can specify other tools to keep in the go docker image by separating with && operater as follows:

    $ slim build --target go-env:latest --tag go-env:slim1 --http-probe=false --exec "go version && bash && mv --help && cp --help && cat --help && ls --help && tail --help && apt-get --help && git --help"

    Slim build docker image

    1. Finally, you can verify that the new slim image is significantly smaller than the original image by running the docker images or docker image ls command:
    $ docker images
    REPOSITORY                TAG       IMAGE ID       CREATED          SIZE
    go-env                    slim1     4433a43fec5d   5 seconds ago    32.8MB
    go-env                    slim      65d9cdf799a6   3 minutes ago    17.1MB
    docker-slim-empty-image   latest    3f098d6f3d22   3 minutes ago    0B
    go-env                    latest    c27eca3990ed   14 minutes ago   921MB

    This command will display the list of Docker images, along with their sizes. You should see that the new slim image takes up significantly less space than the original image.

    Conclusion

    Creating slim Docker images using docker-slim is a straightforward process that can help reduce the size of your Docker images and improve their security and performance. With docker-slim, you can analyze your container image and application, and build a new optimized image that takes up less space and consumes fewer resources.

    About the author:
    Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.
    Tags:cloud-computinghowtodocker
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS