我目前正在运行三个 docker 容器:
- 用于前端 Web 应用程序的 Docker 容器(暴露在端口 8080 上)
- 后端服务器的 Docker 容器(暴露在 5000 端口)
- 我的 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 post描述了第二个潜在问题,即 Docker 容器的/etc/hosts
文件不包含以下行。我也纠正了这个问题,这似乎不是问题。
127.0.0.1 localhost
有谁知道为什么我的赛普拉斯 Docker 容器似乎无法连接到我可以通过http://localhost:8080上的网络浏览器访问的网络应用程序?
下面是我的赛普拉斯容器的 Dockerfile
正如赛普拉斯关于 Docker 的文档所提到的,赛普拉斯/包含的图像已经有一个现有的入口点。由于我想在运行 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
下面是我的 package.json 文件中对应于npm run cypress-run-chrome
.
"cypress-run-chrome": "NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome",
下面是协调所有 4 个容器的 docker-compose.yml 文件。
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"
}