Proxying API Requests in Docker Container running react app

后端 未结 2 1909
闹比i
闹比i 2021-01-25 20:17

I\'m running a simple react app in a docker container. During development I\'m using the proxy key in package.json to specify my backend api url: \"proxy\": \

相关标签:
2条回答
  • 2021-01-25 20:51

    You're now running your React app inside a Docker container, so your "localhost" is no longer your local machine, but the container instead. You need to proxy-it to your backend's IP. Are you running the API in another container?

    0 讨论(0)
  • 2021-01-25 20:55

    You need to set the port to your backend service instead of localhost while running the app in docker. Check the following docker container and it's services for example. We have the frontend running in port 3000 and backend running in port 5000. So, replace localhost with "proxy": "http://backend:5000"

    version: '3'
    
    services:
      backend:
        build: ./backend
        ports:
          - 5000:5000
      frontend:
        build: ./frontend
        ports:
          - 3000:3000
        links:
          - backend
        command: npm start
    
    
    0 讨论(0)
提交回复
热议问题