Docker compose 如何将路径从一个容器挂载到另一个容器? [英] Docker compose how to mount path from one to another container?

查看:149
本文介绍了Docker compose 如何将路径从一个容器挂载到另一个容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 nignx 容器 和一个 asset 容器,我的所有资产都是通过 grunt 或其他一些工具构建的.

I've nignx container and one asset container which have all my assets build from grunt or some other tools.

现在在 docker compose 文件中,我想将 asset container's 的文件夹路径挂载到 nginx container 中,以便 nginx 可以提供这些文件.

Now in docker compose file, i want to mount asset container's 's folder path into nginx container so nginx can serve that files.

  • 我们该怎么做呢?我不记得了,但我认为有一个选项可以让我们与另一个容器共享一个容器的路径.

  • 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.

假设如果我将 nginx 扩展到 2 个容器,那么该挂载是否适用于所有 nginx 实例?

Suppose if i scale up nginx to 2 container then will that mount works for all instance of nginx?

如果我扩大 asset container 那么会发生什么?

if i scale up asset container then what will happen?

我也想将它安装到我的主机上,以便轻松完成开发.

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.

您可以完全在 Docker 内部完成此操作.

You can do this completely inside of 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:

底部是定义的单个卷,名为asset-volume".

At the bottom is a single volume defined, named "asset-volume".

然后在您的每个服务中,您告诉 Docker 将该卷挂载到特定路径.我展示了容器内的示例路径,只需将它们调整为您希望它们在容器中的任何路径.

Then in each of your services, you tell Docker to mount that volume at a certain path. 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.

请注意,如果您希望只有一个可以进行更改,您始终可以在某些服务中将卷挂载为只读,方法是在卷字符串的末尾添加 :ro.

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.

services:
  servicename:
    volumes:
      - asset-volume:/var/lib/assets:ro

使用主机目录

或者,您可以使用主机上的目录并将其挂载到容器中.这样做的好处是您可以使用 Docker 之外的工具(例如 GUI 文本编辑器和其他工具)直接处理文件.

Using a host directory

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中定义一个volume,而是挂载了外部目录.

It's the same, except you don't define a volume in Docker, instead mounting the external directory.

version: '3'
services:
  nginx:
    volumes:
      - ./assets:/var/lib/assets
  asset:
    volumes:
      - ./assets:/var/lib/assets

在本例中,本地目录assets"使用相对路径 ./assets 挂载到两个容器中.

In this example, the local directory "assets" is mounted into both containers using the relative path ./assets.

您还可以针对不同的开发和生产环境进行设置.将所有内容放入 docker-compose.yml 除了 卷挂载.然后再制作两个文件.

You can also set it up for a different dev and production environment. Put everything in docker-compose.yml except the volume mounts. Then make two more files.

  • docker-compose.dev.yml
  • docker-compose.prod.yml

在这些文件中只放置定义卷挂载的最小配置.我们将把它与 docker-compose.yml 混合以获得最终配置.

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 -f docker-compose.yml 
    -f docker-compose.dev.yml 
    up -d

对于生产,只需使用 prod 文件而不是 dev 文件.

And for production, just use the prod file instead of the dev file.

这里的想法是在 docker-compose.yml 中保留大部分配置,而在替代文件中只保留最小的差异集.

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.prod.yml

version: '3'
services:
  nginx:
    volumes:
      - asset-volume:/var/lib/assets

docker-compose.dev.yml

version: '3'
services:
  nginx:
    volumes:
      - ./assets:/var/lib/assets

这篇关于Docker compose 如何将路径从一个容器挂载到另一个容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆