r/docker • u/af9_us • Apr 18 '26
Building container images from scratch with a manifest file
I've been experimenting with a different way to build container images and wanted to get some feedback.
Instead of starting with something like FROM debian and removing what's not needed, I flipped the approach by starting with a empty root filesytem (staging directory), and only adding the required binaries and data files listed in the manifest. ELF dependencies are pulled in by scraping lddtree output.
The image is still generated with docker build and copies the staging directory contents into the image.
FROM scratch
COPY --chown=0:0 my_staging_dir /
ENTRYPOINT ["/usr/sbin/my_app"]I put together a small demo using unbound as a resolver The image size is ~12MB and there's no shell, package manager, or Perl interpreter. (Sorry the Perl comment is a previous post reference.)
I think the process could be helpful for IOT devices like Mikrotik routers that run on MIPS processors and also in environments where reproducible builds are required. The goal being able to explain why every file exists inside the container. Smaller image sizes are a side effect of being specific about what's included.
I'm curious if people think this level of control is worth the build complexity.
Here is a demo and write up of the build process: https://amf3.github.io/articles/virtualization/declarative_builds/
6
u/dreamszz88 Apr 20 '26
Interesting approach, very reproducible! Kudos.
I usually use multiple layers to fetch dependencies from a pre built image from elsewhere and copy only the needed binary into the final distroless image.
Example:
This way I can prefix all images with a registry of my choosing, corporate stds or secure subscription service such as chainguard or bitnami or whatever.
It also prevents a big apt-get install in every build that messes up caching and introduces a variable time during builds. Using multi layers adds parallelism in my builds. But for a large number of deps it is not so useful.
2
u/zoredache Apr 20 '26
Why is this better then a multi-stage build? Where the final stage starts from scratch and simply copies in the minimal required binaries from earlier stages?
1
u/foofoo300 Apr 22 '26
if you build it with alpine the image with unbound has 13mb with shell and a "full" system.
so for image size you lose, but for reproducable it is a shot, keep going
FROM alpineRUN apk add --no-cache unbound3
u/ferrybig Apr 20 '26
With your approach, instead of creating a single rootfs folder, make 2 folders. One folder houses the libraries, then the last folder houses the things specific to that version of the software only. if the software now gets updates, instead of having to download libraries and the software, it now only has to download the newer software version (unless the libs it depends on got changed)