NGINX和多个docker-compose [英] NGINX and multiple docker-compose

查看:402
本文介绍了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.com;

    #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 http://$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\.com;

    location / {
        #use variable from regexp to pass request to desired container
        proxy_pass http://$webapp:8080;
    }
}

通过对webapp1.example.com的此类配置请求,将传递到容器"webapp1",将webapp2.example.com传递到"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.com;

    #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 http://$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\.com;

    location / {
        #use variable from regexp to pass request to desired container
        proxy_pass http://$webapp:8080;
    }
}

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

这篇关于NGINX和多个docker-compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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