Docker refuses to work on my localhost:3000 port
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
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 cypressservices: 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
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