【发布时间】:2020-06-14 08:35:36
【问题描述】:
我正在尝试在 Dockerfile 中使用 Docker 容器运行 Cypress 测试:
FROM cypress/included:4.8.0
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY cypress.json /usr/src/app/cypress.json
我用docker-compose up -d --build 旋转我所有的容器,它们运行得很好,但是当
我尝试运行基本的赛普拉斯测试:
docker run -it -v $PWD:/services/cypress -w /usr/src/app --entrypoint=cypress tdd_cypress run
我明白了:
Cypress could not verify that this server is running:
> http://nginx:80
We are verifying this server because it has been configured as your `baseUrl`.
Cypress automatically waits until your server is accessible before running tests.
We will try connecting to it 3 more times...
We will try connecting to it 2 more times...
We will try connecting to it 1 more time...
Cypress failed to verify that your server is running.
Please start this server and then run Cypress again.
这是我的 docker-compose 文件:
version: '3.7'
services:
users:
container_name: flask
build:
context: ./services/users
dockerfile: Dockerfile
volumes:
- './services/users:/usr/src/app'
ports:
- 5001:5000
environment:
- FLASK_ENV=development
- APP_SETTINGS=project.config.DevelopmentConfig
- DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev
- DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test
- SECRET_KEY=python_rocks
depends_on:
- users-db
users-db:
container_name: postgres
build:
context: ./services/users/project/db
dockerfile: Dockerfile
ports:
- 5436:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
client:
container_name: react
build:
context: ./services/client
dockerfile: Dockerfile
volumes:
- './services/client:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- 3007:3000
environment:
- NODE_ENV=development
- REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
depends_on:
- users
nginx:
container_name: nginx
build:
context: ./services/nginx
dockerfile: Dockerfile
restart: always
ports:
- 80:80
depends_on:
- users
- client
cypress:
container_name: cypress
build:
context: ./services/cypress
dockerfile: Dockerfile
depends_on:
- nginx
- users
- client
我发现了非常相似的主题here,解决方案与下面的解决方案几乎相同,但我的 Cypress 实例一直超时。
更新:
在收到其他人的反馈后,我在 Docker-Compose 中编辑了 Cypress 服务,使其看起来像这样:
cypress:
container_name: cypress
ipc: host
network_mode: host
build:
context: ./services/cypress
dockerfile: Dockerfile
并且baseUrl 设置为http://nginx:80 并且还尝试了http://client:3007。可悲的是,无论哪种方式,它都对错误没有影响。我已运行 docker-compose up -d --build 以确保更改生效。
赛普拉斯无法访问端口 80 上的 Nginx 有什么好的理由吗?任何反馈 非常感谢。
【问题讨论】:
-
每个容器都有自己的
localhost,所以赛普拉斯试图连接到自己。 Networking in Compose 描述了您的容器将看到的整体网络环境以及如何从一个容器连接到另一个容器。 -
@DavidMaze 好的,我很抱歉,我不够具体。我的意思是 Nginx 容器上的 localhost,因为它是路由所有流量的代理,它侦听端口 80。因此我需要 Cypress 容器才能访问 Nginx 上的端口 80。