NGINX和多个docker

编程入门 行业动态 更新时间:2024-10-26 07:33:36
本文介绍了NGINX和多个docker-compose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果要使用Docker容器设置NGINX,一种选择是在docker-compose.yml中设置NGINX实例,并将NGINX容器链接到所有应用程序容器.

但是,此方法的缺点是docker-compose.yml变为服务器级,因为只有一个NGINX容器才能将端口80/443公开给Internet.

我感兴趣的是能够在同一服务器上定义多个docker-compose.yml,但仍然可以通过单个服务器特定的NGINX容器轻松地在每个撰写文件中公开面向公众的容器.

我觉得这应该很容易,但是我还没有找到一个好的资源或例子.

解决方案

首先,您需要为Nginx和代理容器创建网络:

docker network create nginx_network

接下来,运行带有撰写文件的nginx容器,如下所示:

services: nginx: image: your_nginx_image ports: - "80:80" - "443:443" networks: - nginx_network networks: nginx_network: external: true

之后,您可以运行代理容器:

services: webapp1: image: ... container_name: mywebapp1 networks: - nginx_network #proxy and app must be in same network - webapp1_db_network #you can use additional networks for some stuff database: image: ... networks: - webapp1_db_network networks: nginx_network: external: true webapp1_db_network: ~ #this network won't be accessible from outside

此外,要执行此操作,您需要正确配置nginx:

server { listen 80; server_name your_app.example; #Docker DNS resolver 127.0.0.11; location / { #hack to prevent nginx to resolve container's host on start up set $docker_host "mywebapp1"; proxy_pass $docker_host:8080; } }

您需要告诉nginx使用Docker的DNS,以便它能够通过容器名称访问容器.

但是请注意,如果您先运行nginx容器,则nginx将尝试解析另一个容器的主机并失败,因为其他容器尚未运行.您可以将主机放入变量中使用技巧.有了这个hack,nginx才会在收到请求之前尝试解析主机.

通过这种组合,您可以始终使nginx正常运行,同时独立启动和停止代理的应用程序.

UPD: 如果您需要更多动态解决方案,则可以通过以下方式修改Nginx配置:

server { listen 80; resolver 127.0.0.11; #define server_name with regexp which will read subdomain into variable server_name ~^(?<webapp>.+)\.example\; location / { #use variable from regexp to pass request to desired container proxy_pass $webapp:8080; } }

通过对webapp1.example的此类配置请求,将传递到容器"webapp1",将webapp2.example传递到"webapp2"等.您只需要添加DNS记录并使用正确的名称运行应用程序容器即可. /p>

If I want to setup NGINX with my docker containers, one option is to setup the NGINX instance in my docker-compose.yml, and link the NGINX container to all application containers.

The drawback of this approach, however, is that the docker-compose.yml becomes server-level, since only one NGINX container can expose port 80/443 to the internet.

What I am interested in, is to be able to define several docker-compose.yml on the same server, but still easily expose the public-facing containers in each compose file via a single server-specific NGINX container.

I feel this should be pretty easy, but I haven't been able to find a good resource or example for this.

解决方案

First, you need to create network to for Nginx and proxied containers:

docker network create nginx_network

Next, run nginx container with compose file like this:

services: nginx: image: your_nginx_image ports: - "80:80" - "443:443" networks: - nginx_network networks: nginx_network: external: true

After that you can run proxied containers:

services: webapp1: image: ... container_name: mywebapp1 networks: - nginx_network #proxy and app must be in same network - webapp1_db_network #you can use additional networks for some stuff database: image: ... networks: - webapp1_db_network networks: nginx_network: external: true webapp1_db_network: ~ #this network won't be accessible from outside

Also, to make this work you need to configure your nginx properly:

server { listen 80; server_name your_app.example; #Docker DNS resolver 127.0.0.11; location / { #hack to prevent nginx to resolve container's host on start up set $docker_host "mywebapp1"; proxy_pass $docker_host:8080; } }

You need to tell nginx to use Docker's DNS, so it will be able to access containers by their names.

But note, that if you run nginx container before others, then nginx will try to resolve another containers' host and fail, because other containers are not running yet. You can use a hack with placing host into variable. With this hack nginx won't try to resolve host until receiving a request.

With this combination you can have nginx always up, while starting and stopping proxied applications independently.

UPD: If you want more dynamic solution, you can modify Nginx config the following way:

server { listen 80; resolver 127.0.0.11; #define server_name with regexp which will read subdomain into variable server_name ~^(?<webapp>.+)\.example\; location / { #use variable from regexp to pass request to desired container proxy_pass $webapp:8080; } }

With such configuration request to webapp1.example will be passed to container "webapp1", webapp2.example to "webapp2" etc. All you need is just add DNS records and run app containers with right name.

更多推荐

NGINX和多个docker

本文发布于:2023-10-22 08:38:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1517004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   NGINX   docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!