RKe*_*nel 5 docker docker-compose
我想在 docker 容器之间共享单个文件
ContainerA:
- file-1
- file-2
- file-3
- shared-file
ContainerB
- file-a
- file-b
- file-c
- shared-file
Run Code Online (Sandbox Code Playgroud)
不可能为我分享完整的卷。容器中的路径不一样。
由于docker默认将目录作为卷,并且仅当源是现有文件时才绑定安装文件,因此使用起来有点困难。
对于file要在当前目录中共享的文件,您可以使用bind-mountsdocker-compose.yml
services:
app1:
# ...
volumes:
- ./file:/path/to/file1:rw
app2:
# ...
volumes:
- ./file:/path/to/file2:rw
Run Code Online (Sandbox Code Playgroud)
这要求文件已经存在于容器之外。另请参阅jubnzv 的回答中有关绑定安装文件的警告。
根据您的用例,更好的解决方法可能是使用符号链接并将共享文件安装在目录卷中:
# tree layout in containers:
ContainerA:
|- shared/
| ` shared-file
`- app_folder_a/
|- file-1
|- file-2
|- file-3
`- shared-file-a -> /shared/shared-file
ContainerB:
|- shared/
| ` shared-file
`- app_folder_b/
|- file-a
|- file-b
|- file-c
`- shared-file-b -> /shared/shared-file
Run Code Online (Sandbox Code Playgroud)
# tree layout in containers:
ContainerA:
|- shared/
| ` shared-file
`- app_folder_a/
|- file-1
|- file-2
|- file-3
`- shared-file-a -> /shared/shared-file
ContainerB:
|- shared/
| ` shared-file
`- app_folder_b/
|- file-a
|- file-b
|- file-c
`- shared-file-b -> /shared/shared-file
Run Code Online (Sandbox Code Playgroud)
或者,如果您不想/不能在主机上拥有该文件或共享目录,您可以使用带有符号链接的命名卷:
# docker-compose.yml
services:
app1:
# ...
volumes:
- ./shared/:/shared/:rw
app2:
# ...
volumes:
- ./shared/:/shared/:rw
Run Code Online (Sandbox Code Playgroud)
以及容器B中符号链接的共享文件:
ContainerB:
|- shared/
| ` ...
`- app_folder_b/
|- file-a
|- file-b
|- file-c
`- shared-file-b -> /shared/shared-file-a
Run Code Online (Sandbox Code Playgroud)
这里重要的部分是,在创建新卷时,shared第一次docker将使用容器中目标目录的内容预填充它。因此,为了正确工作,app1需要首先启动,以便shared卷中填充内容app_folder_a- 因此depends_on.
有了这个,您就拥有了shared卷,其中填充了容器A的内容/app_folder_a/,并安装到相同的容器以及容器B上/shared/,同时/app_folder_b/shared-file-b是 的符号链接/shared/shared-file-a。
| 归档时间: |
|
| 查看次数: |
1415 次 |
| 最近记录: |