headless chrome in docker with python. Chrome failed to start: crashed

I want to run this simple script inside a docker container:

def hi_chrome():
    from xvfbwrapper import Xvfb
    from splinter import Browser
    vdisplay = Xvfb()
    vdisplay.start()

    print "spawning connector"
    oBrowser = Browser('chrome')
    oBrowser.visit("http://google.co.za")
    assert oBrowser.title == "Google"
    print "yay"
    vdisplay.stop()

if __name__ == '__main__':
    hi_chrome()

I’ve gotten the script to run in a virtual environment by doing all the pip and apt-get installs listed in the my docker file and just running the script. But when I try run it inside a container I get:

  • File mounted as docker volume becomes corrupt when saved outside docker
  • Use a single VM with Vagrant to host Docker Containers across multiple projects
  • How can I add a volume to an existing Docker container?
  • Docker 'WARNING: permission denied' on ubuntu
  • When to use auto mapped Docker data volume
  • How to reduce memory consumption of docker containers?
  • Traceback (most recent call last):
      File "app.py", line 19, in <module>
        hi_chrome()
      File "app.py", line 10, in hi_chrome
        oBrowser = Browser('chrome')
      File "/usr/local/lib/python2.7/dist-packages/splinter/browser.py", line 63, in Browser
        return driver(*args, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/splinter/driver/webdriver/chrome.py", line 31, in __init__
        self.driver = Chrome(chrome_options=options, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
        desired_capabilities=desired_capabilities)
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
        self.start_session(desired_capabilities, browser_profile)
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
        response = self.execute(Command.NEW_SESSION, capabilities)
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
        self.error_handler.check_response(response)
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
      (Driver info: chromedriver=2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.8.0-34-generic x86_64)
    

    I’ve had similar problems when trying to run my script using other containers on docker-hub. I’ve tried using chrome instead of chromium and I’ve tried using some containers I found on docker-hub but I keep finding broken nonesense. This should be simple.

    My main suspicion is that it’s a versioning thing. But it works in the venv so that doesnt make too much sense. Or docker just needs something fancy to get the chrome webdriver to run.

    Can someone please point out my obvious and noobish mistake?

    My Dockerfile looks like

    FROM ubuntu:16.04
    
    RUN apt-get update -y && \
        apt-get install -y python-pip python-dev xvfb chromium-browser && \
        pip install --upgrade pip setuptools
    
    RUN pip install chromedriver_installer
    
    COPY ./requirements.txt /app/requirements.txt
    
    WORKDIR /app
    
    RUN pip install -r requirements.txt
    
    COPY . /app
    
    ENTRYPOINT [ "python" ]
    
    CMD [ "app.py" ]
    

    And requirements.txt:

    splinter==0.7.5
    xvfbwrapper==0.2.8
    

  • Docker-compose rails postgres
  • Graylog SMTP server without TLS/SSL
  • Setup Openstack Havana with Docker driver
  • How can I save my RSA KEY in my Docker Machine?
  • Vagrant would not provision docker
  • Using Ansible,Jenkins and docker to build fast test environments
  • One Solution collect form web for “headless chrome in docker with python. Chrome failed to start: crashed”

    I found an image that sorta worked and then beat it into submission… Nice thing about this solution is it doesn’t need xvfbwrapper so it’s nice and simple.

    App.py

    def hi_chrome():
        # from xvfbwrapper import Xvfb
        from splinter import Browser
        # vdisplay = Xvfb()
        # vdisplay.start()
    
        print "spawning connector"
        oBrowser = Browser('chrome')
        oBrowser.visit("http://google.co.za")
        assert oBrowser.title == "Google"
        print "yay"
    
        # vdisplay.stop()
    
    if __name__ == '__main__':
        hi_chrome()
    

    requirements:

     splinter==0.7.5
    

    Dockerfile

    FROM markadams/chromium-xvfb
    
    RUN apt-get update && apt-get install -y \
        python python-pip curl unzip libgconf-2-4
    
    ENV CHROMEDRIVER_VERSION 2.26
    
    RUN curl -SLO "https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" \
      && unzip "chromedriver_linux64.zip" -d /usr/local/bin \
      && rm "chromedriver_linux64.zip"
    
    COPY requirements.txt /usr/src/app/requirements.txt
    WORKDIR /usr/src/app
    
    RUN pip install -r requirements.txt
    
    COPY . /usr/src/app
    
    ENTRYPOINT [ "python" ]
    
    CMD [ "app.py" ]
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.