How to run google chrome headless in docker?

My problem is how to run google chrome in docker container for e2e testing. I create a Dockerfile from official Jenkins image, but when try to run google chrome, it crashes and show the error:

Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
Trace/breakpoint trap (core dumped)

The Jenkins docker image uses Debian jessie.

I can run google chrome with --headless flag and the X server is not needed.

This is my docker file:

  • https://github.com/ripper2hl/jenkinswithchrome

Jenkins official image:

  • https://github.com/jenkinsci/docker

The repo a person runs google chrome with GUI from docker:

  • https://github.com/jessfraz/dockerfiles/blob/master/chrome/stable/Dockerfile

My first approach is to use xvbf, but the process is more simple when used --headless flag.

  • https://gist.github.com/addyosmani/5336747

I can run chrome in Ubuntu server with the same commands for installation, but in docker it fails.

After other intents, I used --no-sandbox flag, but docker images shows the next error.

[0427/180929.595479:WARNING:audio_manager.cc(295)] Multiple instances of AudioManager detected
[0427/180929.595537:WARNING:audio_manager.cc(254)] Multiple instances of AudioManager detected
libudev: udev_has_devtmpfs: name_to_handle_at on /dev: Operation not permitted

Actually I ran this command:

google-chrome-stable --headless --disable-gpu --no-sandbox http://www.google.com


Solution 1:

Just launch chrome with --no-sandbox that s resolves the problem

Solution 2:

Using this image alpeware/chrome-headless-trunk worked for me in ubuntu! The command used in that container to launch headless chrome is this:

/usr/bin/google-chrome-unstable \
--disable-gpu --headless --no-sandbox \
--remote-debugging-address=0.0.0.0 \
--remote-debugging-port=9222 --user-data-dir=/data

here's a short video of the container in action chrome headless in action

I launched the container in Ubuntu with this command:

 docker run -it --rm -p=0.0.0.0:9222:9222 \ 
 --name=chrome-headless \
 -v /tmp/chromedata/:/data alpeware/chrome-headless-trunk

then used Chrome to connect to the debug port at localhost:9222

With some modifications you could probably get this running in Jenkins!

Sources

  • https://hub.docker.com/r/alpeware/chrome-headless-trunk/
  • https://github.com/alpeware/chrome-headless-trunk