Post/Code

HomeAboutUsesNow

Running Cypress in Docker.

Cypress is a fantastic tool for running end-to-end test on your applications. If you are running your app through Docker, you can set Cypress up for this too.

In your front-end client root directory, you can create a file named Dockerfile-cypress. This will contain:

FROM cypress/browsers:chrome65-ff57

# set working directory
WORKDIR /usr/src/app

ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install cypress
RUN npm install cypress

# copy cypress files and folders
COPY cypress /usr/src/app/cypress
COPY cypress.json /usr/src/app/cypress.json

# confirm the cypress install
RUN ./node_modules/.bin/cypress verify

Then, from your docker-compose.yml file, you can add a stage for Cypress that depends on your front-end stage, for example:

cypress:
    build:
      context: ./path/to/frontend
      dockerfile: Dockerfile-cypress
    depends_on:
      - name_of_frontend_service
    network_mode: "host"

From there, you can then run Cypress via docker-compose by:

docker-compose run cypress ./node_modules/.bin/cypress run