OP could have skipped all this by doing the compilation with cache on the host system and copying the compiled statically linked binary back to the docker image build.