How to build Docker from source without Docker ? How to build Debian packages?

Don’t want to pollute Github, so asking here.

I am aware that building Docker without Docker discussed before many years ago (found and issue on Github from 2015), but the only result of that discussion was a Makefile which obviously won’t work with any modern version of Docker.

In my case, I have a Debian arm64 distribution running inside an LXC (Incus) container on an Android host (Exynos Galaxy S20+ with minor screen damage and a bad battery) on which I want to run Docker.
The host system has “cpuset” cgroup mounted with “noprefix” option which is incompatible with official Docker packages. I cannot change this without breaking the Android host, the only way is to modify the code and rebuild Docker from source. Which would not be a big problem, I have already patched LXC and Incus to work with cpuset noprefix and they both work fine (directly on Android using Debian binaries with modified rpath).

The problem is that the only way provided to rebuild Docker is… to use Docker. That is, Docker depends on itself for builds.

I do fully understand the need for being able to do reproducible builds and having a fixed build environment but it would be good to have some way to build Docker directly on the host system.

Alternatively, a way to build a Debian package using another arm64 machine with working Docker would suffice as well. But again, the build documentation only has instructions on how to build and install Docker inside of a Docker container. Tried googling but didn’t find any documentation about packaging Docker, even though Docker themselves provide Debian packages.

Never mind, rebuilt docker and docker-cli on a different arm64 machine (a Pi 5) with working Docker and copied over the binaries from /usr/local/bin to my LXC container. Not the most convenient way but did get the job done. Fortunately, my LXC container, build host and Docker’s own build container all run Debian Bookworm, so all external library versions were the same.
Created systemd services for containerd and dockerd manually. Everything appears to work fine as if Docker was installed from official packages. hello-world does run without errors now.