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

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

问题描述

我有 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 容器的 文件夹路径挂载到 nginx 容器 中,以便 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?

如果我扩大资产容器,会发生什么?

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:

底部是定义的单个卷,名为资产卷".

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

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 组合如何挂载从一个容器到另一个容器的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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