title: Cypress could not verify that this server is running when using Docker and Docker Compose
body: <p>I currently have three docker containers running:</p>
  
  <ol>
  <li>Docker container for the front-end web app (exposed on port 8080)</li>
  <li>Docker container for the back-end server (exposed on port 5000)</li>
  <li>Docker container for my MongoDB database.</li>
  </ol>
  
  <p>All three containers are working perfectly and when I visit <a href="http://localhost:8080" rel="noreferrer">http://localhost:8080</a>, I can interact with my web application with no issues.</p>
  
  <p>I'm trying to set up a fourth Cypress container that will run my end to end tests for my app. Unfortunately, this Cypress container throws the below error, when it attempts to run my Cypress tests:</p>
  
  <pre><code>cypress | Cypress could not verify that this server is running:
  cypress |
  cypress |   &gt; http://localhost:8080
  cypress |
  cypress | We are verifying this server because it has been configured as your `baseUrl`.
  cypress |
  cypress | Cypress automatically waits until your server is accessible before running tests.
  cypress |
  cypress | We will try connecting to it 3 more times...
  cypress | We will try connecting to it 2 more times...
  cypress | We will try connecting to it 1 more time...
  cypress |
  cypress | Cypress failed to verify that your server is running.
  cypress |
  cypress | Please start this server and then run Cypress again.
  </code></pre>
  
  <p><strong>First potential issue (which I've fixed)</strong></p>
  
  <p>The first potential issue is described by this <a href="https://stackoverflow.com/a/54934920/7489488">SO post</a>, which is that when Cypress starts, my application is not ready to start responding to requests. However, in my Cypress Dockerfile, I'm currently sleeping for 10 seconds before I run my cypress command as shown below. These 10 seconds are more than adequate since I'm able to access my web app from the web browser before the <code>npm run cypress-run-chrome</code> command executes. I understand that the <a href="https://docs.cypress.io/guides/guides/continuous-integration.html#Boot-your-server" rel="noreferrer">Cypress documentation</a> has some fancier solutions for waiting on <a href="http://localhost:8080" rel="noreferrer">http://localhost:8080</a> but for now, I know for sure that my app is ready for Cypress to start executing tests.</p>
  
  <pre><code>ENTRYPOINT sleep 10; npm run cypress-run-chrome
  </code></pre>
  
  <p><strong>Second potential issue (which I've fixed)</strong></p>
  
  <p>The second potential issue is described by this <a href="https://stackoverflow.com/a/56812435/7489488">SO post</a>, which is that the Docker container's <code>/etc/hosts</code> file does not contain the following line. I've also rectified that issue and it doesn't seem to be the problem.</p>
  
  <pre><code>127.0.0.1 localhost
  </code></pre>
  
  <p><strong>Does anyone know why my Cypress Docker container can't seem to connect to my web app that I can reach from my web browser on <a href="http://localhost:8080" rel="noreferrer">http://localhost:8080</a>?</strong></p>
  
  <p><strong>Below is my Dockerfile for my Cypress container</strong></p>
  
  <p>As mentioned by the <a href="https://www.cypress.io/blog/2019/05/02/run-cypress-with-a-single-docker-command/" rel="noreferrer">Cypress documentation about Docker</a>, the cypress/included image already has an existing entrypoint. Since I want to sleep for 10 seconds before running my own Cypress command specified in my package.json file, I've overridden ENTRYPOINT in my Dockerfile as shown below.</p>
  
  <pre><code>FROM cypress/included:3.4.1
  
  COPY hosts /etc/
  
  WORKDIR /e2e
  
  COPY package*.json ./
  
  RUN npm install --production
  
  COPY . .
  
  ENTRYPOINT sleep 10; npm run cypress-run-chrome
  </code></pre>
  
  <p>Below is the command within my package.json file that corresponds to <code>npm run cypress-run-chrome</code>.</p>
  
  <pre><code>"cypress-run-chrome": "NODE_ENV=test $(npm bin)/cypress run --config video=false --browser chrome",
  </code></pre>
  
  <p><strong>Below is my docker-compose.yml file that coordinates all 4 containers.</strong></p>
  
  <pre><code>version: '3'
  services:
      web:
          build:
              context: .
              dockerfile: ./docker/web/Dockerfile
          container_name: web
          restart: unless-stopped
          ports:
              - "8080:8080"
          volumes:
              - .:/home/node/app
              - node_modules:/home/node/app/node_modules
          depends_on:
              - server
          environment:
              - NODE_ENV=testing
          networks:
              - app-network
  
      db:
          build:
              context: .
              dockerfile: ./docker/db/Dockerfile
          container_name: db
          restart: unless-stopped
          volumes:     
              - dbdata:/data/db
          ports:
              - "27017:27017"
          networks:
              - app-network
  
      server:
          build:
              context: .
              dockerfile: ./docker/server/Dockerfile
          container_name: server
          restart: unless-stopped
          ports:
              - "5000:5000"
          volumes:
              - .:/home/node/app
              - node_modules:/home/node/app/node_modules
          networks:
              - app-network
          depends_on:
              - db
          command: ./wait-for.sh db:27017 -- nodemon -L server.js
  
      cypress:
          build:
              context: .
              dockerfile: Dockerfile
          container_name: cypress
          restart: unless-stopped
          volumes:
              - .:/e2e
          depends_on:
              - web
          networks:
              - app-network
  
  networks:
      app-network:
          driver: bridge
  
  volumes:
      dbdata:
      node_modules:
  </code></pre>
  
  <p><strong>Below is what my hosts file looks like which is copied into the Cypress Docker container.</strong></p>
  
  <pre><code>127.0.0.1   localhost
  </code></pre>
  
  <p><strong>Below is what my cypress.json file looks like.</strong></p>
  
  <pre><code>{
    "baseUrl": "http://localhost:8080",
    "integrationFolder": "cypress/integration",
    "fileServerFolder": "dist",
    "viewportWidth": 1200,
    "viewportHeight": 1000,
    "chromeWebSecurity": false,
    "projectId": "3orb3g"
  }
  </code></pre>