使用Nginx反向代理运行多个docker-compose文件 [英] Running multiple docker-compose files with nginx reverse proxy

查看:364
本文介绍了使用Nginx反向代理运行多个docker-compose文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处问了一个问题并得到了我的问题的一部分已解决,但建议我再创建一个问题,因为它在评论中开始变得冗长.

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.

我正在尝试使用docker来运行多个PHP,MySQL&我的Mac上基于Apache的应用程序,所有这些应用程序都将使用不同的docker-compose.yml文件(我链接的文章中有更多详细信息).我有很多存储库,其中一些可以相互通信,但并非所有存储库都是相同的PHP版本.因此,我认为将20多个单独的存储库塞入一个docker-compose.yml文件并不明智.我想为每个存储库使用单独的docker-compose.yml文件,并且希望能够为每个应用程序使用/etc/hosts条目,这样我就不必指定端口.例如:我将访问2个不同的存储库,例如http://dockertest.comhttp://dockertest2.com(使用/etc/hosts条目),而不必指定像http://dockertest.com:8080http://dockertest.com:8081这样的端口.

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). I have quite a few repositories, some of which communicate with one another, and not all of them are the same PHP version. Because of this, I don't think it's wise for me to cram 20+ separate repositories into one single docker-compose.yml file. 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. 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.

使用另一篇文章中的公认答案,我能够一次运行一个应用程序(一个docker-compose.yml文件),但是如果我尝试使用docker-compose up -d启动另一个应用程序,则会导致错误,因为端口80已经被占用.我该如何同时运行多个docker应用程序,每个应用程序都有自己的docker-compose.yml文件,而不必在url中指定端口?

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. 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-compose.yml文件.在我的/etc/hosts中,我有127.0.0.1 dockertest.com

Here's a docker-compose.yml file for the app I made. In my /etc/hosts I have 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:

推荐答案

我建议将nginx-proxy提取到单独的docker-compose.yml并使用以下内容为反向代理"配置创建存储库:

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:

具有额外内容的文件,可添加到/etc/hosts

A file with extra contents to add to /etc/hosts

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

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

接下来,正如您已经提到的,为每个用作Web端点的存储库创建一个docker-compose.yml.您需要将VIRTUAL_HOST env var添加到为您的应用程序提供服务的服务(例如Apache).

Next, as you already mentioned, create a docker-compose.yml for each of your repositories that act as web endpoints. You will need to add VIRTUAL_HOST env var to the services that serve your applications (eg. Apache).

nginx-proxy容器占用空间小,因此可以在永久模式"下运行.这样,每当您使用VIRTUAL_HOST env var启动新容器时,nginx-proxy的配置都会自动更新以包括新的本地域. (您仍然必须使用新条目更新/etc/hosts).

The nginx-proxy container can run in "permanent mode", as it has a small footprint. 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. (You will still have to update /etc/hosts with the new entry).

如果您决定使用网络,则您的Web终结点容器必须与nginx-proxy处于同一网络中,因此必须像下面这样修改docker-compose文件:

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:

# 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

nginx-proxy/docker-compose.yml中创建的reverse-proxy网络在其他docker-compose文件中称为nginx-proxy_reverse-proxy,因为每当您定义网络时,其最终名称将为{{folder name}}_{{network name}}

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}}

如果您想看看依赖浏览器代理扩展而不是/etc/hosts的解决方案,请查看 mitm-proxy-nginx-companion

If you want to have a look at a solution that relies on browser proxy extension instead of /etc/hosts, check out mitm-proxy-nginx-companion

这篇关于使用Nginx反向代理运行多个docker-compose文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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