当使用Docker和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描述了第一个潜在问题,即当赛普拉斯启动时,我的应用程序尚未准备好开始响应请求。但是,在我的Cypress Dockerfile中,我目前正在睡眠10秒钟,然后再运行cypress命令,如下所示。这10秒已足够,因为我可以在执行npm run cypress-run-chrome命令之前从Web浏览器访问Web应用程序。我知道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上的Web浏览器访问的Web应用程序吗?

以下是我的赛普拉斯容器的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"
}
markrolon 回答:当使用Docker和Docker Compose时,赛普拉斯无法验证此服务器是否正在运行

Docker中的

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

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

本文链接:https://www.f2er.com/3160326.html

大家都在问