Docker 拒绝在我的 localhost:3000 端口上工作

Iam*_*ker 7 docker docker-compose cypress

这是我的 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
Run Code Online (Sandbox Code Playgroud)

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...
Run Code Online (Sandbox Code Playgroud)

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

Nic*_*sta 8

问题是容器不知道您的localhost主机名,因为它在隔离的 Docker 网络内运行。如果你想让你的容器知道你的本地网络,你必须使用主机网络,并且容器的网络堆栈不与 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
Run Code Online (Sandbox Code Playgroud)