Creating a simple parent base docker image using scratch

Spread the Knowledge

You can use Docker’s reserved, minimal image, scratch, as a starting point for building containers. Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.

There is a scratch image on docker hub, but it’s essentially just the canvas for creating base images, it’s essentially useless until you setup an OS and create a new image from it. you’ll likely never need to use it unless you’ve written your own operating system, as just about every existing platform is already present in some form or another as a base image for you to build upon.

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.

While scratch appears in Docker’s repository on the hub, you can’t pull it, run it, or tag any image with the name scratch. Instead, you can refer to it in your Dockerfile. For example, to create a minimal container using scratch:

FROM scratch
ADD hello /
CMD ["/hello"]

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 .

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.

$ docker run --rm -it -v $PWD:/build ubuntu:16.04
container# apt-get update && apt-get install build-essential
container# cd /build
container# gcc -o hello -static -nostartfiles hello.c

Then you can run it (on Linux, Mac, or Windows) using: docker run –rm hello

There are more example scripts for creating parent images in the Docker GitHub Repo:

There are one very good reference of understanding and creating the docker images from scratch are

Rajesh Kumar
Latest posts by Rajesh Kumar (see all)