简体   繁体   中英

proxy'ing between docker containers

I have a docker setup with the following

  1. rails api backend
  2. mysql db
  3. redis db
  4. node/react frontend (webpack)
  5. nginx serving the frontend

(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)

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.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:

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.

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). I have included my docker-compose.yml below:

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). But the application is in another container (with another IP). You can reference the rails application by DNS at app since they are both in a default network. The upstream in nginx configuration would look something like proxy_pass http://app:3000; instead.

Read more about the networking at https://docs.docker.com/compose/networking/ , specifically:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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