当使用Docker和Docker Compose时,赛普拉斯无法验证此服务器是否正在运行 [英] Cypress could not verify that this server is running when using Docker and Docker Compose

查看:241
本文介绍了当使用Docker和Docker Compose时,赛普拉斯无法验证此服务器是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 前端Web应用程序的Docker容器(端口8080公开)

  2. 后端服务器的Docker容器(暴露在端口5000上)

  3. 我的MongoDB数据库的Docker容器。

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



我正在尝试设置第四个赛普拉斯容器,该容器将始终运行测试我的应用。不幸的是,当此Cypress容器尝试运行我的Cypress测试时,抛出以下错误:

  cypress |赛普拉斯无法验证该服务器是否正在运行:
cypress |
柏树| > http:// localhost:8080
柏树|
柏树|我们正在验证此服务器,因为它已被配置为您的`baseUrl`。
柏树|
柏树|赛普拉斯会自动等待,直到您的服务器可以访问,然后再运行测试。
柏树|
柏树|我们将尝试再连接3次...
cypress |我们将尝试再连接2次...
cypress |我们将尝试再连接1次...
cypress |
柏树|赛普拉斯无法验证您的服务器正在运行。
柏树|
柏树|请启动该服务器,然后再次运行赛普拉斯。

第一个潜在问题(已解决)



第一个潜在问题是由 SO帖子描述的,即赛普拉斯启动,我的应用程序尚未准备好开始响应请求。但是,在我的Cypress Dockerfile中,我正在睡眠10秒钟,然后再运行cypress命令,如下所示。这10秒钟足够了,因为在执行 npm run cypress-run-chrome 命令之前,我可以通过网络浏览器访问我的Web应用程序。我了解赛普拉斯文档有一些内容等待 http:// localhost:8080 的理想解决方案,但就目前而言,我确定我的应用已准备就绪赛普拉斯开始执行测试。

  ENTRYPOINT sleep 10; npm run cypress-run-chrome 

第二个潜在问题(已解决)



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

  127.0.0.1 localhost 

有人知道为什么我的Cypress Docker容器似乎无法连接到我可以访问的Web应用程序吗从我的网络浏览器上 http:// localhost:8080



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



有关Docker的Cypress文档,该cypress / included图像已经存在有一个现有的入口点。由于在运行package.json文件中指定的自己的Cypress命令之前我想睡10秒钟,因此我已在Dockerfile中覆盖了ENTRYPOINT,如下所示。

  FROM cypress / included:3.4.1 

COPY主机/ etc /

WORKDIR / e2e

COPY软件包* .json ./

运行npm install --production

COPY。 。

ENTRYPOINT睡眠10; npm run cypress-run-chrome

下面是我package.json文件中与 npm run cypress-run-chrome

  cypress-run-chrome : NODE_ENV =测试$(npm bin)/ cypress run --config video = false-浏览器镶边,

以下是我的docker-compose.yml文件,该文件可协调所有4个容器。

 版本: 3 
服务:
网址:
构建:
上下文:。
dockerfile:./docker/web/Dockerfile
container_name:web
重新启动:除非停止
端口:
- 8080:8080
卷:
-。:/ home / node / app
-node_modules:/ home / node / app / node_modules
depends_on:
-服务器
环境:
-NODE_ENV =测试
网络:
-应用程序网络

db:
构建:
上下文:。
dockerfile:./docker/db/Dockerfile
container_name:db
重新启动:除非停止
卷:
-dbdata:/ data / db
端口:
- 27017:27017
网络:
-应用程序网络

服务器:
构建:
上下文:。
dockerfile:./docker/server/Dockerfile
container_name:服务器
重新启动:除非停止
端口:
- 5000:5000
卷:
-。:/ home / node / app
-node_modules:/ home / node / app / node_modules
网络:
-应用程序网络
取决于:
-db
命令:./wait-for.sh db:27017-nodemon -L server.js

柏树:
生成:
上下文:。
dockerfile:Dockerfile
container_name:柏树
重新启动:除非停止
卷:
-。:/ e2e
depend_on:
-web
网络:
-应用程序网络

网络:
应用程序网络:
驱动程序:桥接

数量:
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
}


解决方案

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



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


I currently have three docker containers running:

  1. Docker container for the front-end web app (exposed on port 8080)
  2. Docker container for the back-end server (exposed on port 5000)
  3. Docker container for my MongoDB database.

All three containers are working perfectly and when I visit http://localhost:8080, I can interact with my web application with no issues.

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 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. However, in my Cypress Dockerfile, I'm currently sleeping for 10 seconds before I run my cypress command as shown below. 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. 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.

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

Below is my Dockerfile for my Cypress container

As mentioned by the Cypress documentation about Docker, the cypress/included image already has an existing entrypoint. 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.

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.

"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.

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.

127.0.0.1   localhost

Below is what my cypress.json file looks like.

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

解决方案

localhost in Docker is always "this container". Use the names of the service blocks in the docker-compose.yml as hostnames, i.e., http://web:8080

(Note that I copied David Maze's answer from the comments)

这篇关于当使用Docker和Docker Compose时,赛普拉斯无法验证此服务器是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆