dockerdocker-image

What is docker's scratch image?


I'm new to docker and I was trying out the first hello world example in the docs. As I understand the hello-world image is based on top of the scratch image. Could someone please explain how the scratch image works? As I understand it is essentially blank. How is the binary executed in the hello-world image then?


Solution

  • The scratch image is the most minimal image in Docker. This is the base ancestor for all other images. The scratch image is actually empty. It doesn't contain any folders/files ...

    The scratch image is mostly used for building other base images. For instance, the debian image is built from scratch as such:

    FROM scratch
    ADD rootfs.tar.xz /
    CMD ["bash"]
    

    The rootfs.tar.xz contains all the files system files. The Debian image adds the filesystem folders to the scratch image, which is empty.

    As I understand it is essentially blank. How is the binary executed in the hello-world image then?

    The scratch image is blank.The hello-world executable added to the scratch image is actually statically compiled, meaning that it is self-contained and doesn't need any additional libraries to execute.

    As stated in the offical docker docs:

    Assuming you built the “hello” executable example from the Docker GitHub example C-source code, and you compiled it with the -static flag, you can then build this Docker image using: docker build --tag hello

    This confirms that the hello-world executable is statically compiled. For more info about static compiling, read here.