182

I'm using docker-compose and v3. I'm trying to mount a volume in docker:

./appdata:/appdata

I'd like to have this as a volume and then reference that volume from multiple containers. The volume configuration reference only shows data-volume: as a named volume, with no value, so it doesn't look like the above.

services:

    nginx:
        build: ./nginx/
        ports:
            - 80:80
        links:
            - php
        volumes:
            - app-volume

    php:
        build: ./php/
        expose:
            - 9000
        volumes:
            - app-volume

volumes:
     app-volume: ./appdata:/appdata

This gives me:

ERROR: In file './docker-compose.yml', volume 'app-volume' must be a mapping not a string.

Obviously I know I need to change the volumes key/value pair, but I'm not sure how to change this so I can share a volume between services.

I've also checked out volumes_from but this effectively just allows inheritance from other containers. I've seen someone use volumes_from on another container that contains the mapping they want, but with command: true set so that the container is never actually run, which to me just seems like a hack.

How can I do this?


Note, I do have the following working:

nginx:
    volumes:
        - ./appdata:/appdata
php:
    volumes:
        - ./appdata:/appdata

But that's just duplication and is something I'm hoping a named volume could help me avoid :-)

0

4 Answers 4

217

The named volumes can be shared across containers in the following way:

services:
    nginx:
        build: ./nginx/
        ports:
            - 80:80
        links:
            - php
        volumes:
            - app-volume:location_in_the_container

    php:
        build: ./php/
        expose:
            - 9000
        volumes:
            - app-volume:location_in_the_container

volumes:
     app-volume: 

Here's an example config that I use for better understanding. I'm exposing the static files generated from my web container to a named volume called static-content which is then read and served by the nginx container:

services:
  nginx:
    container_name: nginx
    build: ./nginx/

    volumes:
      - static-content:/usr/src/app

  web:
    container_name: web
    env_file: .env
    volumes:
      - static-content:/usr/src/app/public
    environment:
      - NODE_ENV=production

    command: npm run package

volumes:
  static-content:
13
  • 139
    Where do you set the location of static_content on the host filesystem? Commented Mar 13, 2018 at 22:37
  • 11
    Whitespace in app-volume: location_in_the_container is wrong.
    – hasufell
    Commented Oct 22, 2018 at 3:36
  • 9
    What if /usr/src/app in the nginx container and /usr/src/app/public in the web container both got original content, which one will be used and why?
    – deko
    Commented Jan 3, 2019 at 17:09
  • 3
    @TravisBear for this use case (sharing data between containers) there is no really need to have it on the host. Example with static data is great - you execute collectstatic in one container and want results to be available in another one, but you don't care about host folder Commented Mar 20, 2019 at 9:45
  • 18
    @Kannaj TravisBear's question is the one that correctly identifies the issue I find the most confusing. How in the compose file can you specify where the named volume is sourced? I don't want to leave it up to the docker engine to determine where to store the named volume on the host, I want to specify a path. Commented Apr 3, 2019 at 20:15
47

This solves it without using named volumes:

      volumes:
          - ./appdata:/appdata

So, it looks like:

services:

  nginx:
      build: ./nginx/
      ports:
          - 80:80
      links:
          - php
      volumes:
          - ./appdata:/appdata

  php:
      build: ./php/
      expose:
          - 9000
      volumes:
          - ./appdata:/appdata
4
  • 6
    Ah, nice timing! I did this above (see my change). However, it seems we're still duplicating the mapping. If I use this over 3 containers, it gets big. Can we use named containers to avoid this duplication?
    – Jimbo
    Commented May 31, 2017 at 12:31
  • The thing is that named volumes is not something only about syntax and clear code. It will create a volume inside docker data installation directory and you will not have your local files there (the ./appdata). Is it useful for you anyway?
    – Robert
    Commented May 31, 2017 at 12:35
  • 3
    I definitely need the ./appdata, that is what I'm trying to do. Leave this answer here though :) +1
    – Jimbo
    Commented May 31, 2017 at 12:53
  • 5
    What happens if I have two containers of the same image, uploading file (via upload file service) in one container, it's gonna be available in the other one? if not, how can I do that?
    – magnoz
    Commented Nov 14, 2018 at 12:24
40

You can use one of the two options:

  1. Named volumes: https://docs.docker.com/compose/compose-file/07-volumes/

  2. extension-fields to avoid duplicating volumes source and prevent yourself from future typos:

version: '3.5'

x-services-volume:
  &services-volume
  type: bind
  source: ./appdata
  target: /appdata

services:

    nginx:
        build: ./nginx/
        ports:
            - 80:80
        links:
            - php
        volumes: *services-volume

    php:
        build: ./php/
        expose:
            - 9000
        # Use same way as for nginx if target override not needed.
        volumes:
            - <<: *services-volume
            target: /opt/target-override

NOTE: This feature is available starting from version 3.4 file format.

10
  • If the *services-volume is just a pointer to the value set above, this looks awesome... I'll have to try it.
    – Jimbo
    Commented Jul 13, 2020 at 14:51
  • @Jimbo yes, it is, also note that docker-compose file version shall be 3.4+ Commented Jul 13, 2020 at 14:54
  • 6
    Named volumes, aka the top-level volumes field, seem to still be a thing in v3 of docker-compose.
    – Alex Povel
    Commented Jul 21, 2020 at 16:48
  • 1
    @and0r the folder is shared, imagine google drive and real users + shared folder, if someone delete file on that folder than it's gone for the rest of users Commented Oct 22, 2020 at 18:12
  • 5
    @AndriyIvaneyko Are named volumes actually removed? Couldn't find any references on that. Commented Mar 22, 2021 at 14:32
14

Previous answers helped me a lot (thanks!) but it took some more time to figure out how to set options to create a shared tmpfs volume (memory file system). I'm sharing it in the hope to make life easier for developers in the same situation.

An example showing only the relevant parts of docker-compose.yml:

version: '3'

volumes:
  shared-tmpfs:
    driver: local
    driver_opts:
      type: "tmpfs"
      device: "tmpfs"
      o: "size=256m,uid=1000"

services:
  nginx:
    volumes:
      - shared-tmpfs:/tmp/mytmpfs

  php-fpm:
    volumes:
      - shared-tmpfs:/tmp/mytmpfs

I use it to spare my SSD from heavy writes (building lot's of static html files) in development/watch mode.

You can read more about driver_opts in the official Docker docs here.

2
  • What about the accepted answer didn't work for you? Here you have to define it twice - with the accepted answer's & and * notation so you only have to define it once?
    – Jimbo
    Commented Mar 23, 2021 at 9:32
  • 1
    @Jimbo I do like the accepted answer but I wasn't able to achieve the same with that approach. That solution binds an existing directory, my solution creates a new tmpfs volume on-the-fly with the given options. It may be my fault, but I wasn't able to convert it to the form of the accepted answer (extension fields).
    – szegheo
    Commented Mar 23, 2021 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.