<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rajvrdhan on Medium]]></title>
        <description><![CDATA[Stories by Rajvrdhan on Medium]]></description>
        <link>https://medium.com/@rajv4rdhan?source=rss-c26c4b77541f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MMmMZaFHcCzBqbF0</url>
            <title>Stories by Rajvrdhan on Medium</title>
            <link>https://medium.com/@rajv4rdhan?source=rss-c26c4b77541f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 09:08:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajv4rdhan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building Docker Images From Scratch (No Base Image)]]></title>
            <link>https://medium.com/@rajv4rdhan/building-docker-images-from-scratch-no-base-image-5e44849ac0be?source=rss-c26c4b77541f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e44849ac0be</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[docker-image]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Rajvrdhan]]></dc:creator>
            <pubDate>Tue, 01 Sep 2026 20:08:57 GMT</pubDate>
            <atom:updated>2026-09-01T20:09:32.360Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VI7wqFSVJ6MPsNvvq4YxSQ.png" /></figure><p>Every time i write a Dockerfile, i use some base image. So one qustion always come in my head — how can i write a Dockerfile <em>without</em> any base image, from scratch.</p><p>Every dockerfile i see, it start like this:</p><pre>FROM ubuntu:24.04</pre><p>So what exactly this base image is? Let’s go slow and figure it out one by one.</p><h3>What is a base image?</h3><p>A base image is just the read-only filesystem layer that is starting point of your image. It is not a full operating system, because it dont have a kernel. Docker containers share the host machine kernel, they dont bring their own.</p><p>So really, a base image is just a collection of userland stuff — things like /bin, /etc, shell, coreutils, package manager etc. Nothing more.</p><blockquote>Most modern CPUs have a kernel or supervisor mode, and a restricted user mode. This is a hardware feature of the CPU. “Userland” is another name for code running in user mode. — <a href="https://unix.stackexchange.com/questions/137820/whats-the-difference-of-the-userland-vs-the-kernel">unix.stackexchange.com</a></blockquote><p>So when we say FROM ubuntu:24.04, we are basically just layering a filesystem on top of the host machine&#39;s kernel. Kernel is the program which runs in ring 0 in x86 architecture (or &quot;system mode&quot; in ARM) — it is the layer that actually talk to hardware.</p><h3>Why we even need a base image?</h3><p>Good qustion, and answer is little subtle. Docker uses something call Union File System.</p><blockquote>Since Docker uses a Union File System, the processes think the whole file system is mounted read-write. But all the changes go to the top-most writable layer, and underneath, the original file in the read-only image is unchanged. Since images don’t change, images do not have state. — <a href="https://stackoverflow.com/questions/20274162/why-do-you-need-a-base-image-with-docker">stackoverflow.com</a></blockquote><p>And another good point:</p><blockquote>Since all Linux distributions run the same (yup, it’s a bit simplified) Linux kernel and differ only in userland software, it’s pretty easy to simulate a different distribution environment — by just installing that userland software and pretending it’s another distribution. — <a href="https://serverfault.com/questions/755607/why-do-we-use-a-os-base-image-with-docker-if-containers-have-no-guest-os">serverfault.com</a></blockquote><p>TL;DR — what comes inside a base image is userland: shell, standard linux utilities, package manager, libc, and so on. Not a kernel.</p><h3>How docker actually works under the hood (short version)</h3><p>Docker take host kernel, and add a read-only layer of filesystem on top based on what you asked for (like ubuntu, alpine, debian). Then on top of that, it add another read-only layer for your app code and its dependency. And finally on the very top, it add one thin writable layer, where all the runtime changes happen (temp files, logs, whatever your app writes).</p><p>So in this sense, base image is not really “OS” — it is more like an anchor, or a reference point, that give your app a filesystem to live in.</p><h3>Now let’s build from Scratch</h3><pre>FROM scratch</pre><p>scratch is a special, reserved keyword in docker. It is not a real image sitting somewhere on docker hub. It is literally zero files. No shell, no package manager, no libc, nothing at all.</p><p>That mean, inside a scratch container, you can only run a binary that dont need anything else — no shared libraries, no /bin/sh, nothing. Just the raw executable and syscalls to the kernel.</p><p>And funny part — scratch is actually the ancestor of every other image that exist. Even ubuntu, debian, alpine — they all eventually built FROM scratch at the very bottom.</p><blockquote>The scratch image is mostly used for building other base images. For instance, the <a href="https://hub.docker.com/_/debian/">debian</a> image is built from scratch as such:</blockquote><pre>FROM scratch<br>ADD rootfs.tar.xz /<br>CMD [&quot;bash&quot;]</pre><p>The rootfs.tar.xz here contains all the filesystem files — basically the entire userland of debian, packed in one tarball. Debian image just extract this tarball on top of the empty scratch image, and boom, now you have a &quot;debian&quot; filesystem, even tho there is no debian kernel involved anywhere.</p><blockquote>As I understand it, scratch is essentially blank. So how the binary is executed in hello-world image then? — <a href="https://stackoverflow.com/questions/47373889/what-is-dockers-scratch-image">stackoverflow.com</a></blockquote><p>Answer is simple: the binary itself is fully self contain (statically compiled), so it dont need anything from the filesystem to run. Kernel just execute it directly.</p><h3>Example 1 — Hello World in Go, from scratch</h3><p>Let’s start with the classic. A tiny go program that just print “hello world”.</p><pre>package main</pre><pre>import &quot;fmt&quot;</pre><pre>func main() {<br>	fmt.Println(&quot;Hello, World from scratch container!&quot;)<br>}</pre><p>Now, when we compile a go binary, all the dependency the program needs get combine into single file. That is why go is so good fit for scratch images — the compiler statically link everything by default (as long as you disable cgo).</p><p>Build it like this:</p><pre>CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o hello .</pre><p>CGO_ENABLED=0 is the important part here — it make sure go dont link against system&#39;s C library (glibc), and produce a fully static binary. If you skip this, binary might still need libc.so at runtime, and since scratch dont have that file, container will just crash with something like no such file or directory (confusing error, since file is actually there, just missing libc).</p><p>Now the Dockerfile:</p><pre>FROM golang:1.22 AS builder<br>WORKDIR /app<br>COPY . .<br>RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o hello .</pre><pre>FROM scratch<br>COPY --from=builder /app/hello /hello<br>ENTRYPOINT [&quot;/hello&quot;]</pre><p>This is a multi-stage build — first stage (builder) has full golang toolchain to compile the code, second stage is just scratch with only the compiled binary copied in. Final image will be literally few MBs, sometimes even under 5MB, since it is only the binary and nothing else.</p><h3>Example 2 — A real static file server (the one i actually use)</h3><p>Ok now let’s do something bit more real. A small go http server which serve static files, and we ship it in scratch image too.</p><p>Here is the go code:</p><pre>package main</pre><pre>import (<br>	&quot;log&quot;<br>	&quot;net/http&quot;<br>)</pre><pre>func main() {<br>	fs := http.FileServer(http.Dir(&quot;./&quot;))<br>	http.Handle(&quot;/&quot;, fs)<br>	log.Println(&quot;Listening on :8080...&quot;)<br>	err := http.ListenAndServe(&quot;:8080&quot;, nil)<br>	if err != nil {<br>		log.Fatal(err)<br>	}<br>}</pre><p>Nothing fancy — just serve current directory files on port 8080.</p><p>Now, the build command. If you are on Windows / Powershell:</p><pre>$env:CGO_ENABLED=&quot;0&quot;; $env:GOOS=&quot;linux&quot;; $env:GOARCH=&quot;amd64&quot;; go build -a -installsuffix cgo -ldflags &#39;-extldflags &quot;-static&quot;&#39; -o server .</pre><p>And if you are on linux/mac (bash):</p><pre>CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags &#39;-extldflags &quot;-static&quot;&#39; -o server .</pre><p>Quick note on flags, since they look scary first time:</p><ul><li>CGO_ENABLED=0 — disable cgo, so no dynamic linking to glibc.</li><li>GOOS=linux GOARCH=amd64 — cross compile for linux amd64, even if you building on windows or mac. This is needed since container will run on linux always.</li><li>-a — force rebuild of all packages, even ones already cached. Just to be safe.</li><li>-installsuffix cgo — separate the build cache for cgo vs non-cgo builds, avoid mixing them up.</li><li>-ldflags &#39;-extldflags &quot;-static&quot;&#39; — tell external linker to also link statically, extra safety net incase something still try to dynamic link.</li></ul><p>After this, you get one single server binary that has literally zero external dependency.</p><p>Now the Dockerfile:</p><pre>FROM scratch<br>WORKDIR /app<br>COPY server .<br>COPY index.html .<br>EXPOSE 8080<br>CMD [&quot;./server&quot;]</pre><p><a href="https://github.com/rajv4rdhan/labops/tree/main/app/BunnyPage">Gitrepo</a> <a href="https://hub.docker.com/repository/docker/rajv4rdhan/labops-bunny/tags">Dockerhub</a></p><p>See how simple this is — no base OS at all. Just:</p><ol><li>WORKDIR /app — set working directory (docker will create it, even in scratch)</li><li>Copy compiled binary</li><li>Copy the static html file it need to serve</li><li>EXPOSE 8080 — just documentation, tell people this container listen on 8080</li><li>CMD [&quot;./server&quot;] — run the binary directly</li></ol><p>That’s literally the whole image. No apt-get, no apk add, nothing.</p><h3>What about Node.js or Python?</h3><p>Now, obvious qustion come next — can we do same trick with Node.js or Python apps?</p><p>Short answer: no, not directly, and here is why:</p><ul><li>Dynamic linking — most interpreter (node, python) themself are dynamically linked against libc and other system library.</li><li>Need for libc — python/node runtime literally cannot run without glibc or musl present in filesystem.</li><li>Runtime dependencies — python needs its standard library files, node needs its core modules, none of that exist in scratch.</li><li>Missing shell and utilities — a lot of npm/pip package internally shell out to sh, bash, or other tools during runtime, which scratch simply dont have.</li></ul><p>So for these language, pure scratch is basically impossible unless you statically compile the whole interpreter yourself (which is painful and rare).</p><p>The real world alternative for this is distroless images. Distroless is like a middle ground — it keep only the minimum runtime (like python interpreter + its needed shared libs), but strip away shell, package manager, and all the extra userland tools that a full ubuntu/debian image carry.</p><p>Docker has good writeup on this concept here: <a href="https://docs.docker.com/dhi/core-concepts/distroless/">docs.docker.com/dhi/core-concepts/distroless</a></p><p>Basic idea of distroless:</p><ul><li>Keep language runtime (python, node, java etc)</li><li>Remove shell (no /bin/sh)</li><li>Remove package manager (no apt, no apk)</li><li>Remove all the debugging tool</li><li>Result — much smaller and much safer image, but still runnable for interpreted language</li></ul><p>So think of it like this: scratch is for compiled static binaries (go, rust with musl, C with static linking). distroless is for interpreted or dynamically linked language, where you still need runtime but not the whole OS baggage.</p><h3>Advantages of minimal (scratch/distroless) images</h3><p>Now let’s talk why people even bother doing all this.</p><ul><li>Smaller image size — obviously the biggest win. Less MB to pull, push, store.</li><li>Better security — less software means less code that can have vulnerability in first place.</li><li>Reduced attack surface — no shell means attacker cant just docker exec in and start running arbitrary commands even if they somehow get access.</li><li>Faster deployments — smaller image pull faster across network, specially important in CI/CD pipeline or auto-scaling scenario.</li><li>Fewer vulnerabilities in scanners — tools like Trivy or Grype scan your image for CVEs. A full ubuntu base can easily have 50–100+ known CVE just from unrelated packages you never even use. Scratch image basically has zero, since there is nothing to scan except your own binary.</li></ul><p>Rough size comparison, just to give a feel:</p><pre>Ubuntu image: 80–100 MB<br>Alpine image: ~5 MB<br>Scratch image: only your binary (few MB or less)</pre><h3>Disadvantages (equally important to know)</h3><p>It’s not all sunshine tho. There is real tradeoffs here:</p><ul><li>No shell for debugging — you cant docker exec -it container sh into a scratch container, since there is no shell to exec into. If something break in production, debugging get much harder.</li><li>No package manager — cant install anything on the fly, even for quick testing.</li><li>No curl — cant even check network connectivity from inside container easily.</li><li>No bash — scripts that rely on shell just wont run.</li><li>Harder troubleshooting — you basically depend fully on your app’s own logging, since there is no way to poke around filesystem manually.</li><li>Only suitable for static binaries — this approach just dont work well for interpreted languages without extra effort (see distroless section above).</li></ul><p>So readers should keep balanced view here — scratch images are great for specific use case (compiled, static binary services), but its not a silver bullet for every project.</p><h3>Conclusion</h3><p>FROM scratch dont create a new kernel, or a complete operating system. Containers, even scratch based one, still fully rely on host Linux kernel for everything — process, memory, networking, all of it.</p><p>What docker really doing is just packaging files into immutable layers, and running your process inside isolated namespaces (with help of cgroups and namespaces from linux kernel).</p><p>A scratch image just remove everything except what your app absolutely need — pushing idea of minimalism to its most extreme point. For compiled, statically linked binaries (like go), this is fantastic and give you tiny, secure, fast images. For interpreted languages, look at distroless instead, since pure scratch just isnt practical there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e44849ac0be" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>