use*_*888 7 testing docker dockerfile docker-compose cypress
我目前正在运行三个 docker 容器:
所有三个容器都运行良好,当我访问http://localhost:8080时,我可以毫无问题地与我的 Web 应用程序交互。
我正在尝试设置第四个 Cypress 容器,它将为我的应用程序运行端到端测试。不幸的是,当这个 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.
Run Code Online (Sandbox Code Playgroud)
第一个潜在问题(我已修复)
这篇SO 帖子描述了第一个潜在问题,即当 Cypress 启动时,我的应用程序尚未准备好开始响应请求。然而,在我的 Cypress Dockerfile 中,我当前在运行 cypress 命令之前休眠了 10 秒,如下所示。这 10 秒绰绰有余,因为我可以在npm run cypress-run-chrome命令执行之前从 Web 浏览器访问我的 Web 应用程序。我知道Cypress 文档有一些更奇特的解决方案用于等待http://localhost:8080,但现在,我确信我的应用程序已准备好让 Cypress 开始执行测试。
ENTRYPOINT sleep 10; npm run cypress-run-chrome
Run Code Online (Sandbox Code Playgroud)
第二个潜在问题(我已修复)
这篇SO post描述了第二个潜在问题,即 Docker 容器的/etc/hosts文件不包含以下行。我也纠正了这个问题,看来这不是问题。
127.0.0.1 localhost
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么我的 Cypress Docker 容器似乎无法连接到我可以从http://localhost:8080上的 Web 浏览器访问的 Web 应用程序?
以下是我的 Cypress 容器的 Dockerfile
正如有关 Docker 的 Cypress 文档中提到的,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
Run Code Online (Sandbox Code Playgroud)
以下是我的 package.json 文件中与npm run cypress-run-chrome.
"cypress-run-chrome": "NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome",
Run Code Online (Sandbox Code Playgroud)
下面是我的 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:
Run Code Online (Sandbox Code Playgroud)
下面是我的主机文件,它被复制到 Cypress Docker 容器中。
127.0.0.1 localhost
Run Code Online (Sandbox Code Playgroud)
下面是我的 cypress.json 文件的样子。
{
"baseUrl": "http://localhost:8080",
"integrationFolder": "cypress/integration",
"fileServerFolder": "dist",
"viewportWidth": 1200,
"viewportHeight": 1000,
"chromeWebSecurity": false,
"projectId": "3orb3g"
}
Run Code Online (Sandbox Code Playgroud)
localhost在 Docker 中始终是“这个容器”。使用 docker-compose.yml 中服务块的名称作为主机名,即http://web:8080
(请注意,我从评论中复制了大卫梅兹的答案)
| 归档时间: |
|
| 查看次数: |
14858 次 |
| 最近记录: |