Managing Dockerfile that compile source

I have an Dockerfile that does quite some compilation. I want to slim down the resulting image with an eye towards improving load times. This is in particular an R based project so it has things like RTools that runs Gcc and everything. It seems like all the gcc build libraries and likely intermediate files that were used are just left in the container.

Ideally, I would build these externally, then just add them to the Dockerfile. Is there any mechanism in Docker for managing these sort of container builds? It would be cool if you could build the binaries in one image then install them to another.

Seems like you are looking for this: Multi-stage builds | Docker Documentation