简体   繁体   English

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

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

I currently have three docker containers running:我目前正在运行三个 docker 容器:

  1. Docker container for the front-end web app (exposed on port 8080)前端 web 应用程序的 Docker 容器(暴露在端口 8080 上)
  2. Docker container for the back-end server (exposed on port 5000) Docker 用于后端服务器的容器(暴露在端口 5000 上)
  3. Docker container for my MongoDB database.我的 MongoDB 数据库的 Docker 容器。

All three containers are working perfectly and when I visit http://localhost:8080 , I can interact with my web application with no issues.所有三个容器都运行良好,当我访问http://localhost:8080时,我可以毫无问题地与我的 web 应用程序交互。

I'm trying to set up a fourth Cypress container that will run my end to end tests for my app.我正在尝试设置第四个赛普拉斯容器,该容器将为我的应用程序运行端到端测试。 Unfortunately, this Cypress container throws the below error, when it attempts to run my Cypress tests:不幸的是,当这个 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.

First potential issue (which I've fixed)第一个潜在问题(我已修复)

The first potential issue is described by this SO post , which is that when Cypress starts, my application is not ready to start responding to requests.这个SO post描述了第一个潜在问题,即当赛普拉斯启动时,我的应用程序还没有准备好开始响应请求。 However, in my Cypress Dockerfile, I'm currently sleeping for 10 seconds before I run my cypress command as shown below.但是,在我的赛普拉斯 Dockerfile 中,我目前正在休眠 10 秒钟,然后运行我的 cypress 命令,如下所示。 These 10 seconds are more than adequate since I'm able to access my web app from the web browser before the npm run cypress-run-chrome command executes.这 10 秒绰绰有余,因为我可以在npm run cypress-run-chrome命令执行之前从 web 浏览器访问我的 web 应用程序。 I understand that the Cypress documentation has some fancier solutions for waiting on http://localhost:8080 but for now, I know for sure that my app is ready for Cypress to start executing tests.我知道赛普拉斯文档有一些更高级的解决方案可以等待http://localhost:8080但现在,我确定我的应用程序已准备好让赛普拉斯开始执行测试。

ENTRYPOINT sleep 10; npm run cypress-run-chrome

Second potential issue (which I've fixed)第二个潜在问题(我已修复)

The second potential issue is described by this SO post , which is that the Docker container's /etc/hosts file does not contain the following line.SO 帖子描述了第二个潜在问题,即 Docker 容器的/etc/hosts文件不包含以下行。 I've also rectified that issue and it doesn't seem to be the problem.我也纠正了这个问题,这似乎不是问题。

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 ? 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 ?

Below is my Dockerfile for my Cypress container下面是我的赛普拉斯容器的 Dockerfile

As mentioned by the Cypress documentation about Docker , the cypress/included image already has an existing entrypoint.正如赛普拉斯关于 Docker 的文档所述,赛普拉斯/包含的图像已经有一个现有的入口点。 Since I want to sleep for 10 seconds before running my own Cypress command specified in my package.json file, I've overridden ENTRYPOINT in my Dockerfile as shown below.由于我想在运行 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

Below is the command within my package.json file that corresponds to 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",

Below is my docker-compose.yml file that coordinates all 4 containers.下面是我的 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:

Below is what my hosts file looks like which is copied into the Cypress Docker container.下面是我的主机文件的样子,它被复制到赛普拉斯 Docker 容器中。

127.0.0.1   localhost

Below is what my cypress.json file looks like.下面是我的 cypress.json 文件的样子。

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

localhost in Docker is always "this container". Docker 中的localhost始终是“此容器”。 Use the names of the service blocks in the docker-compose.yml as hostnames, ie, http://web:8080使用 docker-compose.yml 中的服务块名称作为主机名,即http://web:8080

(Note that I copied David Maze's answer from the comments) (请注意,我从评论中复制了 David Maze 的答案)

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

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