docker - 使用 Docker 和 Docker Compose 时, Cypress 无法验证此服务器是否正在运行

标签 docker testing docker-compose dockerfile cypress

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

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

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

我正在尝试设置第四个 Cypress 容器,它将运行我的应用程序的端到端测试。不幸的是,这个 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 命令执行之前从 Web 浏览器访问我的 Web 应用程序。我知道 Cypress documentation有一些更好的解决方案等待 http://localhost:8080但就目前而言,我确信我的应用已准备好让 Cypress 开始执行测试。

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 所述,柏树/包含的图像已经有一个现有的入口点。因为我想在运行 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:

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

最佳答案

Docker 中的

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

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

关于docker - 使用 Docker 和 Docker Compose 时, Cypress 无法验证此服务器是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709659/

相关文章:

testing - 通过 nosetests 测试 Flask 应用程序时,如何发送带有请求的 cookie?

mysql - Golang MySQL Docker 连接被拒绝

python-3.x - docker-compose找不到文件

docker - 您可以控制Dockerfile中的Docker运行参数吗

node.js - 在 docker 容器上运行 NOT headless chrome

docker - traefik - 同一主机的多个端口绑定(bind) V2

docker - 主机和docker容器的区别

grails - Grails 插件服务与 GORM 的集成测试应该有效吗?

docker - 在Docker中配置Traefik以路由子文件夹

delphi - 如何测试 TImage 是否分配了图形?