繁体   English   中英

使用 Docker 和 Docker 时,赛普拉斯无法验证此服务器是否正在运行

[英]Cypress could not verify that this server is running when using Docker and Docker Compose

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

ENTRYPOINT sleep 10; npm run cypress-run-chrome

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

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

127.0.0.1 localhost

Does anyone know why my Cypress Docker container can't seem to connect to my web app that I can reach from my web browser on http://localhost:8080 ?

下面是我的赛普拉斯容器的 Dockerfile

正如赛普拉斯关于 Docker 的文档所述,赛普拉斯/包含的图像已经有一个现有的入口点。 由于我想在运行 package.json 文件中指定的赛普拉斯命令之前休眠 10 秒,因此我在 Z3254677A7917C6C01F55212F86C 中覆盖了 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:

下面是我的主机文件的样子,它被复制到赛普拉斯 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"
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM