5

I am working on an Apple M1 Pro and running onto several issues with Docker and chromedriver.

So I want to run RSpec tests with selenium chrome headless on my Docker container. My Dockerfile is:

# Start from the official ruby image, then update and install JS & DB
FROM --platform=linux/amd64 ruby:2.6.6
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

# Install Chromedriver
RUN apt-get update && apt-get install -y unzip && \
    CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/ && \
    unzip ~/chromedriver_linux64.zip -d ~/ && \
    rm ~/chromedriver_linux64.zip && \
    chown root:root ~/chromedriver && \
    chmod 755 ~/chromedriver && \
    mv ~/chromedriver /usr/bin/chromedriver && \
    sh -c 'wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -' && \
    sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \
    apt-get update && apt-get install -y google-chrome-stable

# Create a directory for the application and use it
RUN mkdir /myapp
WORKDIR /myapp

# Gemfile and lock file need to be present, they'll be overwritten immediately
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

# Install gem dependencies
RUN gem install bundler:2.2.32
RUN bundle install

RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn
RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \
apt-get install nodejs

COPY . /myapp

# This script runs every time the container is created, necessary for rails
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start rails
CMD ["rails", "server", "-b", "0.0.0.0"]

My docker-compose.yml is:

version: "3"

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: yyy
      POSTGRES_PASSWORD: xxx
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      POSTGRES_USER: yyy
      POSTGRES_PASSWORD: xxx
      POSTGRES_HOST: db

My capybara.rb:

require 'capybara/rspec'
require 'selenium-webdriver'

Capybara.register_driver :selenium_chrome_headless do |app|
  Capybara::Selenium::Driver.new app,
    browser: :chrome,
    clear_session_storage: true,
    clear_local_storage: true,
    capabilities: [Selenium::WebDriver::Chrome::Options.new(
      args: %w[headless disable-gpu no-sandbox window-size=1024,768],
    )]
end

Capybara.javascript_driver = :selenium_chrome_headless

and Gemfile:

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara'
  gem 'launchy', '~> 2.4.3'
  gem 'selenium-webdriver'
  #To clean database for tests
  gem 'database_cleaner'
  # Easy installation and use of web drivers to run system tests with browsers
  # gem 'webdrivers'
end

This installs the chromedriver and google-chrome-stable in the /usr/bin directory in the container:

# which chromedriver
/usr/bin/chromedriver
# which google-chrome-stable
/usr/bin/google-chrome-stable

And their version matches (I read online that if it doesn't match it gives problems):

# google-chrome-stable --version
Google Chrome 98.0.4758.80 
# chromedriver --version
ChromeDriver 98.0.4758.80 (7f0488e8ba0d8e019187c6325a16c29d9b7f4989-refs/branch-heads/4758@{#972})

So it should be working. When I try to run the test, when the test is using js:true and I am in need of a headless chrome, this error happens:

Selenium::WebDriver::Error::UnknownError:
       unknown error: Chrome failed to start: crashed.
         (chrome not reachable)
         (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

I have been searching around but I found nothing that can help since I have the M1 chip and the answers I found didn't work.

NOTE:

Don't know if it is helpful but when I enter the container and try to run chromedriver it goes right but when running google-chrome-stable an error occurs:

# google-chrome-stable
[57:57:0210/020137.011691:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
# google-chrome-stable --no-sandbox
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
[129:129:0210/020150.185829:ERROR:nacl_fork_delegate_linux.cc(329)] Bad NaCl helper startup ack (0 bytes)
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
[93:137:0210/020150.317808:ERROR:file_path_watcher_linux.cc(321)] inotify_init() failed: Function not implemented (38)
[0210/020150.398311:ERROR:scoped_ptrace_attach.cc(27)] ptrace: Function not implemented (38)
qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped
Trace/breakpoint trap

I have been going around this for a long time. Do you have any idea how to solve? Thanks!

josegp
  • 499
  • 6
  • 21
  • For _**Chrome failed to start**_ See [this](https://stackoverflow.com/a/61733706/7429447) and [this](https://stackoverflow.com/a/53078276/7429447) discussion. For _**NaCl helper**_ Check [this](https://stackoverflow.com/a/60548787/7429447) discussion. – undetected Selenium Feb 10 '22 at 12:27
  • Thanks @undetectedSelenium , checked those and tried before but I am looking for a solution that works with M1 chip :( – josegp Feb 10 '22 at 12:45
  • Can you link me to some documentation of _M1 chip_ to figure out how that can be different? – undetected Selenium Feb 10 '22 at 12:47
  • 1
    Sure. This is a thread I read about: https://github.com/SeleniumHQ/docker-selenium/issues/1076#issuecomment-1001055386 @undetectedSelenium If you know anything about the matter please do tell me! – josegp Feb 10 '22 at 12:55
  • @josegp Did you get any lead on this? I'm also looking for the solution – Manish Sharma Jul 09 '22 at 17:57
  • Sadly I didn’t. It seems it is not supported for now @ManishSharma – josegp Jul 11 '22 at 21:41
  • 1
    @josegp I have find the solution. if you find it helpful. Please upvote. – Manish Sharma Jul 19 '22 at 10:33
  • I have the same problem and I did not install chromedriver in Dockerfile with apt. – maricavor Nov 14 '22 at 17:35

1 Answers1

3

For solving the chrome and chromedriver issue in mac m1 arm64:

You can use the following docker image seleniarm/standalone-chromium:latest for arm64 architecture to run your system test using the open source Chromium browser.

seleniarm/standalone-chromium:latest

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • This solution indeed working with M1 processor if it is running as a remote host in docker, but then I have issues with devise and sign_in. Devise cannot sign in user because of the session_store and different hosts. Anybody know how to make work devise session and chromium in docker? – maricavor Nov 14 '22 at 17:38
  • @maricavor Can you share the code how you have configured it? – Manish Sharma Nov 15 '22 at 05:47
  • ok, my standalone chromium running in docker, see [docker-compose file](https://github.com/maricavor/standalone-chromium/blob/main/docker-compose.yml). Capybara settings are [here](https://github.com/maricavor/standalone-chromium/blob/main/capybara.rb). Running for example this [test](https://github.com/maricavor/standalone-chromium/blob/main/invoices_test.rb) will end in 401 Unauthorized. – maricavor Nov 15 '22 at 12:32