【问题标题】:Docker refuses to work on my localhost:3000 portDocker 拒绝在我的 localhost:3000 端口上工作
【发布时间】:2020-09-03 22:35:34
【问题描述】:

这是我的 docker-compose.yml 我正在尝试在我的本地项目上运行 cypress,但它拒绝在端口上运行。 我做错了什么?

version: '3.2'
   
# run Cypress tests and exit with command
#   docker-compose up --exit-code-from cypress
services:
  cypress:
    # the Docker image to use from https://github.com/cypress-io/cypress-docker-images
    image: "cypress/included:5.0.0"
    environment:
      - CYPRESS_baseUrl=http://localhost:3000
    # share the current folder as volume to avoid copying
    working_dir: /e2e
     command: "--browser chrome"
     ports:
     - 3333:3000
     volumes:
       - ./:/e2e

compose-docker 的结果:

cypress_1  | 
cypress_1  | Cypress automatically waits until your server is accessible before running tests.
cypress_1  | 
cypress_1  | We will try connecting to it 3 more times...
cypress_1  | We will try connecting to it 2 more times...
cypress_1  | We will try connecting to it 1 more time...
cypress_1  | 
cypress_1  | Cypress failed to verify that your server is running.
cypress_1  | 
cypress_1  | Please start this server and then run Cypress again.
e2e_cypress_1 exited with code 1
Aborting on container exit...

我确定我的 localhost:3000 正在运行,我可以通过浏览器运行它。

【问题讨论】:

  • 被测系统是否在主机的 3000 端口上运行?如果是这样,那么我们需要在CYPRESS_baseUrl中提供主机的docker-internal ip(docker-machine ip),而不是localhost
  • docker在我的本地机器上,项目也在本地
  • 再次:如果被测系统运行在docker主机上,而不是和cypress在同一个容器中,那么我们需要将CYPRESS_baseUrl指向docker主机。
  • 正确,测试根本不在容器中运行,cypress 是一个 docker 容器。如何为 cypress 容器定义 CYPRESS_baseUrl?
  • 在您的撰写文件中。在- CYPRESS_baseUrl=http://localhost:3000 行中,将localhost 更改为主机的docker-internal ip 地址。

标签: docker docker-compose cypress


【解决方案1】:

问题是容器不知道你localhost 主机名,因为它在一个隔离的 docker 网络中运行。如果你想让你的容器知道你的本地网络,你必须Use host networking 并且容器的网络堆栈没有与Docker主机隔离。EG:

    version: '3.2'
    
    # run Cypress tests and exit with command
    #   docker-compose up --exit-code-from cypress
    services:
      cypress:
        # the Docker image to use from https://github.com/cypress-io/cypress-docker-images
        image: "cypress/included:5.0.0"
        environment:
          - CYPRESS_baseUrl=http://localhost:3000
        # share the current folder as volume to avoid copying
        working_dir: /e2e
        command: "--browser chrome"
        network_mode: "host"
        ports:
        - 3333:3000
        volumes:
          - ./:/e2e

【讨论】:

  • 它对我有用。就我而言,我试图从另一个容器下载一个反应包。赛普拉斯的容器无法看到它,因为请求是针对 localhost 而不是容器名称。这解决了问题
【解决方案2】:

baseUrl 可以在你的配置文件中设置(默认为 cypress.json)——然后你可以在你的操作系统中设置一个环境变量来覆盖它,如下所示。

尝试使用CYPRESS_BASE_URL 而不是CYPRESS_baseUrl

并确保在 docker-compose 文件中使用 network_mode: "host"

另一种方式,您可以在crypess.json 中定义baseUrl 并将volume 添加到docker 容器中:

e2e-chrome:
  image: "cypress/included:4.1.0"
  # container_name: cypress
  # "cypress/included" images have entrypoint set to globally installed cypress
  # so the command can simply add any arguments
  command: "--browser chrome"
  volumes:
    - ./cypress.json:/cypress.json

参考:docker-compose.yml

version: '3.2'

# run Cypress tests and exit with command
#   docker-compose up --exit-code-from cypress
services:
  cypress:
    image: "cypress/included:5.2.0"
    environment:
      - CYPRESS_BASE_URL=http://host.docker.internal:3000
    working_dir: /user-management-ui
    #command: "--browser chrome"
    network_mode: "host"
    volumes:
      - ./:/user-management-ui

【讨论】:

  • 感谢您的回答,我已将 cypress.json 中的 baseUrl 设置为 localhost:3000 并在 docker-compose 中设置:BASE_URL 为 host.docker.internal:3000 如果我将其设置为 localhost:3000 我得到一个错误。如果我在 cypress hub 内运行它,它可以与 NOT DOCKER 解决方案一起使用。我怎样才能只使用 docker?
  • 确保你必须删除ports: - 3333:3000并在crypress docker-compose.yml文件中使用network_mode: "host"
  • 我为您添加了一个示例,说明我的docker-compose.yml 的样子
【解决方案3】:

通过我的设置,我使用 docker-compose 创建一个网络,然后我的服务使用该网络相互通信。我有一个 node 容器和另一个 cypress 容器。

networks:
  custom:
    driver: bridge

要通过 docker 运行我的 cypress 测试,我必须将 cypress 容器指向运行服务器的节点容器。在我的 docker-compose 中,节点容器被称为 node,它也是主机名。所以我的docker-compose有一个CYPRESS_BASE_URL环境变量,它覆盖了http://localhost:3000的默认cypressbaseUrl

services:
  node:
    image: ...
    container_name: node
    volumes:
      - ./:/home/node/app
    networks:
      - custom
    ports:
      - '3000:3000'
    ...other config

  cypress:
    image: ...
    container_name: cypress
    volumes:
      - ./:/home/node/app
    networks:
      - custom
    working_dir: /home/node/app
    environment:
      - DISPLAY=
      - CYPRESS_BASE_URL=http://node:3000/ -> I can't configure this, because I'm working with external API, I don't have access to API only via http://localhost:3000

cypress.json:

{
    "baseUrl": "http://localhost:3000/",
    ...
}

为了运行测试,我首先 exec 进入节点容器来启动节点服务器,它监听 http://localhost:3000, (http://node:3000)

docker exec -it node bash

然后构建并运行您的项目 (npm start)

然后在不同的终端,执行到柏树容器中

docker exec -it cypress bash

然后运行测试:

cypress run

【讨论】:

  • 我在本地没有后端,它是不同的 API,通过这种配置,我仍然无法从 cypress 容器中识别 localhost:3000
  • localhost 永远不会使用网络模式作为网桥。您需要将网络模式设置为host
  • 它是主机,但仍然无法正常工作。你能告诉我哪里错了吗?
  • 你试过什么?你读过这个页面吗? docs.docker.com/network/host
  • 是的,没有帮助的链接,你能解释一下吗?
猜你喜欢
  • 2023-02-05
  • 2021-09-25
  • 1970-01-01
  • 2022-07-11
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 2019-07-31
相关资源
最近更新 更多