【问题标题】:Cypress can't verify localhost even though it is up and running赛普拉斯无法验证 localhost,即使它已启动并正在运行
【发布时间】:2020-06-14 08:35:36
【问题描述】:

我正在尝试在 Dockerfile 中使用 Docker 容器运行 Cypress 测试:

FROM cypress/included:4.8.0

WORKDIR /usr/src/app

ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY cypress.json /usr/src/app/cypress.json

我用docker-compose up -d --build 旋转我所有的容器,它们运行得很好,但是当 我尝试运行基本的赛普拉斯测试: docker run -it -v $PWD:/services/cypress -w /usr/src/app --entrypoint=cypress tdd_cypress run

我明白了:

Cypress could not verify that this server is running:

  > http://nginx:80

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.

Please start this server and then run Cypress again.

这是我的 docker-compose 文件:

version: '3.7'

services:

  users:
    container_name: flask
    build:
      context: ./services/users
      dockerfile: Dockerfile
    volumes:
      - './services/users:/usr/src/app'
    ports:
      - 5001:5000
    environment:
      - FLASK_ENV=development
      - APP_SETTINGS=project.config.DevelopmentConfig
      - DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev
      - DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test
      - SECRET_KEY=python_rocks
    depends_on:
      - users-db

  users-db:
    container_name: postgres
    build:
      context: ./services/users/project/db
      dockerfile: Dockerfile
    ports:
      - 5436:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

  client:
    container_name: react
    build:
      context: ./services/client
      dockerfile: Dockerfile
    volumes:
      - './services/client:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - 3007:3000
    environment:
      - NODE_ENV=development
      - REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
    depends_on:
      - users

  nginx:
    container_name: nginx
    build:
      context: ./services/nginx
      dockerfile: Dockerfile
    restart: always
    ports:
      - 80:80
    depends_on:
      - users
      - client

  cypress:
    container_name: cypress
    build:
      context: ./services/cypress
      dockerfile: Dockerfile
    depends_on:
      - nginx
      - users
      - client

我发现了非常相似的主题here,解决方案与下面的解决方案几乎相同,但我的 Cypress 实例一直超时。

更新:

在收到其他人的反馈后,我在 Docker-Compose 中编辑了 Cypress 服务,使其看起来像这样:

cypress:
    container_name: cypress
    ipc: host
    network_mode: host
    build:
      context: ./services/cypress
      dockerfile: Dockerfile

并且baseUrl 设置为http://nginx:80 并且还尝试了http://client:3007。可悲的是,无论哪种方式,它都对错误没有影响。我已运行 docker-compose up -d --build 以确保更改生效。

赛普拉斯无法访问端口 80 上的 Nginx 有什么好的理由吗?任何反馈 非常感谢。

【问题讨论】:

  • 每个容器都有自己的localhost,所以赛普拉斯试图连接到自己。 Networking in Compose 描述了您的容器将看到的整体网络环境以及如何从一个容器连接到另一个容器。
  • @DavidMaze 好的,我很抱歉,我不够具体。我的意思是 Nginx 容器上的 localhost,因为它是路由所有流量的代理,它侦听端口 80。因此我需要 Cypress 容器才能访问 Nginx 上的端口 80。

标签: docker cypress


【解决方案1】:

所以,最终我认为我的做法是解决方法。我没有请求赛普拉斯识别在它旁边运行的服务并在其上运行测试,而是采用了不同的方法。我知道 Nginx 容器在我的本地计算机上有一个分配的 IP,所以我做了什么我使用 ifconfig 查找它,在我的情况下它吐了出来:

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:9eff:fe60:ffea  prefixlen 64  scopeid 0x20<link>
        ether 02:42:9e:60:ff:ea  txqueuelen 0  (Ethernet)
        RX packets 95725  bytes 6812024 (6.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 126972  bytes 163560616 (163.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

然后我抓取了 IPv4 地址并将其放入 cypress.json 中,使其如下所示:

{
  "baseUrl": "http://172.17.0.1",
  "video": false
}

我不需要指定端口 80,因为如果您不指定它,默认情况下会选择它。在生产中,我做了同样的事情,但使用了生产服务器 IP 而不是开发服务器。

诚然,这不是最优雅的解决方案,但它确实有效,如果有人找到更好的解决方案,请分享。我很想听听和学习。

【讨论】:

    【解决方案2】:

    首先,这既不是 cypress 问题,也不是 docker 问题。 在 docker-compose 中使用时使用不正确。当您使用 docker-compose 启动本地环境时,如果您说 localhost,您的 docker 不知道哪个是 localhost,因为在其中运行了多个服务......如果您想与其他服务交谈,您必须提供这是服务名称。

    您必须提供应用程序的服务名称,即在您的情况下

    解决方案:http://nginx。参考下面的 compose sn-p。

    frontend-app:
        container_name: frontend-app
        build:
          context: ./services/frontend-repo
          dockerfile: Dockerfile
        restart: always
        ports:
          - 80:80
    
      cypress:
        container_name: cypress
        build:
          context: ./services/cypress
          dockerfile: Dockerfile
        depends_on:
          - frontend-app
    

    在这种情况下,你必须打开http://frontend-app

    【讨论】:

    • 感谢您的意见。不过,我也得到与您的解决方案相同的错误输出。
    • ... 例外的是现在它显示的是 http://nginx 而不是 http://localhost
    • 你能不能试试这个http://nginx:80(或)http://client:3007,让我知道它是否有效。请不要忘记在 docker-compose 文件中的 cypress 服务中添加 ipc: hostnetwork_mode: host
    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2023-04-04
    • 2023-02-10
    • 2020-12-23
    相关资源
    最近更新 更多