gpt4 book ai didi

多个容器之间的 Docker 命名卷

转载 作者:行者123 更新时间:2023-12-02 17:50:11 25 4
gpt4 key购买 nike

我想将一些服务部署到我的服务器中,它们都将使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com