{"id":23587,"date":"2022-10-03T14:10:14","date_gmt":"2022-10-03T14:10:14","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=23587"},"modified":"2025-07-12T05:40:18","modified_gmt":"2025-07-12T05:40:18","slug":"creating-a-simple-parent-base-docker-image-using-scratch","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/creating-a-simple-parent-base-docker-image-using-scratch\/","title":{"rendered":"Docker Tutorials: Image &#8211; Creating a docker image using scratch"},"content":{"rendered":"\n<p>You can use Docker\u2019s reserved, minimal image,&nbsp;scratch, as a starting point for building containers. Using the&nbsp;scratch&nbsp;\u201cimage\u201d signals to the build process that you want the next command in the&nbsp;Dockerfile&nbsp;to be the first filesystem layer in your image.<\/p>\n\n\n\n<p>There is a scratch image on docker hub, but it&#8217;s essentially just the canvas for creating base images, it&#8217;s essentially useless until you setup an OS and create a new image from it. you&#8217;ll likely never need to use it unless you&#8217;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.<\/p>\n\n\n\n<p><strong>The&nbsp;scratch&nbsp;image is the most minimal image in Docker<\/strong>. This is the base ancestor for all other images. The&nbsp;scratch&nbsp;image is actually empty. It doesn&#8217;t contain any folders\/files &#8230;<\/p>\n\n\n\n<p>The&nbsp;scratch&nbsp;image is mostly used for building other base images. For instance, the&nbsp;<a href=\"https:\/\/hub.docker.com\/_\/debian\/\" target=\"_blank\" rel=\"noopener\">debian<\/a>&nbsp;image is built from scratch as such:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">FROM scratch  \nADD rootfs.tar.xz \/  \nCMD [\"bash\"]<\/pre>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/github.com\/debuerreotype\/docker-debian-artifacts\/blob\/de09dd55b6328b37b89a33e76b698f9dbe611fab\/buster\/slim\/rootfs.tar.xz\" target=\"_blank\" rel=\"noopener\">rootfs.tar.xz<\/a>&nbsp;contains all the files system files. The Debian image adds the filesystem folders to the scratch image, which is empty.<\/p>\n\n\n\n<p>While&nbsp;scratch&nbsp;appears in Docker\u2019s repository on the hub, you can\u2019t pull it, run it, or tag any image with the name&nbsp;scratch. Instead, you can refer to it in your&nbsp;Dockerfile. For example, to create a minimal container using&nbsp;scratch:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">FROM scratch<br>ADD hello \/<br>CMD [\"\/hello\"]<\/pre>\n\n\n\n<p>Assuming you built the \u201chello\u201d executable example&nbsp;<a href=\"https:\/\/github.com\/docker-library\/hello-world\/blob\/master\/hello.c\" target=\"_blank\" rel=\"noopener\">from the Docker GitHub example C-source code<\/a>, and you compiled it with the&nbsp;-static&nbsp;flag, you can then build this Docker image using:&nbsp;docker build &#8211;tag hello .<\/p>\n\n\n\n<p><strong>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&#8217;t need any additional libraries to execute.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker run --rm -it -v $PWD:\/build ubuntu:16.04\ncontainer# apt-get update &amp;&amp; apt-get install build-essential\ncontainer# cd \/build\ncontainer# gcc -o hello -static -nostartfiles hello.c<\/pre>\n\n\n\n<p>Then you can run it (on Linux, Mac, or Windows) using:&nbsp;docker run &#8211;rm hello<\/p>\n\n\n\n<p>There are more example scripts for creating parent images in the Docker GitHub Repo:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/moby\/moby\/blob\/master\/contrib\/mkimage\/busybox-static\" target=\"_blank\" rel=\"noopener\">BusyBox<\/a><\/li><li>CentOS \/ Scientific Linux CERN (SLC)&nbsp;<a href=\"https:\/\/github.com\/moby\/moby\/blob\/master\/contrib\/mkimage\/rinse\" target=\"_blank\" rel=\"noopener\">on Debian\/Ubuntu<\/a>or&nbsp;<a href=\"https:\/\/github.com\/moby\/moby\/blob\/master\/contrib\/mkimage-yum.sh\" target=\"_blank\" rel=\"noopener\">on CentOS\/RHEL\/SLC\/etc.<\/a><\/li><li><a href=\"https:\/\/github.com\/moby\/moby\/blob\/master\/contrib\/mkimage\/debootstrap\" target=\"_blank\" rel=\"noopener\">Debian \/ Ubuntu<\/a><\/li><\/ul>\n\n\n\n<p><strong>There are one very good reference of understanding and creating the docker images from scratch are<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/ericchiang.github.io\/post\/containers-from-scratch\/\" target=\"_blank\" rel=\"noreferrer noopener\">Containers from Scratch<\/a><\/li><li><a href=\"https:\/\/docs.docker.com\/develop\/develop-images\/baseimages\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create a base docker image<\/a><\/li><\/ul>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_39042\"><iframe loading=\"lazy\"  id=\"_ytid_31524\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_39042\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>You can use Docker\u2019s reserved, minimal image,&nbsp;scratch, as a starting point for building containers. Using the&nbsp;scratch&nbsp;\u201cimage\u201d signals to the build process that you want the next command in the&nbsp;Dockerfile&nbsp;to be&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862],"tags":[],"class_list":["post-23587","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=23587"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23587\/revisions"}],"predecessor-version":[{"id":31424,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/23587\/revisions\/31424"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=23587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=23587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=23587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}