hackp0int
hackp0int

Reputation: 4161

Docker refuses to work on my localhost:3000 port

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.

Upvotes: 10

Views: 7840

Answers (3)

huytmb
huytmb

Reputation: 4274

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

Upvotes: 4

Greg K
Greg K

Reputation: 434

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

Upvotes: 1

Nicolas Acosta
Nicolas Acosta

Reputation: 807

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

Upvotes: 8

Related Questions