compose icon indicating copy to clipboard operation
compose copied to clipboard

Named Volumes & Persistance in version 3

Open r0ssIV opened this issue 9 years ago • 41 comments

This is basically a question about named volumes. Starting from version 3 file format docker-compose does not allow volumes-from which was deprecated in favor of named volumes.

But there is a problem - with named volumes you cannot bind to local host folder & make it persistent. So, for example, if I want to share my project folder among nginx, fpm and mysql docker containers, I need to specify a volume mapping setting for each of them.

It's not possible to just create one named volume mapped to project folder & then simply reuse it in all containers.

Why this is not possible? I am aware of the third-party docker plugin like https://github.com/CWSpear/local-persist but as all third-party things, this does not make me feel I should use it (is it stable or maybe it is slow). Are there any particular reasons (drawbacks) why it was not implemented in the core?

r0ssIV avatar Mar 28 '17 17:03 r0ssIV

One solution would be to declare each bind individually, like:

version: '3'
services:
  mysql:
    volumes:
      - ./project:/opt/project
    ...
  nginx:
    volumes:
      - ./project:/opt/project
    ...
  fpm:
    volumes:
      - ./project:/opt/project
    ...

This is more explicit than using volumes_from, which might actually be a good thing.

On the other hand, why do your nginx and mysql services need access to your code?

shin- avatar Mar 28 '17 18:03 shin-

@shin- the solution you describe is what I use now (and it seems not good for me, because of duplication & increased typoo possibility), but since V3 deprecated volumes-from, it means that docker team decided it's wrong approach.

There are scenarios where it is desirable to share one volume among multiple containers. For example nginx - to serve static content from project folder, fpm - to access both dynamic and static

Docker now says "use named volumes", but omits "persistence". Why is it?

If we don't use one volume per multiple containers we also have another issue - increased number of orphaned volumes after several successful/unsuccessful builds.

p.s.: also aware about docker system prune but that's not what I'd like to spend time every day

r0ssIV avatar Mar 28 '17 19:03 r0ssIV

@rivaros I have the same problem. I also want to use named volume, but there seems to be no way to tell docker where to mount the volume from the host?

markvillar avatar Apr 15 '17 21:04 markvillar

I'm having difficulty with this as well. With the updates to docker-compose in v3.2 and long-syntax there doesn't appear to be a way to have names/aliases for bind mounted volumes. It appears that the source is used for the name for volume mounted volumes, but the local path for bind mounted volumes...

With the removal of volumes_from support, I was wondering if it might be possible to define named volume support for bind mounted volumes, which would allow for single local path definition that can be shared between several services...

version: '3'
services:
  web:
    ...
    volumes:
      - common_code
  worker:
    ...
    volumes:
      - common_code
volumes:
  - type: bind
    name: common_code
    source: /local/path
    target: /application/path

It was mentioned that local-persist could be used as an approach to named local volumes, but I echo the question of why this isn't present in core. However, an approach like that would allow for even more nuanced behaviour like:

version: '3'
services:
  worker:
    ...
    volumes:
      - common_code:/application/path
  different-worker:
    ...
    volumes:
      - common_code:/different/application/path
volumes:
  - type: bind
    name: common_code
    source: /local/path

Is there a reason we couldn't have named/aliased bind-type volume definitions inside the docker compose file?

masterful avatar Apr 19 '17 17:04 masterful

Upvoting this again. Any comments from off team why it's not implemented?

r0ssIV avatar May 04 '17 20:05 r0ssIV

Like @rivaros said in comment 299299853. Any comments?

We use docker configuration inside a sub folder of a git repository. This means we have nginx/php machines pointing to ../ and ../public/ We should be able to set that source folder in named volumes.

Then again, I'm not even seeing volumes mounted on the way @shin- described in comment 289867611

hmhofman avatar May 30 '17 14:05 hmhofman

Months ago and still no answer from docker team? 😞

GodLesZ avatar Jul 04 '17 06:07 GodLesZ

Hello,

Thanks for docker, but i agree with others, the volume from has no replacement now.

Applications which i use are in the same states that persons spoken before, we have :

  • a app server container
  • a command container
  • a www server container

each of there works on the same data (assets, appfile, etc...). Traditionnal way to deploy, capistrano and same tools, don't works as they use many symbolic links not mounted by docker. The only way to deploy cleanly is consequently to use an image with data only, purge the volume on host and up the containers.

Another solution could be "use one container with all inside" : web server, app server, all cli command, but in this case why use docker ?

I think, the volume from or shared app data (code, assets aren't data, just the result of the build) should be reevaluated, maybe with just an option to volume, by example :

#...
  volumes:
  - type: volume
    source: mydata
    target: /data
    volume:
         copy_on: start # or up or empty or false
#...

Thanks for docker and docker-compose.

nicodmf avatar Aug 09 '17 21:08 nicodmf

👍

aaronlifton avatar Aug 17 '17 19:08 aaronlifton

Wowwwww!!! is almost 2018 and none has been assigned to this? This is a "must" since I am running into the same issue and there is a few workaround in this topic and out there but why haven't as part of the core? Upvoting this

reypm avatar Dec 18 '17 15:12 reypm

How is this still not being implemented? At least I'd like to know an official workaround for this problem!

SimonLammer avatar Dec 21 '17 06:12 SimonLammer

+100

luislobo avatar Jan 17 '18 23:01 luislobo

+1

robertcsakany avatar Jan 20 '18 11:01 robertcsakany

+1

hosamshahin avatar Jan 20 '18 23:01 hosamshahin

Damn frustrating when using it for local development in complex orchestrations.

rijnhard avatar Jan 25 '18 15:01 rijnhard

+1

mohanzeal avatar Jan 26 '18 17:01 mohanzeal

+1

tatyanamincheva avatar Feb 02 '18 07:02 tatyanamincheva

up vote!

jangolle avatar Feb 09 '18 12:02 jangolle

+1

bvisonl avatar Feb 10 '18 17:02 bvisonl

+1

xico42 avatar Feb 10 '18 20:02 xico42