首页 > 解决方案 > 错误:在 dockerized vue 应用程序中连接 ECONNREFUSED 127.0.0.1:8080

问题描述

我有一个 vuejs 应用程序,我正在尝试设置使用 docker + docker-compose 的无头测试。我似乎无法解决服务似乎都可以正确启动但似乎无法相互通信的问题:

Dockerfile:

FROM node:lts-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install -f

EXPOSE 8080
# Note 8080 is exposed! I'm not crazy!
CMD ["npm", "run", "serve"]

码头工人撰写文件:

version: '3.5'
services:
    my-app:
        build: .
        ports: 
            - "8080:8080"
        volumes:
          - type: bind
            source: ./app
            target: /app
          - "/app/node_modules"
    cypress:
      image: "cypress/included:8.2.0"
      depends_on:
        - my-app
      #environment:
      #  - CYPRESS_baseUrl=http://localhost:8080
      working_dir: /app
      volumes: 
        - type: bind
          source: ./app
          target: /app

volumes: 
  app: 

但是,cypress 会吐出如下错误:

cypress_1  | http://localhost:8080/thank-you
cypress_1  | 
cypress_1  | We attempted to make an http request to this URL but the request failed without a response.
cypress_1  | 
cypress_1  | We received this error at the network level:
cypress_1  | 
cypress_1  |   > Error: connect ECONNREFUSED 127.0.0.1:8080
cypress_1  | 
cypress_1  | Common situations why this would fail:
cypress_1  |   - you don't have internet access
cypress_1  |   - you forgot to run / boot your web server
cypress_1  |   - your web server isn't accessible
cypress_1  |   - you have weird network configuration settings on your computer
cypress_1  |       at http://localhost:44829/__cypress/runner/cypress_runner.js:143973:23
cypress_1  |       at visitFailedByErr (http://localhost:44829/__cypress/runner/cypress_runner.js:143332:12)
cypress_1  |       at http://localhost:44829/__cypress/runner/cypress_runner.js:143972:11
cypress_1  |       at tryCatcher (http://localhost:44829/__cypress/runner/cypress_runner.js:13212:23)
cypress_1  |       at Promise._settlePromiseFromHandler (http://localhost:44829/__cypress/runner/cypress_runner.js:11147:31)
cypress_1  |       at Promise._settlePromise (http://localhost:44829/__cypress/runner/cypress_runner.js:11204:18)
cypress_1  |       at Promise._settlePromise0 (http://localhost:44829/__cypress/runner/cypress_runner.js:11249:10)
cypress_1  |       at Promise._settlePromises (http://localhost:44829/__cypress/runner/cypress_runner.js:11325:18)
cypress_1  |       at _drainQueueStep (http://localhost:44829/__cypress/runner/cypress_runner.js:7919:12)
cypress_1  |       at _drainQueue (http://localhost:44829/__cypress/runner/cypress_runner.js:7912:9)
cypress_1  |       at Async.../../node_modules/bluebird/js/release/async.js.Async._drainQueues (http://localhost:44829/__cypress/runner/cypress_runner.js:7928:5)
cypress_1  |       at Async.drainQueues (http://localhost:44829/__cypress/runner/cypress_runner.js:7798:14)
cypress_1  |   From Your Spec Code:
cypress_1  |       at Context.eval (http://localhost:44829/__cypress/tests?p=tests/e2e/specs/views/ThankYou.js:101:8)
cypress_1  |   
cypress_1  |   From Node.js Internals:
cypress_1  |     Error: connect ECONNREFUSED 127.0.0.1:8080
cypress_1  |         at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)

我曾经提出此设置的参考资料:

注意:我尝试了许多 baseUrl 的变体(localhost、my-app 等)。所有导致同样的问题。

标签: dockervue.jscypress

解决方案


localhost在容器的上下文中是容器本身。因此,当您的 cypress 容器尝试连接到 localhost:8080 时,它会尝试连接到自身的 8080 端口。

Docker compose 创建了一个虚拟网络,每个容器都可以通过其服务名称来寻址。所以 cypress 需要连接http://my-app:8080/thank-you而不是http://localhost:8080/thank-you.

当连接来自同一虚拟网络上的容器时,它连接到的端口是内部端口。因此,即使您不映射端口,它也可以工作。如果您想从主机访问容器,您只需要映射端口。

要考虑的另一件事是,您的节点应用程序需要接受来自 localhost 以外的其他来源的连接。如果没有,请确保绑定到 .0.0.0.0 上app.listen


推荐阅读