DEV Community

Tony Metzidis
Tony Metzidis

Posted on

1 1

Build a 980-byte Docker Image -- Scratch Images Episode II

📓The gist

Why stop at 100kB? What's the smallest possible, runnable docker image we can make?

hello.asm

hello.asm

; Define variables in the data section
SECTION .DATA
    hello:     db 'Hello world!',10
    helloLen:  equ $-hello

; Code goes in the text section
SECTION .TEXT
    GLOBAL _start 

_start:
    mov eax,4            ; 'write' system call = 4
    mov ebx,1            ; file descriptor 1 = STDOUT
    mov ecx,hello        ; string to write
    mov edx,helloLen     ; length of string to write
    int 80h              ; call the kernel

    ; Terminate program
    mov eax,1            ; 'exit' system call
    mov ebx,0            ; exit with error code 0
    int 80h              ; call the kernel

Makefile


hello.o: hello.asm
    nasm -f elf64 $< -o $@

hello: hello.o
    ld $< -o $@

The Dockerfile

FROM alpine:latest as builder
WORKDIR /build
RUN apk update && \
    apk add nasm make binutils
COPY .  ./
RUN make hello

FROM scratch
WORKDIR /
COPY --from=builder /build/hello .
CMD ["/hello"]

Basically the same as before, just new apk deps for nasm.

Only 980 Bytes!

docker images |grep tiny-hello|awk '{print $8}'
952B

What's Next?

We'll build a more workable REST API that guts 99% out of a typical docker image.
This was a shorter example, since it's principally the same as the last .

➡️ Until then, can you beat 980-bytes?

Many thanks to my friend and devops expert Philippe and this hello world assembler for encouraging me to go smaller.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay