【问题标题】:Cypress could not verify that this server is running when using Docker and Docker Compose赛普拉斯在使用 Docker 和 Docker Compose 时无法验证此服务器是否正在运行
【发布时间】:2019-11-05 10:43:45
【问题描述】:

我目前正在运行三个 docker 容器:

  1. 前端 Web 应用程序的 Docker 容器(暴露在 8080 端口上)
  2. 后端服务器的 Docker 容器(暴露在 5000 端口上)
  3. 我的 MongoDB 数据库的 Docker 容器。

所有三个容器都运行良好,当我访问 http://localhost:8080 时,我可以毫无问题地与我的 Web 应用程序交互。

我正在尝试设置第四个赛普拉斯容器,该容器将为我的应用运行端到端测试。不幸的是,这个 Cypress 容器在尝试运行我的 Cypress 测试时会引发以下错误:

cypress | Cypress could not verify that this server is running:
cypress |
cypress |   > http://localhost:8080
cypress |
cypress | We are verifying this server because it has been configured as your `baseUrl`.
cypress |
cypress | Cypress automatically waits until your server is accessible before running tests.
cypress |
cypress | We will try connecting to it 3 more times...
cypress | We will try connecting to it 2 more times...
cypress | We will try connecting to it 1 more time...
cypress |
cypress | Cypress failed to verify that your server is running.
cypress |
cypress | Please start this server and then run Cypress again.

第一个潜在问题(我已修复)

SO post 描述了第一个潜在问题,即当 Cypress 启动时,我的应用程序尚未准备好开始响应请求。但是,在我的 Cypress Dockerfile 中,我目前正在休眠 10 秒,然后运行我的 cypress 命令,如下所示。这 10 秒绰绰有余,因为我可以在 npm run cypress-run-chrome 命令执行之前从网络浏览器访问我的网络应用程序。我知道Cypress documentation 有一些更好的解决方案可以等待http://localhost:8080,但现在,我确定我的应用已准备好让赛普拉斯开始执行测试。

ENTRYPOINT sleep 10; npm run cypress-run-chrome

第二个潜在问题(我已修复)

SO post 描述了第二个潜在问题,即Docker 容器的/etc/hosts 文件不包含以下行。我也纠正了这个问题,似乎不是问题。

127.0.0.1 localhost

有谁知道为什么我的 Cypress Docker 容器似乎无法连接到我可以通过网络浏览器访问 http://localhost:8080 的网络应用程序?

下面是我的 Cypress 容器的 Dockerfile

正如Cypress documentation about Docker 所述,cypress/included 映像已经有一个现有的入口点。由于我想在运行 package.json 文件中指定的 Cypress 命令之前休眠 10 秒,因此我在 Dockerfile 中覆盖了 ENTRYPOINT,如下所示。

FROM cypress/included:3.4.1

COPY hosts /etc/

WORKDIR /e2e

COPY package*.json ./

RUN npm install --production

COPY . .

ENTRYPOINT sleep 10; npm run cypress-run-chrome

下面是我的 package.json 文件中对应于npm run cypress-run-chrome 的命令。

"cypress-run-chrome": "NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome",

下面是我的 docker-compose.yml 文件,它协调所有 4 个容器。

version: '3'
services:
    web:
        build:
            context: .
            dockerfile: ./docker/web/Dockerfile
        container_name: web
        restart: unless-stopped
        ports:
            - "8080:8080"
        volumes:
            - .:/home/node/app
            - node_modules:/home/node/app/node_modules
        depends_on:
            - server
        environment:
            - NODE_ENV=testing
        networks:
            - app-network

    db:
        build:
            context: .
            dockerfile: ./docker/db/Dockerfile
        container_name: db
        restart: unless-stopped
        volumes:     
            - dbdata:/data/db
        ports:
            - "27017:27017"
        networks:
            - app-network

    server:
        build:
            context: .
            dockerfile: ./docker/server/Dockerfile
        container_name: server
        restart: unless-stopped
        ports:
            - "5000:5000"
        volumes:
            - .:/home/node/app
            - node_modules:/home/node/app/node_modules
        networks:
            - app-network
        depends_on:
            - db
        command: ./wait-for.sh db:27017 -- nodemon -L server.js

    cypress:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: cypress
        restart: unless-stopped
        volumes:
            - .:/e2e
        depends_on:
            - web
        networks:
            - app-network

networks:
    app-network:
        driver: bridge

volumes:
    dbdata:
    node_modules:

下面是我的 hosts 文件的样子,它被复制到 Cypress Docker 容器中。

127.0.0.1   localhost

下面是我的 cypress.json 文件的样子。

{
  "baseUrl": "http://localhost:8080",
  "integrationFolder": "cypress/integration",
  "fileServerFolder": "dist",
  "viewportWidth": 1200,
  "viewportHeight": 1000,
  "chromeWebSecurity": false,
  "projectId": "3orb3g"
}

【问题讨论】:

  • localhost 在 Docker 中始终是“这个容器”。您可以使用docker-compose.yml 中的服务块名称作为主机名; http://web:8080.
  • @DavidMaze 是的,这绝对是问题所在。如果您将其写为解决方案,我很乐意接受它作为答案。我想这个问题和解决方案会对像我这样的其他 Docker 新手有所帮助。

标签: docker testing docker-compose dockerfile cypress


【解决方案1】:

localhost 在 Docker 中始终是“这个容器”。使用 docker-compose.yml 中服务块的名称作为主机名,即http://web:8080

(请注意,我从 cmets 复制了 David Maze 的答案)

【讨论】:

  • 我也面临同样的问题,我明白问题出在哪里,但解决办法是什么?在 docker-compose.yml 文件中编辑什么,使 cypress 的本地主机指向 web 的 localhost。
  • @Chromeium -> 使用 docker-compose.yml 中服务块的名称作为主机名,即web:8080
  • 柏树容器:环境:(换行符)-CYPRESS_baseUrl=web
猜你喜欢
  • 2022-11-10
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多