6

Here is my docker-compose.yml I'm trying to run cypress on my local project and it's refuses to run on the port. What I'm doing wrong?

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

Result of the 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...

I know for sure that my localhost:3000 is running, I'm able to run it via browser.

n0nvme
  • 663
  • 1
  • 12
IamStalker
  • 3,813
  • 8
  • 50
  • 88
  • Is the system under test running on the host-machine on port 3000? If so, then we need to provide the host's docker-internal ip (`docker-machine ip`) in `CYPRESS_baseUrl`, not `localhost`. – Turing85 Sep 03 '20 at 22:48
  • the docker is on my local machine and the project is also local – IamStalker Sep 03 '20 at 22:52
  • Again: If the system under test runs on the docker host, not in the same container as cypress, then we need to point `CYPRESS_baseUrl` to the docker host. – Turing85 Sep 03 '20 at 22:54
  • Correct, the test runs not in the container at all , cypress is a docker container. How do I define the CYPRESS_baseUrl for the cypress container? – IamStalker Sep 03 '20 at 22:56
  • In your compose-file. In line `- CYPRESS_baseUrl=http://localhost:3000`, change `localhost` to the host's docker-internal ip address. – Turing85 Sep 03 '20 at 23:02
  • @Turing85 I'm unable to get it's local IP – IamStalker Sep 03 '20 at 23:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220964/discussion-between-iamstalker-and-turing85). – IamStalker Sep 04 '20 at 09:43
  • I suggest that you replace localhost with own ip addresses does [this](https://stackoverflow.com/a/64343704/7192651) help you? – Yuqiu G. Oct 13 '20 at 22:05
  • https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container – Don't Panic Oct 14 '20 at 08:31
  • what server are you using? In order to access localhost, one needs a web server to run locally. Mapping the ports is HOST:CONTAINER and in strings "3333:3000" – Petronella Oct 20 '20 at 12:42

3 Answers3

4

The problem is that the container doesn't know you localhost hostname, because it is running inside an isolated docker network. If you want your container to know your local network, you have to Use host networking and the container's network stack is not isolated from the Docker host.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
Nicolas Acosta
  • 384
  • 1
  • 7
  • It worked for me. In my case, I was trying to download a react bundle from another container. Cypress' container wasn't able to see it because the request was being made to localhost, and not to the container name. This solved the issue – Jefferson Carvalho Nov 26 '20 at 12:38
2

baseUrl can be set in your configuration file (cypress.json by default) - and then you can set an environment variable in your OS to override it like shown below.

Try to using CYPRESS_BASE_URL instead of CYPRESS_baseUrl

And make sure that you using network_mode: "host" in docker-compose file.

Another way, you can define baseUrl in crypess.json and add volume to docker container:

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

Refer: 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
IamStalker
  • 3,813
  • 8
  • 50
  • 88
huytmb
  • 1,168
  • 1
  • 5
  • 12
  • thank you for the answer, I've set baseUrl inside cypress.json as http://localhost:3000 and inside the docker-compose: BASE_URL is http://host.docker.internal:3000 if I set it as localhost:3000 I get an error. If I run it inside cypress hub it works with a NOT DOCKER solution. How can I use only docker? – IamStalker Oct 20 '20 at 17:14
  • Make sure, you have to remove `ports: - 3333:3000` and use `network_mode: "host"` in crypress `docker-compose.yml` file – huytmb Oct 21 '20 at 01:53
  • I've added an example for you of how my `docker-compose.yml` looks like – IamStalker Oct 21 '20 at 22:14
1

With my setup, I use docker-compose to create a network, then my services use that network to talk to each other. I have a node container, and another cypress container.

networks:
  custom:
    driver: bridge

To run my cypress tests through docker, I have to point the cypress container to the node container, which runs the server. In my docker-compose, the node container is called node which will also be the hostname. So my docker-compose has a CYPRESS_BASE_URL environment variable, which overrides the default cypress baseUrl of http://localhost:3000

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/",
    ...
}

To run tests, I first exec into the node container to start the node server, which listens on http://localhost:3000, (http://node:3000)

docker exec -it node bash

then build and run your project (npm start)

then in a different terminal, exec into cypress container

docker exec -it cypress bash

then run tests:

cypress run
IamStalker
  • 3,813
  • 8
  • 50
  • 88
Greg K
  • 424
  • 4
  • 7
  • I don't have backend locally it's different API and by this configuration I'm still unable to recognise http://localhost:3000 from within the cypress container – IamStalker Oct 13 '20 at 14:45
  • localhost will never work with network mode as bridge. You'll need to set your network mode to `host` – Greg K Oct 15 '20 at 14:09
  • It's host, and still doesn't work. Can you please show me where I'm wrong? – IamStalker Oct 15 '20 at 20:00
  • What have you tried? Did you read this page? https://docs.docker.com/network/host/ – Greg K Oct 16 '20 at 14:20
  • YES, not helpful link, can you explain? – IamStalker Oct 17 '20 at 21:45
  • The configuration which you have provided doesn't work for me 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" ipc: host environment: - CYPRESS_baseUrl=http://host.docker.internal:3000 working_dir: /user-management-ui #command: "--browser chrome" network_mode: "host" ports: - 3333:3000 volumes: - ./:/user-management-ui – IamStalker Oct 17 '20 at 21:46