Docker Compose-在多个容器之间共享命名卷
我正在使用docker-compose和v3。我正在尝试在Docker中装载卷:
I'm using docker-compose and v3. I'm trying to mount a volume in docker:
./ appdata:/ appdata
我想将此作为体积,然后从多个容器中引用该体积。 卷配置参考仅显示 data-volume:
作为命名卷,没有任何值,因此看起来不像上面。
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
这给了我:
错误:文件'./docker-compose.yml'中,卷 app-volume必须是映射而不是字符串。
ERROR: In file './docker-compose.yml', volume 'app-volume' must be a mapping not a string.
很明显,我知道我需要更改 volumes
键/值对,但是我不确定如何进行更改,以便我可以在服务之间共享卷。
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.
我还检查了 volumes_from
,但这实际上只允许从其他容器继承。我见过有人在另一个包含他们想要的映射的容器上使用 volumes_from
,但是使用了 command:true
来设置容器从未真正运行过,在我看来就像是黑客。
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.
我该怎么做?
注意,我做具有以下工作:
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 :-)
可以通过以下方式在容器之间共享命名卷:
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:
这是我用来更好理解的示例配置。我将从 web
容器生成的静态文件暴露给名为 static-content
的命名卷,然后读取该卷并由 nginx
容器提供服务:
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: