karma-runner / karma-chrome-launcher

A Karma plugin. Launcher for Chrome and Chrome Canary.
MIT License
467 stars 119 forks source link

How to launch --headless on Docker or wercker? #125

Closed Quramy closed 7 years ago

Quramy commented 7 years ago

Hi. I want to run ChromeHeadless on CI services.

I could not launch google-chrome --headless because the following error:

Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted

Is there some workarounds?

I tried to run in local env(macOS Chrome canary), and it works well.

andybry commented 7 years ago

Hi,

I was getting this issue as well. For me adding the --privileged flag when running docker run resolved the issue.

The complete command I ran when starting the Docker container was: docker run -it --privileged -v $(pwd):${MOUNT_POINT} ${DOCKER_IMAGE} bash

Hope that helps you too!

Quramy commented 7 years ago

I tried --privileged flag on my local Docker host and the error was removed. I don't know how to customise this flag on Wercker CI, so I'll search for information a little more.

Thanks!

hoeck commented 7 years ago

There is no need to start a privileged container, turning off the chrome sandbox with --no-sandbox is enough. Added the following to my karma.conf.js:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
        ChromeHeadlessNoSandbox: {
            base: 'ChromeHeadless',
            flags: [
                '--no-sandbox', // required to run without privileges in docker
                '--user-data-dir=/tmp/chrome-test-profile',
                '--disable-web-security'
            ]
        }
    },
andybry commented 7 years ago

Hi @hoeck

I agree that's a better solution if it works. However, it didn't work for me at the time I wrote the above comment (and I haven't retried it).

sam0104 commented 6 years ago

Where this file is located karma.conf.js ? I do not see any such file while made a find / -name "karma.conf.js" inside the container as root user.

fatihdestegul commented 4 years ago

There is no need to start a privileged container, turning off the chrome sandbox with --no-sandbox is enough. Added the following to my karma.conf.js:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
        ChromeHeadlessNoSandbox: {
            base: 'ChromeHeadless',
            flags: [
                '--no-sandbox', // required to run without privileges in docker
                '--user-data-dir=/tmp/chrome-test-profile',
                '--disable-web-security'
            ]
        }
    },

For continuous integration, it will be better setting them with a variable. So that you can run tests on your local with real browsers while you are testing it with headless in CI.

karma.conf.js

 const isBuild = process.env.CI;
  let settings = {
  ....
  ....
  ....
}
if (isBuild) {
    settings = {
      ...settings,
      browsers: ['ChromeHeadlessNoSandbox'],
      customLaunchers: {
        ChromeHeadlessNoSandbox: {
          base: 'ChromeHeadless',
          flags: ['--no-sandbox']
        }
      },
    };
  }

config.set(settings);
teacherpan commented 3 years ago

There is no need to start a privileged container, turning off the chrome sandbox with --no-sandbox is enough. Added the following to my karma.conf.js:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
        ChromeHeadlessNoSandbox: {
            base: 'ChromeHeadless',
            flags: [
                '--no-sandbox', // required to run without privileges in docker
                '--user-data-dir=/tmp/chrome-test-profile',
                '--disable-web-security'
            ]
        }
    },

worked for me , thank u.