简体   繁体   English

React 应用程序集代理不适用于 docker compose

[英]React app set proxy not working with docker compose

I am trying to use the docker compose to build two containers:我正在尝试使用 docker compose 来构建两个容器:

  1. React app反应应用
  2. A flask server powered with gunicorn由 gunicorn 驱动的 Flask 服务器

I docker composed them up and both of them were boosted up.我 docker 将它们组合起来,并且它们都得到了提升。 When I visited the react, it supposed to proxy the request from the react app with port 3000 to flask server with port 5000. But I encountered this:当我访问react时,它应该将来自react应用程序的3000端口的请求代理到5000端口的flask服务器。但我遇到了这个:

frontend_1  | Proxy error: Could not proxy request /loadData/ from localhost:3000 to http://backend:5000.
frontend_1  | See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

which I figure means it still does not know the actual IP address of the backend container.我认为这意味着它仍然不知道后端容器的实际 IP 地址。

Here are some configurations:下面是一些配置:

docker-compose.yml docker-compose.yml

version: "3"

services:
    backend:
        build: ./
        expose: 
            - "5000"
        ports: 
            - "5000:5000"
        volumes: 
            - .:/app
        command: gunicorn server:app -c ./gunicorn.conf.py

        networks: 
            - app-test
    frontend:
        build: ./frontend
        expose: 
            - "3000"
        ports: 
            - "3000:3000"
        volumes: 
            - ./frontend:/app
        networks: 
            - app-test
        depends_on: 
            - backend
        links:
            - backend
        command: yarn start


networks:
    app-test:
        driver: bridge

backend Dockerfile后端 Dockerfile

FROM python:3.7.3

WORKDIR /app 

COPY . /app 

RUN pip install -r requirements.txt 

frontend Dockerfile前端 Dockerfile

FROM node:latest

WORKDIR /app 

COPY . /app 

gunicorn.conf.py gunicorn.conf.py

workers = 5
worker_class = "gevent"
bind = "127.0.0.1:5000"

frontend package.json前端 package.json

{
  "proxy": "http://backend:5000",
}

I tried nearly everything said online, and it just does not proxy the request.我几乎尝试了网上所说的所有内容,但它只是不代理请求。

Some information I have already known:我已经知道的一些信息:

  1. Both containers are worked.两个容器都工作了。
  2. I can ping the internal IP from the frontend container to the backend, and it responds, so no network issues.我可以将前端容器的内部 IP ping 到后端,它会响应,所以没有网络问题。
  3. when localhost:3000 is requested, my system will call Axios to send a POST request (/loadData) to the backend, where the proxy part should do the work and then the request should become somebackendip:5000/loadData/当请求 localhost:3000 时,我的系统会调用 Axios 向后端发送 POST 请求 (/loadData),代理部分应该在这里完成工作,然后请求应该变成 somebackendip:5000/loadData/

Anyone could help me?任何人都可以帮助我吗?

Thanks in advance!提前致谢!

尝试在 gunicorn conf 中将bind = "0.0.0.0:5000"更改为bind = "0.0.0.0:5000"并将后端服务中的端口相应地更改为"127.0.0.1:5000:5000" (最后一个是可选的)

By changing gunicorn.conf.py with通过改变gunicorn.conf.py

bind = "0.0.0.0:5000"

fixed my problem.解决了我的问题。

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

相关问题 Docker 与反应应用程序组成网络问题 - Docker compose networking issue with react app 设置代理服务器以创建反应应用程序 - Set up proxy server for create react app Docker 撰写未正确复制文件以运行反应应用程序 - Docker compose not properly copying files to run react app 了解 docker 构成 django 的端口接线,反应 app 和 haproxy - Understanding docker compose port wiring for django, react app and haproxy 如何在 nginx 上为反应应用程序设置代理通行证? - How to set proxy pass for react app on nginx? 在 docker-compose 中没有启动带有 craco 的 React App - React App with craco doesn't start in docker-compose Docker Compose 与 Docker 工具箱:Node、Mongo、React。 React 应用程序未显示在所述地址中 - Docker Compose with Docker Toolbox: Node, Mongo, React. React app not showing in the said adress 使用 docker-compose 构建应用程序时,如何在 React 应用程序中为 URL 端点使用 Docker 容器名称? - How to use Docker container name for URL endpoint in React app when building app with docker-compose? npm 包/库不能在 Docker 中用于反应应用程序? - npm packages/libraries not working in Docker for react app? Docker:docker-compose用于捆绑流星应用程序不起作用..(npm错误) - Docker: docker-compose for bundling meteor app not working.. (npm errors)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM