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

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

问题描述

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

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

 版本:"3.6"服务:网络:建造:语境: .dockerfile:./Dockerfile图片:webtest命令:./start.sh数量:- .:/代码-静态文件:/static/teststaticfilesnginx:图片:nginx:1.15.8-alpine端口:-"80:80"数量:-./nginx-config:/etc/nginx/conf.d-静态文件:/static/teststaticfiles取决于:-网络数量:静态文件: 

但是,我实际需要的是使nginx撰写文件位于单独的文件中,也可以位于完全不同的文件夹中.换句话说, docker compose up 命令将单独运行.我尝试了以下方法:

第一个撰写文件:

 版本:"3.6"服务:网络:建造:语境: .dockerfile:./Dockerfile图片:webtest命令:./start.sh数量:- .:/代码-静态文件:/static/teststaticfiles网络:-directorylocation-nginx_mynetwork数量:静态文件:网络:directorylocation-nginx_mynetwork:外部:真 

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

 版本:"3.6"服务:nginx:图片:nginx:1.15.8-alpine端口:-"80:80"数量:-./nginx-config:/etc/nginx/conf.d-静态文件:/static/teststaticfiles网络:- 我的网络数量:静态文件:网络:我的网络: 

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

在此处找到,可以正常工作的一种解决方法是将nginx容器静态文件的体积改为如下:

 <代码>-/var/lib/docker/volumes/directory_static-files/_data:/static/teststaticfiles 

上面的方法可以正常工作,但是看起来很脆弱"且脆弱.是否有另一种方法可以在容纳在不同撰写文件中的容器之间共享卷,而无需映射/var/lib/docker/volumes 目录.

解决方案

像在问题中一样,通过分离2个 docker-compose.yml 文件,实际上创建了2个不同的卷.这就是为什么在 nginx 服务的卷中看不到 web 服务中的数据的原因,因为只有两个不同的卷.

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

 示例/|-网络/|-docker-compose.yml#您的第一个docker撰写文件|-nginx/|-docker-compose.yml#您的第二个docker compose文件 

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

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

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

 卷:静态文件:外部的:名称:web_static-files 

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

  docker-compose \-f web/docker-compose.yml \-p abcd \向上 

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

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

 版本:"3.6"数量:静态文件: 

并在Web和Nginx docker-compose.yml 文件中将其作为外部卷使用,名称为 volumes_static-files :

 卷:volumes_static-files:外部:真 

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

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

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:

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:

First compose file:

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

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:

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.

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

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.

解决方案

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

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

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

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

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

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

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

version: '3.6'

volumes:
  static-files:

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

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撰写文件之间共享卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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