How to mount docker volume into my docker project using compose?

泄露秘密 提交于 2019-12-10 09:11:08

问题


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.



来源:https://stackoverflow.com/questions/39977955/how-to-mount-docker-volume-into-my-docker-project-using-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!