Docker撰写 – 在多个容器之间共享指定的卷

我正在使用docker-compose和v3。 我正在试图在docker中安装一个卷:

./appdata:/appdata

我希望将其作为一个卷,然后从多个容器中引用该卷。 卷configuration参考只显示data-volume:作为一个命名卷,没有值,所以它看起来不像上面那样。

 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 

这给了我:

错误:在文件“./docker-compose.yml”中,卷“app-volume”必须是映射而不是string。

很明显,我知道我需要改变volumes键/值对,但我不知道如何改变这个,所以我可以共享服务之间的卷。

我也检查了volumes_from但是这只是允许inheritance其他容器。 我看到有人在另一个包含他们想要的映射的容器上使用volumes_from ,但是使用command: true设置,这样容器从来没有真正运行过,对我来说这只是一个黑客。

我怎样才能做到这一点?


请注意,我有以下工作:

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

但是这只是重复,是我希望命名卷可以帮助我避免:-)

命名卷可以通过以下方式在容器之间共享

 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: 

inheritance人一个示例configuration,我使用更好的理解。 我将从我的web容器生成的静态文件暴露给名为static-content的命名卷,然后由nginx容器读取并提供

 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: 

这解决了它不使用命名卷:

  volumes: - ./appdata:/appdata 

所以,它看起来像:

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