Search code examples
ruby-on-railsdockerseleniumcapybararspec-rails

Cannot use selenium_chrome_headless on docker container Apple M1 Chip


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!


Solution

  • 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