【问题标题】:proxy'ing between docker containersdocker容器之间的代理
【发布时间】:2017-10-12 23:17:18
【问题描述】:

我有一个带有以下内容的 docker 设置

  1. rails api 后端
  2. mysql数据库
  3. redis 数据库
  4. 节点/反应前端(webpack)
  5. nginx 服务于前端

(rails 后端目前正在通过内置 puma 服务器提供服务 - 我想我会将其移动到运行节点应用程序的同一 nginx 服务器)

我的问题是前端会在后端请求东西,但这不起作用。

我在nginx上设置了一个代理如下:

#nginx.conf

server {
    listen 8080;

    # Always serve index.html for any request
      location / {
        # Set path
        root /wwwroot/;
        try_files $uri /index.html;
      }

      location /api/ {
        proxy_pass http://127.0.0.1:3000;
      }
}

但是当我发起 api 调用时,我在 nginx 日志中得到以下信息:

nginx-server | 2017/05/13 20:56:08 [error] 5#5: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: , request: "POST /api/authenticate HTTP/1.1", upstream: "http://127.0.0.1:3000/api/authenticate", host: "localhost:8080", referrer: "http://localhost:8080/"
nginx-server | 172.21.0.1 - - [13/May/2017:20:56:08 +0000] "POST /api/authenticate HTTP/1.1" 502 575 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" "-"

而且我没有看到任何东西击中 puma 服务器。

我不确定我应该去哪里找。这是我的 docker-compose 文件的问题还是 nginx 问题(或两者兼而有之)。 我在下面包含了我的 docker-compose.yml:

version: '2'

services:
  nginx:
    build:
      context: .
      dockerfile: docker.nginx
    image: pt-nginx
    container_name: nginx-server
    ports:
      - "8080:8080"
    volumes:
      - wwwroot:/wwwroot

  webpack:
    build:
      context: ./frontend
      dockerfile: docker.webpack
    image: pt-webpack
    container_name: react-frontend
    ports:
      - "35729:35729"
    volumes:
      - ./frontend:/app
      - /app/node_modules
      - wwwroot:/wwwroot
  db:
    build:
      context: ./backend
      dockerfile: docker.mysql
    image: pt-mysql
    container_name: mysql-server

    env_file: ./backend/.env
    ports:
      - "3306:3306"
    volumes:
      - ./sql/data/:/var/lib/mysql

  redis:
    build:
      context: ./backend
      dockerfile: docker.redis
    image: pt-redis
    container_name: redis-server

  app:
    build:
      context: ./backend
      dockerfile: docker.rails
    image: pt-rails
    container_name: rails-server

    command: >
      sh -c '
      rake resque:start_workers;
      bundle exec rails s -p 3000 -b 0.0.0.0;
      '
    env_file: ./backend/.env
    volumes:
      - ./backend:/usr/src/app
      - /Users/mh/Pictures/ROR_PT/phototank:/Users/martinhinge/Pictures/ROR_PT/phototank
    ports:
      - "3000:3000"
    expose:
      - "3000"
    depends_on:
      - db
      - redis

volumes:
  wwwroot:
    driver: local

【问题讨论】:

    标签: ruby-on-rails nginx docker docker-compose


    【解决方案1】:

    问题是您的 nginx 服务正在请求 localhost (127.0.0.1) 的上游。但是应用程序在另一个容器中(使用另一个 IP)。您可以通过 DNS 在app引用rails 应用程序,因为它们都在默认网络中。 nginx 配置中的上游看起来类似于 proxy_pass http://app:3000;

    https://docs.docker.com/compose/networking/ 上阅读有关网络的更多信息,具体而言:

    在 Web 容器中,您到 db 的连接字符串看起来像 postgres://db:5432,而在主机上,连接字符串看起来像 postgres://{DOCKER_IP}:8001。

    【讨论】:

    • 我按照建议进行了更改,但 nginx 无法识别 'app',我得到:2017/05/14 09:54:06 [emerg] 11#11: host not found in upstream "app" in /etc/nginx/nginx.conf:43 nginx: [emerg] host not found in upstream "app" in /etc/nginx/nginx.conf:43 - 我需要使用环境变量
    • 可能是因为没有链接:docs.docker.com/compose/networking/#links。但默认情况下不应要求它们。可以试试docker exec进入nginx容器看看能不能ping通app吗?
    • 当我尝试构建时,我之前的评论出现了错误:docker-compose build app。是的,我可以 ping app
    • 这没有意义。您可以为app 发布Dockerfile 吗?该错误是 nginx 错误。这对我来说听起来像 build 步骤可能正在启动 nginx 服务,但我不知道为什么。
    • 你是绝对正确的。它在 nginx docker 文件中。首先它将 nginx.conf 从主机复制到容器,然后它会启动 nginx。我现在已将 nginx.conf 从主机映射到 docker-compose.yml 文件中的容器,因此我不必在 nginx.conf 更改时重新构建。此外,我已经从 dockerfile 中删除了启动...简而言之,它可以工作!!!谢谢你的帮助!!
    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多