Docker 中的赛普拉斯无法连接到端口 4200 上的 angular 本地主机

Cypress in Docker can't connect to angular localhost on port 4200

在 Jenkins 中,我正在 运行ning Docker 中的前端 angular 服务,并且还想要 运行 Cypress 并连接到 运行ning angular服务器。

在 Angular Docker 文件中是 EXPOSE 8080,我无法更改,但 Cypress BaseURl 设置为:http://localhost:4200.

这就是我在构建前端时映射该端口的原因:

sh 'docker build -t myimage3 .'
sh 'docker create --name frontend --network my-net -p 4200:8080 myimage3'
sh 'docker start frontend'

前端启动并 运行正在容器中。

赛普拉斯我运行在同一个网络上:

sh 'docker build -t myimage4 .'
sh 'docker create --name test --network my-net myimage4'
sh 'docker start testrepo'

但由于某些原因,cypress 无法连接到本地主机的 4200 端口:

Cypress could not verify that this server is running:

  > http://localhost:4200

We are verifying this server because it has been configured as your `baseUrl`.

Cypress automatically waits until your server is accessible before running tests.

We will try connecting to it 3 more times...
We will try connecting to it 2 more times...
We will try connecting to it 1 more time...

Cypress failed to verify that your server is running.

cypress Docker文件是这样的:

FROM cypress/base

WORKDIR /test

COPY ./package*.json ./
RUN npm i
COPY ./cypress.json .

RUN npm run test

我试着把 EXPOSE 4200 放在最后一行之前,但没有成功。

似乎无法从另一个容器连接到 localhost:4200,即使网络相同。 不幸的是,我无法更改 Cypress 的 baseURL,因为我需要它作为 localhost:4200

如果他们在同一个网络上,那么 frontend:8080 应该适合你。

host.docker.internal:4200,因为不应在 docker 容器中使用本地主机。

参考:https://docs.docker.com/desktop/windows/networking/

本地主机(您的前端 angular)无法从 cypress docker 图像访问。

尝试将 localhost 替换为您要连接的 docker 图像的名称。在你的情况下 myimage3.