DEV Community

Tony Metzidis
Tony Metzidis

Posted on

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.

Top comments (0)