docker - 错误:在 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)
我曾经提出此设置的参考资料:
- https://www.cypress.io/blog/2019/05/02/run-cypress-with-a-single-docker-command/
- https://github.com/bahmutov/cypress-open-from-docker-compose/blob/master/e2e/docker-compose.yml
- https://github.com/bahmutov/demo-docker-cypress-included/blob/master/cy-run.sh
- https://github.com/cypress-io/cypress-example-docker-compose
注意:我尝试了许多 baseUrl 的变体(localhost、my-app 等)。所有导致同样的问题。
解决方案
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
。
推荐阅读
- php - 在foreach中使用递归函数替换PHP数组值
- python - Python:从字符串中删除选定的空格
- vuejs2 - vue-i18n - 检测“未知”类型的令牌
- reactjs - 如何设置 aws-sdk 以从 s3 存储桶中的公共文件中获取文件内容?
- javascript - 动态加载图像,图像错误时中断循环
- wordpress - WP 重定向功能在实时站点上不起作用
- performance - Liferay 7 中搜索容器的性能不佳
- java - 如何修复“XSSFWorkbook 无法解析为类型”?
- c# - 如何以编程方式突出显示 DateTimePicker 中的特定部分?
- angular - 每次获取数据时,Angular rxjs-interval 运算符(轮询)都会重绘组件视图