简体   繁体   English

使用 nginx 反向代理运行多个 docker-compose 文件

[英]Running multiple docker-compose files with nginx reverse proxy

I asked a question here and got part of my problem solved, but I was advised to create another question because it started to get a bit lengthy in the comments.在这里问了一个问题并解决了我的部分问题,但我被建议创建另一个问题,因为它在评论中开始变得有点冗长。

I'm trying to use docker to run multiple PHP,MySQL & Apache based apps on my Mac, all of which would use different docker-compose.yml files (more details in the post I linked).我正在尝试使用docker-compose.yml在我的 Mac 上运行多个基于 PHP、MySQL 和 Apache 的应用程序,所有这些应用程序都将使用不同docker-compose.yml文件(更多详细信息在我链接的帖子中)。 I have quite a few repositories, some of which communicate with one another, and not all of them are the same PHP version.我有很多存储库,其中一些存储库相互通信,而且并非所有存储库都是相同的 PHP 版本。 Because of this, I don't think it's wise for me to cram 20+ separate repositories into one single docker-compose.yml file.正因为如此,我认为将 20 多个单独的存储库塞进一个 docker-compose.yml 文件是不明智的。 I'd like to have separate docker-compose.yml files for each repository and I want to be able to use an /etc/hosts entry for each app so that I don't have to specify the port.我想为每个存储库使用单独的 docker-compose.yml 文件,并且我希望能够为每个应用程序使用/etc/hosts条目,这样我就不必指定端口。 Ex: I would access 2 different repositories such as http://dockertest.com and http://dockertest2.com (using /etc/hosts entries), rather than having to specify the port like http://dockertest.com:8080 and http://dockertest.com:8081 .例如:我会访问 2 个不同的存储库,例如http://dockertest.comhttp://dockertest2.com (使用/etc/hosts条目),而不必像http://dockertest.com:8080那样指定端口http://dockertest.com:8080http://dockertest.com:8081

Using the accepted answer from my other post I was able to get one app running at a time (one docker-compose.yml file), but if I try to launch another with docker-compose up -d it results in an error because port 80 is already taken.使用我另一篇文章中接受的答案,我能够一次运行一个应用程序(一个 docker-compose.yml 文件),但是如果我尝试使用docker-compose up -d启动另一个应用程序,则会导致错误,因为端口80 已经被占用了。 How can I runn multiple docker apps at the same time, each with their own docker-compose.yml files and without having to specify the port in the url?如何同时运行多个 docker 应用程序,每个应用程序都有自己docker-compose.yml文件,而不必在 url 中指定端口?

Here's a docker-compose.yml file for the app I made.这是我制作的应用程序的 docker-compose.yml 文件。 In my /etc/hosts I have 127.0.0.1 dockertest.com在我的/etc/hosts我有127.0.0.1 dockertest.com

version: "3.3"
services:
  php:
    build: './php/'
    networks:
      - backend
    volumes:
      - ./public_html/:/var/www/html/
  apache:
    build: './apache/'
    depends_on:
      - php
      - mysql
    networks:
      - frontend
      - backend
    volumes:
      - ./public_html/:/var/www/html/
    environment:
      - VIRTUAL_HOST=dockertest.com
  mysql:
    image: mysql:5.6.40
    networks:
      - backend
    environment:
      - MYSQL_ROOT_PASSWORD=rootpassword
  nginx-proxy:
    image: jwilder/nginx-proxy
    networks:
      - backend
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
networks:
  frontend:
  backend:

I would suggest to extract the nginx-proxy to a separate docker-compose.yml and create a repository for the "reverse proxy" configuration with the following:我建议将nginx-proxy提取到单独docker-compose.yml并使用以下内容为“反向代理”配置创建一个存储库:

A file with extra contents to add to /etc/hosts包含要添加到/etc/hosts额外内容的文件

127.0.0.1 dockertest.com
127.0.0.1 anothertest.com
127.0.0.1 third-domain.net

And a docker-compose.yml which will have only the reverse proxy还有一个docker-compose.yml ,它只有反向代理

version: "3.3"
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

Next, as you already mentioned, create a docker-compose.yml for each of your repositories that act as web endpoints.接下来,正如您已经提到的,为作为 Web 端点的每个存储库创建一个docker-compose.yml You will need to add VIRTUAL_HOST env var to the services that serve your applications (eg. Apache).您需要将VIRTUAL_HOST env var 添加到为您的应用程序(例如 Apache)提供服务的服务中。

The nginx-proxy container can run in "permanent mode", as it has a small footprint. nginx-proxy容器可以在“永久模式”下运行,因为它占用的空间很小。 This way whenever you start a new container with VIRTUAL_HOST env var, the configuration of nginx-proxy will be automatically updated to include the new local domain.这样,每当您使用VIRTUAL_HOST env var 启动新容器时, nginx-proxy的配置将自动更新以包含新的本地域。 (You will still have to update /etc/hosts with the new entry). (您仍然需要使用新条目更新/etc/hosts )。


If you decide to use networks, your web endpoint containers will have to be in the same network as nginx-proxy , so your docker-compose files will have to be modified similar to this:如果您决定使用网络,则您的 Web 端点容器必须与nginx-proxy位于同一网络中,因此您的 docker-compose 文件必须像这样修改:

# nginx-proxy/docker-compose.yml
version: "3.3"
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    networks:
      - reverse-proxy
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
networks:
  reverse-proxy:
# service1/docker-compose.yml

version: "3.3"
services:
  php1:
    ...
    networks:
      - backend1
  apache1:
    ...
    networks:
      - nginx-proxy_reverse-proxy
      - backend1
    environment:
      - VIRTUAL_HOST=dockertest.com
  mysql1:
    ...
    networks:
      - backend1
networks:
  backend1:
  nginx-proxy_reverse-proxy:
    external: true
# service2/docker-compose.yml

version: "3.3"
services:
  php2:
    ...
    networks:
      - backend2
  apache2:
    ...
    networks:
      - nginx-proxy_reverse-proxy
      - backend2
    environment:
      - VIRTUAL_HOST=anothertest.com
  mysql2:
    ...
    networks:
      - backend2
networks:
  backend2:
  nginx-proxy_reverse-proxy:
    external: true

The reverse-proxy network that is created in nginx-proxy/docker-compose.yml is referred as nginx-proxy_reverse-proxy in the other docker-compose files because whenever you define a network - its final name will be {{folder name}}_{{network name}}nginx-proxy/docker-compose.yml创建的reverse-proxy网络在其他nginx-proxy_reverse-proxy -compose 文件中被称为nginx-proxy_reverse-proxy因为每当你定义一个网络时——它的最终名称将是{{folder name}}_{{network name}}


If you want to have a look at a solution that relies on browser proxy extension instead of /etc/hosts , check outmitm-proxy-nginx-companion如果您想查看依赖于浏览器代理扩展而不是/etc/hosts的解决方案,请查看mitm-proxy-nginx-companion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM