Docker Compose: volumes without colon (:)

百般思念 提交于 2020-01-01 04:03:18

问题


I have a docker-compose.yml file with the following:

volumes:
  - .:/usr/app/
  - /usr/app/node_modules

First option maps current host directory to /usr/app, but what does the second option do?


回答1:


The second one creates an anonymous volume. It will be listed in docker volume ls with a long unique id rather than a name. Docker-compose will be able to reuse this if you update your image, but it's easy to lose track of which volume belongs to what with those names, so I recommend always giving your volume a name.




回答2:


Just to complement the accepted answer, according to Docker's Knowledge Base there are three types of volumes: host, anonymous, and named:

  • A host volume lives on the Docker host's filesystem and can be accessed from within the container. Example volume path:

    /path/on/host:/path/in/container

  • An anonymous volume is useful for when you would rather have Docker handle where the files are stored. It can be difficult, however, to refer to the same volume over time when it is an anonymous volumes. Example volume path:

    /path/in/container

  • A named volume is similar to an anonymous volume. Docker manages where on disk the volume is created, but you give it a volume name. Example volume path:

    name:/path/in/container

The path used in your example is an anonymous volume.




回答3:


I had the same question while I was going through this tutorial, and the answer to what those lines could actually be doing is this:

Without the anonymous volume ('/usr/src/app/node_modules'), the node_modules directory would essentially disappear by the mounting of the host directory at runtime:
Build - The node_modules directory is created.
Run - The current directory is copied into the container, overwriting the node_modules that were just installed when the container was built.

The docker-compose.yml file for this:

version: '3.5'

services:

  something-clever:
    container_name: something-clever
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - '4200:4200'


来源:https://stackoverflow.com/questions/46166304/docker-compose-volumes-without-colon

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