Build a Docker image using docker build and a tmpfs?

Question: How do you specify in a Dockerfile, or on the docker build command line, that you would like to have a tmpfs mounted in the build container? This is in the context of a split build – the first container, which would use the RAM disk, builds the application from source, and the second stage copies the result out to a new container.

This question appears to be similar, but my motivations differ. I’m not so much concerned with stale image layers persisting, but I’m concerned with performance of the build. When experimenting outside of Docker, building the particular application I’m working with was over 4x faster when the entire source tree was in RAM rather than on disk. (The project has many intermediate builds and parallelism, so even an SSD tends to thrash around a bit)

Since Docker does support mounting a tmpfs during a normal docker run command, it would seem there should be a way to include this in a Dockerfile? However, I can’t seem to find this information anywhere – nearly every search for “tmpfs” and “dockerfile” or “build” or “ramdisk” and so on just points to either the above linked post or Docker’s docs on using tmpfs in containers started with docker run.

It would be acceptable if the tmpfs would not persist even through to the second container’s assembly. That could be remedied simply by copying the built application out of the tmpfs within the build container before that container exits, and then using that new location when COPYing.

Source: StackOverflow