How to test web application running on localhost using Cypress running inside a Docker container
In the blog post "Run Cypress with a single Docker command" I gave several examples running Cypress Docker image cypress/included using docker-compose command. I also showed how to run Cypress inside a Docker container, yet see it on the host machine. But I did not give an example of how to run Cypress inside a Docker container and test a web application running on the host machine.
In this short blog post I will show how to do this. I am running Docker v18.09.2 on Mac.
So imagine I have a local web application running on localhost:3333. For example it could be a http-server serving a local public folder:
1
http-server -p 3333 public
I can load http://localhost:3333 in any browser and see the page.
My cypress.json file points at this URL
cypress.json
1 2 3
{ "baseUrl":"http://localhost:3333" }
Typically I would install Cypress locally, but maybe I don't want to install any tools. I can still run cypress/included Docker image, but any application running inside the container does NOT have access to the localhost of the host machine. Instead Docker provides a special hostname host.docker.internal for container processes to access the web apps running on the host machine, see this Stackoverflow question. When I start Cypress run, I need to set the base url config parameter, which I will do using an environment variable CYPRESS_baseUrl. Here is the full command with explanation below
Explanation of the "docker run" command line arguments:
-it = interactive terminal -v $PWD:/e2e = map current folder to /e2e inside the container -w /e2e = set working directy to /e2e -e CYPRESS_baseUrl=... = pass environment variable CYPRESS_baseUrl cypress/included:3.3.1 = name of the Docker image with tag