簡體   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