无法在 docker 容器 Apple M1 芯片上使用 selenium_chrome_headless

jos*_*egp 5 selenium ruby-on-rails capybara rspec-rails docker

我正在开发 Apple M1 Pro,并遇到了 Docker 和 chromedriver 的几个问题。

\n

所以我想在我的 Docker 容器上使用 selenium chrome headless 运行 RSpec 测试。我的Dockerfile是:

\n
# Start from the official ruby image, then update and install JS & DB\nFROM --platform=linux/amd64 ruby:2.6.6\nRUN apt-get update -qq && apt-get install -y nodejs postgresql-client\n\n# Install Chromedriver\nRUN apt-get update && apt-get install -y unzip && \\\n    CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \\\n    wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/ && \\\n    unzip ~/chromedriver_linux64.zip -d ~/ && \\\n    rm ~/chromedriver_linux64.zip && \\\n    chown root:root ~/chromedriver && \\\n    chmod 755 ~/chromedriver && \\\n    mv ~/chromedriver /usr/bin/chromedriver && \\\n    sh -c 'wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -' && \\\n    sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \\\n    apt-get update && apt-get install -y google-chrome-stable\n\n# Create a directory for the application and use it\nRUN mkdir /myapp\nWORKDIR /myapp\n\n# Gemfile and lock file need to be present, they'll be overwritten immediately\nCOPY Gemfile /myapp/Gemfile\nCOPY Gemfile.lock /myapp/Gemfile.lock\n\n# Install gem dependencies\nRUN gem install bundler:2.2.32\nRUN bundle install\n\nRUN apt-get update && apt-get install -y curl apt-transport-https wget && \\\ncurl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \\\necho "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \\\napt-get update && apt-get install -y yarn\nRUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \\\napt-get install nodejs\n\nCOPY . /myapp\n\n# This script runs every time the container is created, necessary for rails\nCOPY entrypoint.sh /usr/bin/\nRUN chmod +x /usr/bin/entrypoint.sh\nENTRYPOINT ["entrypoint.sh"]\nEXPOSE 3000\n\n# Start rails\nCMD ["rails", "server", "-b", "0.0.0.0"]\n
Run Code Online (Sandbox Code Playgroud)\n

我的docker-compose.yml是:

\n
version: "3"\n\nservices:\n  db:\n    image: postgres\n    environment:\n      POSTGRES_USER: yyy\n      POSTGRES_PASSWORD: xxx\n    volumes:\n      - ./tmp/db:/var/lib/postgresql/data\n  web:\n    build: .\n    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"\n    volumes:\n      - .:/myapp\n    ports:\n      - "3000:3000"\n    depends_on:\n      - db\n    environment:\n      POSTGRES_USER: yyy\n      POSTGRES_PASSWORD: xxx\n      POSTGRES_HOST: db\n
Run Code Online (Sandbox Code Playgroud)\n

我的capybara.rb

\n
require 'capybara/rspec'\nrequire 'selenium-webdriver'\n\nCapybara.register_driver :selenium_chrome_headless do |app|\n  Capybara::Selenium::Driver.new app,\n    browser: :chrome,\n    clear_session_storage: true,\n    clear_local_storage: true,\n    capabilities: [Selenium::WebDriver::Chrome::Options.new(\n      args: %w[headless disable-gpu no-sandbox window-size=1024,768],\n    )]\nend\n\nCapybara.javascript_driver = :selenium_chrome_headless\n
Run Code Online (Sandbox Code Playgroud)\n

Gemfile

\n
group :test do\n  # Adds support for Capybara system testing and selenium driver\n  gem 'capybara'\n  gem 'launchy', '~> 2.4.3'\n  gem 'selenium-webdriver'\n  #To clean database for tests\n  gem 'database_cleaner'\n  # Easy installation and use of web drivers to run system tests with browsers\n  # gem 'webdrivers'\nend\n
Run Code Online (Sandbox Code Playgroud)\n

这将在容器中的目录 \xe3\x80\x80chromedriver中安装:google-chrome-stable/usr/bin

\n
# which chromedriver\n/usr/bin/chromedriver\n# which google-chrome-stable\n/usr/bin/google-chrome-stable\n
Run Code Online (Sandbox Code Playgroud)\n

他们的版本匹配(我在网上读到,如果不匹配就会出现问题):

\n
# google-chrome-stable --version\nGoogle Chrome 98.0.4758.80 \n# chromedriver --version\nChromeDriver 98.0.4758.80 (7f0488e8ba0d8e019187c6325a16c29d9b7f4989-refs/branch-heads/4758@{#972})\n
Run Code Online (Sandbox Code Playgroud)\n

所以它应该可以工作。当我尝试运行测试时,当测试正在使用js:true并且我需要 a 时headless chrome,会发生此错误:

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

我一直在四处寻找,但没有找到任何可以帮助的东西,因为我有 M1 芯片,而且我找到的答案不起作用。

\n

笔记:

\n

不知道它是否有帮助,但是当我进入容器并尝试运行chromedriver它时一切正常,但运行时google-chrome-stable出现错误:

\n
# google-chrome-stable\n[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.\n# google-chrome-stable --no-sandbox\nqemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped\nqemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped\n[129:129:0210/020150.185829:ERROR:nacl_fork_delegate_linux.cc(329)] Bad NaCl helper startup ack (0 bytes)\nqemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped\n[93:137:0210/020150.317808:ERROR:file_path_watcher_linux.cc(321)] inotify_init() failed: Function not implemented (38)\n[0210/020150.398311:ERROR:scoped_ptrace_attach.cc(27)] ptrace: Function not implemented (38)\nqemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped\nTrace/breakpoint trap\n
Run Code Online (Sandbox Code Playgroud)\n

我已经解决这个问题很长时间了。您知道如何解决吗?谢谢!

\n

小智 3

解决 mac m1 arm64 中的 chrome 和 chromedriver 问题:

您可以使用以下适用seleniarm/standalone-chromium:latest于arm64架构的docker镜像,使用开源Chromium浏览器运行系统测试。

seleniam/独立铬:最新


归档时间:

查看次数:

3301 次

最近记录:

3 年,5 月 前