Solveddocker selenium Headless chrome is not working in the docker

Image:

$ docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
selenium/standalone-chrome                      latest              1dec27211cb6        2 weeks ago         835MB

Container:

853711d44c1e        selenium/standalone-chrome   "/opt/bin/entry_po..."   18 minutes ago      Up 18 
minutes              0.0.0.0:4444->4444/tcp   hopeful_shannon
# This does NOT work
URL = 'http://localhost:4444/wd/hub'
options = webdriver.ChromeOptions()
options.add_argument('headless')
# for remote, options need to be converted to capabilities
driver = webdriver.Remote(command_executor=URL,
                          desired_capabilities=options.to_capabilities())

# this works (but is not using docker)
webdriver.Chrome(options=options)

Stack trace of not working (it times out):

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-62-f265e82ae069> in <module>()
      4 # for remote, options need to be converted to capabilities
      5 driver = webdriver.Remote(command_executor=URL,
----> 6                           desired_capabilities=options.to_capabilities())

/home/pascal/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector)
     96             warnings.warn("Please use FirefoxOptions to set browser profile",
     97                           DeprecationWarning)
---> 98         self.start_session(desired_capabilities, browser_profile)
     99         self._switch_to = SwitchTo(self)
    100         self._mobile = Mobile(self)

/home/pascal/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
    186         parameters = {"capabilities": w3c_caps,
    187                       "desiredCapabilities": capabilities}
--> 188         response = self.execute(Command.NEW_SESSION, parameters)
    189         if 'sessionId' not in response:
    190             response = response['value']

/home/pascal/python/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    254         response = self.command_executor.execute(driver_command, params)
    255         if response:
--> 256             self.error_handler.check_response(response)
    257             response['value'] = self._unwrap_value(
    258                 response.get('value', None))

/home/pascal/python/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    192         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    193             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 194         raise exception_class(message, screen, stacktrace)
    195 
    196     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: Chrome failed to start: exited normally
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.11.9-1-ARCH x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.05 seconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'c3e9aafe9593', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.11.9-1-ARCH', java.version: '1.8.0_131'
Driver info: driver.version: ChromeDriver
Stacktrace:
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (NativeConstructorAccessorImpl.java:-2)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance (Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable (ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed (ErrorHandler.java:167)
    at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$new$0 (JsonWireProtocolResponse.java:53)
    at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$getResponseFunction$2 (JsonWireProtocolResponse.java:91)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$22 (ProtocolHandshake.java:365)
    at java.util.stream.ReferencePipeline$3$1.accept (ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.tryAdvance (Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel (ReferencePipeline.java:126)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel (AbstractPipeline.java:498)
    at java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:485)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:471)
    at java.util.stream.FindOps$FindOp.evaluateSequential (FindOps.java:152)
    at java.util.stream.AbstractPipeline.evaluate (AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst (ReferencePipeline.java:464)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession (ProtocolHandshake.java:368)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession (ProtocolHandshake.java:159)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute (HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute (DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute (RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession (RemoteWebDriver.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession (RemoteWebDriver.java:236)
    at org.openqa.selenium.remote.RemoteWebDriver.<init> (RemoteWebDriver.java:137)
    at org.openqa.selenium.chrome.ChromeDriver.<init> (ChromeDriver.java:184)
    at org.openqa.selenium.chrome.ChromeDriver.<init> (ChromeDriver.java:148)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (NativeConstructorAccessorImpl.java:-2)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance (Constructor.java:423)
    at org.openqa.selenium.remote.server.DefaultDriverProvider.callConstructor (DefaultDriverProvider.java:102)
    at org.openqa.selenium.remote.server.DefaultDriverProvider.newInstance (DefaultDriverProvider.java:96)
    at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance (DefaultDriverFactory.java:60)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call (DefaultSession.java:222)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call (DefaultSession.java:209)
    at java.util.concurrent.FutureTask.run (FutureTask.java:266)
    at org.openqa.selenium.remote.server.DefaultSession$1.run (DefaultSession.java:176)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:617)
    at java.lang.Thread.run (Thread.java:748)
31 Answers

✔️Accepted Answer

Another attempt:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
URL = 'http://localhost:4444/wd/hub'
options.add_argument('--headless')
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--remote-debugin-port=9222")
options.add_argument("--screen-size=1200x800")

driver = webdriver.Remote(command_executor=URL,
                          desired_capabilities=options.to_capabilities())

Other Answers:

run into same issue in my self build docker (debian 9 based), adding "no-sandbox" works. "privileged" or "disable-gpu" is not necessary.

run as normal user (not root).

        options = webdriver.ChromeOptions()
        options.add_argument("headless")
        #options.add_argument("privileged")
        options.add_argument("no-sandbox")
        #options.add_argument("disable-gpu")
        options.add_argument("window-size=1200x800")
        self.browser = webdriver.Chrome(chrome_options=options)

@diemol: I've updated my images with selenium/standalone-chrome-debug:3.6.0-bromine and ran again the commands.
They worked fine.

Here's the result:

seluser@b9e5ef2a74bd:/$ google-chrome --headless --disable-gpu
seluser@b9e5ef2a74bd:/$ google-chrome --headless --disable-gpu --dump-dom http://www.google.com
<body class="hp vasq" onload="document.f&amp;&amp;document.f.q.focus();
...

Thank you very much!

More Issues: