DEV Community

Cover image for Using Scratch Base image provided by Docker
Deepak Patil
Deepak Patil

Posted on • Updated on

Using Scratch Base image provided by Docker

Building Base images in docker is not recommended but for those who are a bit interested in diving deeper in docker, this is a must-know thing. Even though the docker hub provides a huge collection of images for a variety of uses there are some cases where we would want to create something complex from SCRATCH !!!!.

That's where the Scratch base image from Docker comes in handy. There are several ways to generate base images, such as using an already-existing Linux machine or a running container, but for now, let's utilize a Docker scratch image.

To use scratch we just need to add FROM Scratch in our Dockerfiles. Yes, that's it! above this, you can add anything to configure your base image, Build it and it's ready to use.

For Example, let's create one. Create and Add the following to the Dockerfile.

FROM scratch
ADD hellofromdocker /
CMD ["/hellofromdocker"]
Enter fullscreen mode Exit fullscreen mode

That's it here hellofromdocker is a static executable that does not have any runtime or linked dependencies. Since the scratch image only contains the bare minimum of runtime and settings needed to create base images, using other executables or apps may not work.

Once the Dockerfile is done. build an image using docker.
docker build -t helloscratchimg .

Spin up a container from a newly created base image and get hello from Docker! 🤩


Top comments (2)

Collapse
 
dbags profile image
David Bagnara

Does the hellofromdocker executable need to be compiled for the platform that docker is running? If hellofromdocker is created from hellofromdocker.c, and I compile it on my Windows AMD64 platfrom, will the helloscratching image run on, for example, Ubuntu on ARM?

Collapse
 
deepcodr profile image
Deepak Patil • Edited

Yes, the executable needs to be compiled for the platform. You can find more information on this repository