无法执行从一个泊坞窗映像到另一个泊坞窗映像的API请求

时间:2018-09-06 07:32:24

标签: reactjs docker docker-compose docker-container

我正在开发一个具有React前端和Java Spring后端的应用程序。我有一个简单的API调用,我想在其中返回一个新地址。我已经通过使用单独的容器执行curl命令docker container run --rm -it --net multichain-network byrnedo/alpine-curl http://multichain-api:8080/api/blockchain/address/create来验证了这项工作。该调用将返回预期结果。

现在我已经使用docker-compose创建了docker环境,它们都已启动并正在运行,并且可以相互ping通

version: "3.7"

networks:
  multichain-network:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.18.0.0/16
    name: multichain-network

services:
  multichain-api:
    build:
      context: ./blockchain
    networks:
      - multichain-network
    ports:
      - "8080:8080"
    volumes:
      - type: bind
        source: ${PWD}/blockchain/target
        target: /target
    container_name: multichain-api

  frontend:
    build:
      context: ./frontend
    networks:
      - multichain-network
    ports:
      - "3000:3000"
    volumes:
      - type: bind
        source: ${PWD}/frontend
        target: /frontend
    container_name: frontend

我现在还很陌生,所以我想知道这是否可以在我的代码中完成,所以这是进行调用的代码段;

getNewAddress = () => {
    console.log("Button clicked");
    axios.get('http://multichain-api:8080/api/blockchain/address/create')
        .then(function (response) {
            console.log("getting new address");
            this.setState({newAddress: response.data});
            console.log(this.state.newAddress);
        })
        .catch(function (error) {
            console.log(error);
        })
}

您会在呼叫中注意到,我直接呼叫的是容器名称,而不是IP地址。我这样做是因为它们都位于一个网络中,当我使用容器名称从前端ping后端容器时,它会起作用;

docker exec frontend ping multichain-api
PING multichain-api (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.295 ms
64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.158 ms
64 bytes from 172.18.0.4: seq=2 ttl=64 time=0.253 ms

当我的代码尝试进行调用时,这是在开发控制台上产生的错误;

VM2194:1 GET http://multichain-api:8080/api/blockchain/address/create 0 ()
(anonymous) @ VM2194:1
dispatchXhrRequest @ xhr.js:178
xhrAdapter @ xhr.js:12
dispatchRequest @ dispatchRequest.js:59
Promise.then (async)
request @ Axios.js:51
Axios.(anonymous function) @ Axios.js:61
wrap @ bind.js:9
Container._this.getNewAddress @ Container.js:12
apply @ _apply.js:15
baseInvoke @ _baseInvoke.js:21
apply @ _apply.js:16
(anonymous) @ _overRest.js:32
(anonymous) @ Button.js:74
callCallback @ react-dom.development.js:100
invokeGuardedCallbackDev @ react-dom.development.js:138
invokeGuardedCallback @ react-dom.development.js:187
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:201
executeDispatch @ react-dom.development.js:461
executeDispatchesInOrder @ react-dom.development.js:483
executeDispatchesAndRelease @ react-dom.development.js:581
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:592
forEachAccumulated @ react-dom.development.js:562
runEventsInBatch @ react-dom.development.js:723
runExtractedEventsInBatch @ react-dom.development.js:732
handleTopLevel @ react-dom.development.js:4477
batchedUpdates$1 @ react-dom.development.js:16660
batchedUpdates @ react-dom.development.js:2131
dispatchEvent @ react-dom.development.js:4556
interactiveUpdates$1 @ react-dom.development.js:16715
interactiveUpdates @ react-dom.development.js:2150
dispatchInteractiveEvent @ react-dom.development.js:4533
Container.js:19 Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)

请注意,我使用的浏览器是Chromium版本68.0.3440.106。 总而言之,我不确定这是React,docker还是浏览器问题。查看我的后端应用程序的日志,当前端进行呼叫时,没有记录任何活动,因此就好像没有被击中。

任何对此的深刻见解

更新

我在Firefox上进行了此测试,以查看是否是浏览器问题,并且那里的开发控制台应该提供更多信息;

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://multichain-api:8080/api/blockchain/test. (Reason: CORS request did not succeed).

1 个答案:

答案 0 :(得分:1)

您遇到的是cors问题,而不是docker问题。它说,发出请求的服务器的原始域(frontend-react)和服务器的原始域(backend-multichain-api)是不同的,您可以找到cors系统架构here

要在后端服务器中解决此问题,您应该告诉服务器接受来自不同域的请求。