简体   繁体   English

Docker容器之间的代理

[英]proxy'ing between docker containers

I have a docker setup with the following 我有以下内容的docker设置

  1. rails api backend Rails API后端
  2. mysql db MySQL数据库
  3. redis db Redis数据库
  4. node/react frontend (webpack) 节点/反应前端(Webpack)
  5. nginx serving the frontend Nginx服务前端

(the rails backend is currently being served through the builtin puma server - I think I will move it to the same nginx server runing the node app) (rails后端当前通过内置的puma服务器提供服务-我想我会将其移至运行节点应用程序的同一nginx服务器上)

My problem is that the frontend will request stuff on the backend, but this does not work. 我的问题是前端将在后端请求内容,但这不起作用。

I have set up a proxy on nginx as follows: 我已经在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;
      }
}

But when I when I initiate an api call I get the following in the nginx log: 但是当我发起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" "-"

And I do not see any thing hitting the puma server. 而且我看不到有什么东西撞到puma服务器。

I am not sure where I should be looking. 我不确定应该去哪里。 Is this a problem with my docker-compose file or is it a nginx issue (or both). 这是我的docker-compose文件出现问题还是nginx问题(或两者都有)。 I have included my docker-compose.yml below: 我在下面包含了我的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

The problem is that your nginx service is requesting upstream of localhost (127.0.0.1). 问题是您的nginx服务正在请求本地主机(127.0.0.1)的上游。 But the application is in another container (with another IP). 但是应用程序在另一个容器中(具有另一个IP)。 You can reference the rails application by DNS at app since they are both in a default network. 您可以通过DNS在app程序上引用Rails应用程序,因为它们都在默认网络中。 The upstream in nginx configuration would look something like proxy_pass http://app:3000; nginx配置中的上游看起来像proxy_pass http://app:3000; instead. 代替。

Read more about the networking at https://docs.docker.com/compose/networking/ , specifically: https://docs.docker.com/compose/networking/上了解有关网络的更多信息,特别是:

Within the web container, your connection string to db would look like postgres://db:5432, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:8001. 在Web容器中,您与db的连接字符串看起来像postgres:// db:5432,而在主机上,连接字符串看起来像postgres:// {{DOCKER_IP} :: 8001}。

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

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