'How to run Karma tests from docker container?

I've recently moved my node.js app into a docker image and I'd like to run my tests inside the image. My mocha/node tests work fine but the Karma tests involve starting Chrome to run the tests and Chrome isn't installed in the container.

How do I go about addressing this?

  • Install Chrome in the Container? Seems less than ideal as I don't want to ship Chrome to my production servers inside the container.
  • Somehow allow it to connect to Chrome on the host?
  • Create a new image that inherits from my app image and adds Chrome and other things?

Googling 'docker & karma' reveals docker images out there but I can't find instructions on how to think about the problem and the best approach.



Solution 1:[1]

I've found this docker image to be an excellent starting point for running karma tests quickly inside a docker container on Concourse CI:

https://hub.docker.com/r/markadams/chromium-xvfb-js/

It contains node 6.x (latest) + npm and a headless chromium instance using X virtual framebuffer. Working great for me!

Solution 2:[2]

There is also protractor-runner project, and Karma is similar to running Protractor, and so you might be able to learn how to do it from that example. But, I prefer the suggestion to use chromium-xvfb-js image.

Solution 3:[3]

You can try this (tested in Alpine Linux):

Option 1

  1. Ensure that the 9876 port is accessible from the host.

  2. Install chromium:

     apk add chromium # NOT google-chrome-stable
    
  3. Create a wrapper script that starts chromium-browser in headless mode:

     vi /usr/bin/google-chrome-stable
    

    Add the following lines:

     #!/bin/sh
    
     chromium-browser \
     --no-sandbox \
     --headless \
     --disable-gpu \
     --remote-debugging-port=9222 \
     "$@"
    
  4. Make it executable:

     chmod +x /usr/bin/google-chrome-stable
    
  5. Run the tests in headless mode:

     ng test --browsers ChromeHeadless
    

Option 2

You can also run the browser in a virtual X Server called Xvfb:

  1. Remove the --headless option in the /usr/bin/google-chrome-stable script.

  2. Start the server and export the DISPLAY vairable:

     Xvfb :1 -ac -screen 0 1024x768x24 &
     export DISPLAY=:1
    
  3. Run the tests:

     ng test
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Johannes Rudolph
Solution 2 djangofan
Solution 3