多个容器之间的 Docker 命名卷

标签 docker docker-compose volumes

我想将一些服务部署到我的服务器中,它们都将使用 nginx 作为 Web 服务器,每个项目都有自己的 .conf 文件,我想与 nginx 容器共享所有这些。我尝试使用命名卷,但是当它被多个容器使用时,数据会被替换。我想从不同的容器中获取所有这些 .conf 文件并放入一个卷中,以便 nginx 容器可以读取它。我也尝试在命名卷中使用子目录,但是,使用 namedVolumeName/path 不起作用。
Obs:我在所有项目中都使用 docker-compose

version: "3.7"

services:
  backend:
    container_name: jzmimoveis-backend
    image: paulomesquita/jzmimoveis-backend
    command: uwsgi --socket :8000 --wsgi-file jzmimoveis/wsgi.py
    volumes:
      - nginxConfFiles:/app/nginx
      - jzmimoveisFiles:/app/src
    networks:
      - jzmimoveis
    restart: unless-stopped
    expose:
      - 8000

  frontend:
    container_name: jzmimoveis-frontend
    image: paulomesquita/jzmimoveis-frontend
    command: serve -s build/
    volumes:
      - nginxConfFiles:/app/nginx
    networks:
      - jzmimoveis
    restart: unless-stopped
    expose:
      - 5000

volumes:
  nginxConfFiles:
    external: true
  jzmimoveisFiles:
    external: true
networks:
  jzmimoveis:
    external: true
例如,在这种情况下,我是否将前端和后端 nginx 文件都链接到命名卷 nginxConfFiles,但是,当我这样做时 docker-compose up -d在这个文件中,只有一个 .conf 文件出现在卷中,我认为它会被同一文件中的另一个容器覆盖。

最佳答案

或许您可以在 nginx 容器上拥有指向 /etc/nginx/conf.d 的共享卷。 ,然后为每个项目配置文件使用不同的名称。
下面是一个概念验证,三个服务器,每个服务器都附加一个配置文件,一个代理(你的 Nginx),共享卷绑定(bind)到 /config :

version: '3'

services:
  server1:
    image: busybox:1.31.1
    volumes:
    - deleteme_after_demo:/config
    - ./server1.conf:/app/server1.conf
    command: sh -c "cp /app/server1.conf /config; tail -f /dev/null"

  server2:
    image: busybox:1.31.1
    volumes:
    - deleteme_after_demo:/config
    - ./server2.conf:/app/server2.conf
    command: sh -c "cp /app/server2.conf /config; tail -f /dev/null"

  server3:
    image: busybox:1.31.1
    volumes:
    - deleteme_after_demo:/config
    - ./server3.conf:/app/server3.conf
    command: sh -c "cp /app/server3.conf /config; tail -f /dev/null"

  proxy1:
    image: busybox:1.31.1
    volumes:
    - deleteme_after_demo:/config:ro
    command: tail -f /dev/null

volumes:
  deleteme_after_demo:
让我们创建 3 个要包含的配置文件:
➜ echo "server 1" > server1.conf
➜ echo "server 2" > server2.conf
➜ echo "server 3" > server3.conf
然后:
➜ docker-compose up -d                  
Creating network "deleteme_default" with the default driver
Creating deleteme_server2_1 ... done
Creating deleteme_server3_1 ... done
Creating deleteme_server1_1 ... done
Creating deleteme_proxy1_1  ... done
最后,让我们验证配置文件是否可以从代理容器访问:
➜ docker-compose exec proxy1 sh -c "cat /config/server1.conf"
server 1

➜ docker-compose exec proxy1 sh -c "cat /config/server2.conf"
server 2

➜ docker-compose exec proxy1 sh -c "cat /config/server3.conf"
server 3
我希望它有所帮助。
干杯!
注意:您应该看到挂载卷的方式与使用 Unix 挂载命令完全相同。如果您在挂载点内已经有内容,则在挂载后您将不会看到它,而是已挂载设备的内容(除非它是空的并且首先在此处创建)。无论您想看到什么,都必须已经在设备上,或者您需要在之后移动它。
因此,我通过挂载文件来做到这一点,因为我使用的容器中没有数据。然后用启动命令复制这些。您可以通过不同的方式解决它,例如通过使用镜像中的入口点脚本将配置文件复制到已安装的卷。

关于多个容器之间的 Docker 命名卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62616727/

相关文章:

docker - 通过 docker-compose 运行具有多个卷的多个实例

python-3.x - Docker 名称错误 : name 'app' is not defined

docker - 测试容器中的体积

docker - Traefik-本地https无法正常工作。无法访问服务器

docker - 使用 Docker Compose 将文件复制到容器

Wordpress 应用程序服务的路径映射非常慢

docker - 无需链接的Docker卷。用例是什么?

Docker:引起:java.nio.file.FileSystemException:./conf/flow.xml.gz:资源忙

git - 带有 git 私有(private)子模块的 docker autobuild

docker - 容器中的不同图像