[英]Docker compose how to mount path from one to another container?
I've nignx container
and one asset container
which have all my assets build from grunt or some other tools. 我有
nignx container
和一个asset container
,我的所有资产都是从grunt或其他工具构建的。
Now in docker compose file, i want to mount asset container's
's folder path into nginx container
so nginx can serve that files. 现在在docker compose文件中,我想将
asset container's
文件夹路径挂载到nginx container
以便nginx可以提供该文件。
How can we do that? 我们怎么做? i don't remember but i think there is a option where we can share path of one container with another.
我不记得,但我认为有一个选项,我们可以分享一个容器与另一个容器的路径。
Suppose if i scale up nginx to 2 container then will that mount works for all instance of nginx? 假设如果我将nginx扩展到2个容器,那么该挂载是否适用于所有nginx实例?
if i scale up asset container
then what will happen? 如果我扩大
asset container
那会发生什么?
i also want to mount that with my host so development can be done be easily. 我也想和我的主人一起安装它,这样可以很容易地完成开发。
What you want to do is use a volume, and then mount that volume into whatever containers you want it to appear in. 您要做的是使用卷,然后将该卷装入您希望它出现的任何容器中。
You can do this completely inside of Docker. 你可以在Docker中完全做到这一点。
Here is an example (stripped-down - your real file would have much more than this in it, of course). 这是一个例子(精简版 - 当然,你的真实文件会比这更多)。
version: '3'
services:
nginx:
volumes:
- asset-volume:/var/lib/assets
asset:
volumes:
- asset-volume:/var/lib/assets
volumes:
asset-volume:
At the bottom is a single volume defined, named "asset-volume". 底部是一个定义的单个卷,名为“asset-volume”。
Then in each of your services, you tell Docker to mount that volume at a certain path. 然后在每个服务中,告诉Docker将该卷安装在某个路径上。 I show example paths inside the container, just adjust these to be whatever path you wish them to be in the container.
我在容器内部显示了示例路径,只需将它们调整为您希望它们在容器中的任何路径。
The volume is an independent entity not owned by any particular container. 该卷是不属于任何特定容器的独立实体。 It is just mounted into each of them, and is shared.
它只是安装在每个中,并且是共享的。 If one container modifies the contents, then they all see the changes.
如果一个容器修改了内容,那么它们都会看到更改。
Note that if you prefer only one can make changes, you can always mount the volume as read-only in some services, by adding :ro
to the end of the volume string. 请注意,如果您只希望有一个可以进行更改,则可以通过在卷字符串的末尾添加
:ro
来始终将卷安装为某些服务中的只读卷。
services:
servicename:
volumes:
- asset-volume:/var/lib/assets:ro
Alternately you can use a directory on the host and mount that into the containers. 或者,您可以使用主机上的目录并将其挂载到容器中。 This has the advantage of you being able to work directly on the files using your tools outside of Docker (such as your GUI text editor and other tools).
这样做的好处是,您可以使用Docker之外的工具直接处理文件(例如GUI文本编辑器和其他工具)。
It's the same, except you don't define a volume in Docker, instead mounting the external directory. 它是相同的,除了你没有在Docker中定义一个卷,而是安装外部目录。
version: '3'
services:
nginx:
volumes:
- ./assets:/var/lib/assets
asset:
volumes:
- ./assets:/var/lib/assets
In this example, the local directory "assets" is mounted into both containers using the relative path ./assets
. 在此示例中,使用相对路径
./assets
将本地目录“assets”装入两个容器中。
You can also set it up for a different dev and production environment. 您还可以为不同的开发和生产环境进行设置。 Put everything in docker-compose.yml except the volume mounts.
将所有内容放在docker-compose.yml中,卷装入除外 。 Then make two more files.
然后再制作两个文件。
In these files put only the minimum config to define the volume mount. 在这些文件中只放置最小配置来定义卷装入。 We'll mix this with the docker-compose.yml to get a final config.
我们将它与docker-compose.yml混合以获得最终配置。
Then use this. 然后使用它。 It will use the config from docker-compose.yml, and use anything in the second file as an override or supplemental config.
它将使用docker-compose.yml中的配置,并使用第二个文件中的任何内容作为覆盖或补充配置。
docker-compose -f docker-compose.yml \
-f docker-compose.dev.yml \
up -d
And for production, just use the prod file instead of the dev file. 而对于生产,只需使用prod文件而不是dev文件。
The idea here is to keep most of the config in docker-compose.yml, and only the minimum set of differences in the alternative files. 这里的想法是将大部分配置保留在docker-compose.yml中,并且只保留备用文件中的最小差异集。
Example: 例:
docker-compose.prod.yml 泊坞窗,compose.prod.yml
version: '3'
services:
nginx:
volumes:
- asset-volume:/var/lib/assets
docker-compose.dev.yml 泊坞窗,compose.dev.yml
version: '3'
services:
nginx:
volumes:
- ./assets:/var/lib/assets
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.