how to define a general mount point in docker comp

2019-08-20 07:44发布

问题:

I would like to define a general mount volume - along with all the options I would like to have it associated - that can be reused across multiple services. In fact, I'm developing a project which uses the same source for several microservices. That way, the volume will be simpler to manage and modify.

To start off, I used the old way which took advantage of volumes_from:

shared:
  image: phusion/baseimage 
  volumes:
    - ./code:/var/www/html

nginx:
  build: docker/nginx
  ports:
    - "8080:80"
  links:
    - php
  volumes_from:
    - shared

This works, but I had to define a shared service to make it work. As of the 3.0 version, volumes can be used, so I would like to define a general volume and use it into my nginx service, but I'm not finding the right syntax:

version: '3.3'

volumes:
  vol_test:
    type: bind
    source: ./code
    target: /var/www/html
    volume:
      nocopy: true

services:
  nginx:
    build: docker/nginx
    ports:
      - "8080:80"
    volumes:
      - vol_test

Update

I've found that defining a volume the way I want could not be possible, since the following definition:

volumes:
  data-volume:
    type: bind
    source: ./code
    target: /var/www/html
    volume:
      nocopy: true

will produce this output when calling docker-compose up:

ERROR: The Compose file './docker-compose.yml' is invalid because:
volumes.data-volume value Additional properties are not allowed ('volume', 'source', 'type', 'target' were unexpected)

I guess I still have to use the volumes_from way then. Can anybody confirm that?

回答1:

You do not have to use volume_from anymore. Form https://docs.docker.com/compose/compose-file/#volumes:

But, if you want to reuse a volume across multiple services, then define a named volume in the top-level volumes key. Use named volumes with services, swarms, and stack files.

They even specifically address your issue:

Note: The top-level volumes key defines a named volume and references it from each service’s volumes list. This replaces volumes_from in earlier versions of the Compose file format. See Use volumes and Volume Plugins for general information on volumes.

So, for a MWE I defined an empty volume and referenced it in two services. This is the compose file:

version: '3.3'

volumes:
  vol_test:

services:
  reader:
    image: ubuntu
    tty: true
    command: bash -c "sleep 1; cat /volume/file;exit 0"
    volumes:
      - vol_test:/volume

  writer:
    image: ubuntu
    tty: true
    command: bash -c "date | tee /volume/file; exit 0"
    volumes:
      - vol_test:/volume

This gives us the following behavior:

$ date; docker-compose up
So 27. Aug 11:54:13 CEST 2017
Creating network "dockercomposetest_default" with the default driver
Creating dockercomposetest_writer_1 ... 
Creating dockercomposetest_reader_1 ... 
Creating dockercomposetest_writer_1
Creating dockercomposetest_reader_1 ... done
Attaching to dockercomposetest_writer_1, dockercomposetest_reader_1
writer_1  | 27 09:54:15 UTC 2017
reader_1  | 27 09:54:15 UTC 2017
Gracefully stopping... (press Ctrl+C again to force)


回答2:

I can confirm your observation: If you want to mount a host directory, you'll have to use the bind mount syntax.