It's ugly, but I achieved a semblance of this like so:
Dockerfile:
FROM foo
COPY ./m2/ /root/.m2
RUN stuff
imageBuild.sh:
docker build . -t barImage
container="$(docker run -d barImage)"
rm -rf ./m2
docker cp "$container:/root/.m2" ./m2
docker rm -f "$container"
I have a java build that downloads the universe into /root/.m2, and did so every single time. imageBuild.sh
copies the contents of that folder onto the host after the build, and Dockerfile
copies them back into the image for the next build.
This is something like how a volume would work (i.e. it persists between builds).