在单独的 docker compose 文件之间共享卷 [英] Share volumes between separate docker compose files

查看:23
本文介绍了在单独的 docker compose 文件之间共享卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图允许 nginx 在多个容器之间进行代理,同时还可以访问这些容器中的静态文件.

I am trying to allow nginx to proxy between multiple containers while also accessing the static files from those containers.

要在使用 docker compose 创建的容器之间共享卷,以下操作正确:

To share volumes between containers created using docker compose, the following works correctly:

version: '3.6'

services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: webtest
    command: ./start.sh
    volumes:
      - .:/code
      - static-files:/static/teststaticfiles

  nginx:
    image: nginx:1.15.8-alpine
    ports:
      - "80:80"
    volumes:
      - ./nginx-config:/etc/nginx/conf.d
      - static-files:/static/teststaticfiles
    depends_on:
      - web

volumes:
  static-files:

然而,我实际需要的是将 nginx 撰写文件放在一个单独的文件中,并且放在一个完全不同的文件夹中.换句话说,docker compose up 命令将单独运行.我尝试了以下方法:

However what I actually require is for the nginx compose file to be in a separate file and also in a completely different folder. In other words, the docker compose up commands would be run separately. I have tried the following:

第一个撰写文件:

version: '3.6'

services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: webtest
    command: ./start.sh
    volumes:
      - .:/code
      - static-files:/static/teststaticfiles
    networks:
      - directorylocation-nginx_mynetwork

volumes:
  static-files:

networks:
  directorylocation-nginx_mynetwork:
    external: true

第二个撰写文件(即:nginx):

Second compose file (ie: nginx):

version: '3.6'

services:
  nginx:
    image: nginx:1.15.8-alpine
    ports:
      - "80:80"
    volumes:
      - ./nginx-config:/etc/nginx/conf.d
      - static-files:/static/teststaticfiles
    networks:
      - mynetwork

volumes:
  static-files:

networks:
  mynetwork:

以上两个文件在可以查看站点的意义上正常工作.问题是静态文件在 nginx 容器中不可用.因此,该网站不显示任何图像等.

The above two files work correctly in the sense that the site can be viewed. The problem is that the static files are not available in the nginx container. The site therefore displays without any images etc.

此处找到的一种解决方法是将 nginx 容器静态文件卷改为如下所示:>

One work around which works correctly found here is to change the nginx container static files volume to instead be as follows:

- /var/lib/docker/volumes/directory_static-files/_data:/static/teststaticfiles

以上工作正常,但它似乎hacky"和脆弱.是否有另一种方法可以在不同组合文件中的容器之间共享卷,而无需映射 /var/lib/docker/volumes 目录.

The above works correctly, but it seems 'hacky' and brittle. Is there another way to share volumes between containers which are housed in different compose files without needing to map the /var/lib/docker/volumes directory.

推荐答案

通过像您在问题中所做的那样分离 2 个 docker-compose.yml 文件,实际上创建了 2 个不同的卷;这就是您在 nginx 服务的卷内看不到来自 web 服务的数据的原因,因为只有 2 个不同的卷.

By separating the 2 docker-compose.yml files as you did in your question, 2 different volumes are actually created; that's the reason you don't see data from web service inside volume of nginx service, because there are just 2 different volumes.

示例:假设您有以下结构:

Example : let's say you have the following structure :

example/
    |- web/
        |- docker-compose.yml # your first docker compose file
    |- nginx/
        |- docker-compose.yml # your second docker compose file

web 文件夹运行 docker-compose up(或 docker-compose -f web/docker-compose.yml up>example 目录)实际上会创建一个名为 web_static-files 的卷(在 docker-compose.yml 中定义的卷的名称)文件,以该文件所在的文件夹为前缀).

Running docker-compose up from web folder (or docker-compose -f web/docker-compose.yml up from example directory) will actually create a volume named web_static-files (name of the volume defined in docker-compose.yml file, prefixed by the folder where this file is located).

因此,从 nginx 文件夹运行 docker-compose up 实际上会创建 nginx_static-files 而不是重新创建- 根据需要使用 web_static-files.

So, running docker-compose up from nginx folder will actually create nginx_static-files instead of re-using web_static-files as you want.

您可以使用由 web/docker-compose.yml 创建的卷,方法是在第二个 docker compose 文件 (nginx/docker-compose.yml) 中指定这是一个外部卷,它的名字是:

You can use the volume created by web/docker-compose.yml by specifying in the 2nd docker compose file (nginx/docker-compose.yml) that this is an external volume, and its name :

volumes:
  static-files:
    external:
      name: web_static-files

请注意,如果您不希望卷(和所有资源)以文件夹名称(默认)作为前缀,而是以其他名称作为前缀,则可以将 -p 选项添加到 docker-compose 命令:

Note that if you don't want the volume (and all resources) to be prefixed by the folder name (default), but by something else, you can add -p option to docker-compose command :

docker-compose 
    -f web/docker-compose.yml 
    -p abcd 
    up

此命令现在将创建一个名为 abcd_static-files 的卷(您可以在第二个 docker compose 文件中使用该卷).

This command will now create a volume named abcd_static-files (that you can use in the 2nd docker compose file).

您还可以在自己的 docker-compose 文件(如 volumes/docker-compose.yml)上定义卷的创建:

You can also define the volumes creation on its own docker-compose file (like volumes/docker-compose.yml) :

version: '3.6'

volumes:
  static-files:

并引用此卷作为外部卷,名称为 volumes_static-files,在 web 和 nginx docker-compose.yml 文件中:

And reference this volume as external, with name volumes_static-files, in web and nginx docker-compose.yml files :

volumes:
  volumes_static-files:
    external: true

很遗憾,您无法在 docker compose 中设置卷名称,它会自动添加前缀.如果这确实是一个问题,您还可以在运行任何 docker-compose up 命令之前手动创建卷(docker volume create static-files)(我不建议这样做)解决方案,因为它添加了一个手动步骤,如果您在另一个环境中重现您的部署,则可能会忘记该步骤).

Unfortunately, you cannot set the volume name in docker compose, it will be automatically prefixed. If this is really a problem, you can also create the volume manually (docker volume create static-files) before running any docker-compose up command (I do not recommand this solution though because it adds a manual step that can be forgotten if you reproduce your deployment on another environment).

这篇关于在单独的 docker compose 文件之间共享卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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