61

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:

Jenkins official image:

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

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

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

Tom
  • 1,387
  • 3
  • 19
  • 30
Israel Perales
  • 2,192
  • 2
  • 25
  • 30
  • 1
    Would love to have this, especially now that `google-chrome-beta` (version 59) is out. I've tried getting it up and running and hit the same issues. – Ghazgkull May 09 '17 at 17:13
  • Have you tried to run it with all these flags? google-chrome-unstable --disable-gpu --headless --user-data-dir=/var/jenkins_home/chrome-data – Vova Rozhkov May 16 '17 at 11:55
  • This is how i run a C# app + Selenium in Docker: [Other question, same answer](https://stackoverflow.com/a/67912538/8529170) – Michael Santos Jun 09 '21 at 22:52

6 Answers6

11

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

zabumba
  • 12,172
  • 16
  • 72
  • 129
Boikot
  • 304
  • 2
  • 9
  • 6
    This can be a serious security issue. This [simple post](https://www.google.com/googlebooks/chrome/big_26.html) explains why. [This second one explains why in details](https://chromium.googlesource.com/chromium/src/+/master/docs/design/sandbox.md) – Cyril N. Nov 06 '18 at 13:28
  • 2
    Instead of `--no-sandbox`, you may be able to just use a custom `seccomp`. See [here](https://stackoverflow.com/a/53975412/970288). – usethe4ce Dec 30 '18 at 05:11
  • 2
    Just out of curiosity @CyrilN. ... if you are running Chrome inside a Docker instance, which is a sort of sandbox, wouldn't that make the `--no-sandbox` parameter useless? – JwJosefy Sep 17 '21 at 17:54
  • 2
    @JwJosefy Maybe, I'm not a Docker specialist. I think it would depends on what other things you run on your Docker instance. If it's just Chrome, then I'd go with yes, but with a grain of salt. – Cyril N. Sep 20 '21 at 11:46
  • That second article does NOT explain why this is a security issue, instead it just explains what the sandbox is and what it's purpose is. There is not enough context to imply this is fundamentally insecure. – Chris Stryczynski May 18 '23 at 23:29
10

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

Community
  • 1
  • 1
kongkoro
  • 792
  • 3
  • 7
4

This article is exactly what I needed to run Karma tests with Headless Chrome inside docker:

https://hackernoon.com/running-karma-tests-with-headless-chrome-inside-docker-ae4aceb06ed3

Basically, the solution is to run Headless Chrome with the --no-sandbox flag.

drussell
  • 509
  • 4
  • 5
  • This has already been said in [this answer](https://stackoverflow.com/a/45846909) – Adrian W Aug 18 '18 at 20:06
  • 4
    ... and it's a [huge security issue.](https://unix.stackexchange.com/questions/68832/what-does-the-chromium-option-no-sandbox-mean) – Cyril N. Nov 06 '18 at 13:29
3

We built a Docker image with Chrome and Chromedriver that runs Chrome in headless mode for automated tests. We're using this as a drop-in replacement for PhantomJS in our docker-compose.yml setups. The image is based on Alpine Linux and doesn't need or include Selenium so it's pretty small.

Source: https://github.com/retreatguru/headless-chromedriver

Docker Hub: https://hub.docker.com/r/retreatguru/headless-chromedriver

elifiner
  • 7,347
  • 8
  • 39
  • 48
2

I don't have the answer but I know a container which successfully launch a headless Chrome in Docker. The selenium one:

Selenium Chrome Node

I use it for automated testing of my webapp in Chrome

Hope it helps

Carlos Rafael Ramirez
  • 5,984
  • 1
  • 29
  • 36
  • 1
    which testing library do u use? I tried setting up `mocha` with `webdriver.io` but i always end up with `Error: A session id is required for this command but wasn't found in the response payload` when trying to get a value from the browser – Gobliins Aug 28 '17 at 15:09
1

I extend default Dockerfile Selenium Chrome Node by following

FROM selenium/standalone-chrome-debug:latest
MAINTAINER Serge Arbuzov <Serge.Arbuzov@advantechwireless.com>

USER root

### jenkins set up ###
RUN apt-get update && apt-get install -y openssh-server sudo
RUN mkdir /var/run/sshd
RUN adduser jenkins
RUN echo jenkins:jenkins | chpasswd
RUN echo "jenkins ALL=(ALL) NOPASSWD:ALL">>/etc/sudoers

USER root
RUN echo export DISPLAY=":1.5" >> /etc/environment
ADD run.sh /run.sh
RUN chmod +x /run.sh

EXPOSE 22

CMD ["/run.sh"]

And my run.sh is

#!/bin/bash

Xvfb :1 -screen 5 1024x768x8 &
/usr/sbin/sshd -D

So i can use default image as Jenkins node

whitediver
  • 462
  • 3
  • 12