Linked docker-compose containers making http requests

前端 未结 1 938
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 09:41

I\'ve been looking on the web for a while and haven\'t found a solution to my problem which is similar to this StackOverflow question Docker HTTP-requests between containers. Bu

1条回答
  •  不思量自难忘°
    2021-01-21 10:14

    Since you make an Ajax call it will be sent by the browser which is client side and not server side. So when you make a call from :3000 to app:3030 for API, your browser has no idea what app is. So you have few things you can do

    Using host file

    /etc/hosts

     app 
    

    Then when you browse app using app:3000, app:3030 will automatically point to the correct address.

    Using javascript to determine API url

    You can use javascript to get the url that you should be using for api

    document.location.scheme + "://" + document.location.hostname + ":3030"
    

    Using nginx

    You can create nginx reverse proxy

    location / {
       proxy_pass http://localhost:3000;
    
       location /api {
          proxy_pass http://localhost:3030;
       }
    }
    

    This will require some change in your code file so that you use /api

    0 讨论(0)
提交回复
热议问题