Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Docker和Docker Compose时,Cypress无法验证此服务器是否正在运行_Docker_Testing_Docker Compose_Dockerfile_Cypress - Fatal编程技术网

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

使用Docker和Docker Compose时,Cypress无法验证此服务器是否正在运行,docker,testing,docker-compose,dockerfile,cypress,Docker,Testing,Docker Compose,Dockerfile,Cypress,我目前有三个docker容器在运行: 前端web应用程序的Docker容器(暴露在端口8080上) 后端服务器的Docker容器(在端口5000上公开) 我的MongoDB数据库的Docker容器 这三个容器都工作得很好,当我访问时,我可以毫无问题地与我的web应用程序交互 我正在尝试设置第四个Cypress容器,它将为我的应用程序运行端到端测试。不幸的是,此Cypress容器在尝试运行my Cypress测试时抛出以下错误: cypress | Cypress could not verify

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

  • 前端web应用程序的Docker容器(暴露在端口8080上)
  • 后端服务器的Docker容器(在端口5000上公开)
  • 我的MongoDB数据库的Docker容器
  • 这三个容器都工作得很好,当我访问时,我可以毫无问题地与我的web应用程序交互

    我正在尝试设置第四个Cypress容器,它将为我的应用程序运行端到端测试。不幸的是,此Cypress容器在尝试运行my 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.
    
    第一个潜在问题(我已解决)

    第一个潜在问题是这样描述的,即当Cypress启动时,我的应用程序还没有准备好开始响应请求。但是,在我的Cypress Dockerfile中,在运行Cypress命令之前,我目前正在睡眠10秒钟,如下所示。这10秒钟已经足够了,因为在执行
    npm run-cypress-run-chrome
    命令之前,我可以从web浏览器访问我的web应用程序。我知道Cypress有一些更好的解决方案可以等待,但现在,我确信我的应用程序已经准备好让Cypress开始执行测试了

    ENTRYPOINT sleep 10; npm run cypress-run-chrome
    
    第二个潜在问题(我已解决)

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

    127.0.0.1 localhost
    
    有人知道为什么我的Cypress Docker容器似乎无法连接到我的web应用程序,而我可以通过我的web浏览器访问该应用程序吗?

    下面是我的Cypress容器的Dockerfile

    正如前面提到的,cypress/included映像已经有了一个现有的入口点。因为我想在运行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
    
    下面是my 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:
    
    127.0.0.1   localhost
    
    {
      "baseUrl": "http://localhost:8080",
      "integrationFolder": "cypress/integration",
      "fileServerFolder": "dist",
      "viewportWidth": 1200,
      "viewportHeight": 1000,
      "chromeWebSecurity": false,
      "projectId": "3orb3g"
    }
    
    下面是复制到Cypress Docker容器中的“我的主机”文件的外观。

    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:
    
    127.0.0.1   localhost
    
    {
      "baseUrl": "http://localhost:8080",
      "integrationFolder": "cypress/integration",
      "fileServerFolder": "dist",
      "viewportWidth": 1200,
      "viewportHeight": 1000,
      "chromeWebSecurity": false,
      "projectId": "3orb3g"
    }
    
    下面是我的cypress.json文件的样子。

    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:
    
    127.0.0.1   localhost
    
    {
      "baseUrl": "http://localhost:8080",
      "integrationFolder": "cypress/integration",
      "fileServerFolder": "dist",
      "viewportWidth": 1200,
      "viewportHeight": 1000,
      "chromeWebSecurity": false,
      "projectId": "3orb3g"
    }
    

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


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

    localhost
    在Docker中始终是“this container”。您可以使用
    docker compose.yml
    中服务块的名称作为主机名<代码>http://web:8080@DavidMaze是的,这绝对是个问题。如果你把它写成解决方案,我很乐意接受它作为答案。我想这个问题和解决方案会对像我这样的Docker新手有所帮助。我面临着同样的问题,我知道问题是什么,但解决方案是什么?docker-compose.yml文件中要编辑的内容,以便cypress的本地主机指向web的本地主机。@Chromeium->使用docker-compose.yml中服务块的名称作为主机名,即。,