How to mount docker volume into my docker project

2019-05-07 12:38发布

问题:

I have a Maven project. I'm running my Maven builds inside Docker. But the problem with that is it downloads all of the Maven dependencies every time I run it and it does not cache any of those Maven downloads.

I found some work arounds for that, where you mount your local .m2 folder into Docker container. But this will make the builds depend on local setup. What I would like to do is to create a volume (long live) and link/mount that volume to .m2 folder inside Docker. That way when I run the Docker build for the 2nd time, it will not download everything. And it will not be dependent on environment.

How can I do this with docker-compose?

回答1:

Without knowing your exact configuration, I would use something like this...

version: "2"

services:
  maven:
    image: whatever
    volumes:
      - m2-repo:/home/foo/.m2/repository

volumes:
  m2-repo:

This will create a data volume called m2-repo that is mapped to the /home/foo/.m2/repository (adjust path as necessary). The data volume will survive up/down/start/stop of the Docker Compose project.

You can delete the volume by running something like docker-compose down -v, which will destroy containers and volumes.