简体   繁体   English

在 docker-compose 中设置音量

[英]Setting volumes in docker-compose

I need a way to configure docker-compose to create a volume if it's missing, or in case it exists, use it.我需要一种方法来配置 docker-compose 以创建volume ,如果它丢失,或者如果它存在,请使用它。 I need it to be persistent between versions, but I cannot assure it'll be configured upon initial configuration.我需要它在版本之间保持持久性,但我不能保证它会在初始配置时进行配置。

volumes:
  my_volume:
    external: true

I need to mount docker volume and not host directory.我需要挂载 docker 卷而不是主机目录。 something like:就像是:

-v my_volume:/my_files

what's the best solution for such use-case?这种用例的最佳解决方案是什么?

You can use volume for each application or services you set in docker-compose file.您可以为您在 docker-compose 文件中设置的每个应用程序或服务使用音量。 For instance, I set a volume for my nginx server as like.例如,我为我的 nginx 服务器设置了一个音量。

 volumes:
  - ./web/public:/srv/www/static
  - ./default.conf:/etc/nginx/conf.d/default.conf

The left side before colon are the path of the files or folder I want to store inside my docker image as volume whereas on right side I wrote the path where the files will be stored.冒号之前的左侧是我想要存储在 docker 图像中作为卷的文件或文件夹的路径,而在右侧我写了将存储文件的路径。 When you build the file for first time it will create volume if in case if doesn't exist or use existing volume if it exist hope this helps.当您第一次构建文件时,如果不存在,它将创建卷,或者如果存在则使用现有卷,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM