You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker环境下Cypress无法验证服务运行问题求助

问题排查:Cypress无法验证http://client服务运行

问题背景

尝试适配cypress docker-compose示例到自有环境,执行docker-compose up时,Cypress报错无法验证http://client服务运行。本地浏览器能正常访问http://localhost:3000,且已配置wait-for-it脚本等待服务启动。

核心原因分析

  1. wait-for-it脚本目标地址错误:e2e容器内的localhost指向容器自身,而非宿主机或client服务容器,用http://localhost:3000等待根本检测不到client服务。
  2. 网络隔离问题:client服务配置了allhosts网络,但e2e服务未加入该网络,导致两个容器不在同一网络环境,无法通过服务名client互相访问。
  3. baseUrl配置冲突:环境变量CYPRESS_baseUrl=http://client和cypress.config.js里的http://localhost:3000不一致,且wait-for-it等待的地址和Cypress实际访问的地址不匹配。

解决方案

1. 统一网络配置

将e2e服务加入和client相同的allhosts网络,确保容器间能通过服务名通信:
在docker-compose.yml的e2e服务下添加:

networks:
  - allhosts

2. 修正wait-for-it目标地址

把wait-for-it等待的地址改成http://client:3000(client服务的容器内端口),同时统一CYPRESS_baseUrl:
修改e2e服务的environment和command:

environment:
  - CYPRESS_baseUrl=http://client:3000
command: ["./wait-for-it.sh", "-t","30","http://client:3000", "--", "npx","cypress","run"]

3. 优化cypress.config.js(可选)

让配置支持环境变量动态覆盖,兼顾本地开发和容器环境:

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    baseUrl: process.env.CYPRESS_baseUrl || "http://localhost:3000",
    supportFile: false,
  },
});

修改后的完整docker-compose.yml(e2e部分)

e2e:
    #image: "cypress/included:10.6.0"
    image: "cypress"
    build: ./e2e
    container_name: cypress
    depends_on:
      - client
    environment:
      - CYPRESS_baseUrl=http://client:3000
    command: ["./wait-for-it.sh", "-t","30","http://client:3000", "--", "npx","cypress","run"]
    networks:
      - allhosts
    volumes:
      - ./e2e/cypress:/app/cypress
      - ./e2e/cypress.config.js:/app/cypress.config.js
      - ./e2e/wait-for-it.sh:/app/wait-for-it.sh

内容的提问来源于stack exchange,提问作者mheavers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 14:06:40