YusukeIwaki / puppeteer-ruby

A Ruby port of Puppeteer
Apache License 2.0
290 stars 41 forks source link

Add a "NO_SANDBOX" Environment Variable #306

Closed rubys closed 1 year ago

rubys commented 1 year ago

Simple description about the feature

Add a feature like Grover's GROVER_NO_SANDBOX="true" environment variable.

Usecase / Motivation

Rails 7.1 will include a Dockerfile. The current template can be found here: https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/app/templates/Dockerfile.tt . It runs the Rails application as root, which may or may not be a good idea, but at the moment, it is what it is, and Rails users will undoubtedly try it out. Should their applications make use of puppeteer-rails, however, they will be greeted with messages like:

Puppeteer::BrowserRunner::LaunchError (Failed to launch browser!
ERROR:zygote_host_impl_linux.cc(100)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md):

The last link does contain relevant information, but it doesn't help users of puppeteer-ruby: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#setting-up-chrome-linux-sandbox

YusukeIwaki commented 1 year ago

ref: https://github.com/puppeteer/puppeteer/issues/5505

YusukeIwaki commented 1 year ago

@rubys Hi, thank you for your proposal.

Original puppeteer doesn't have such feature at this moment, however I also think it should be available for Ruby/Rails users. I tried to added the implementation on https://rubygems.org/gems/puppeteer-ruby/versions/0.45.2.alpha2 Please try it and let me know whether it works as expected?

export PUPPETEER_RUBY_NO_SANDBOX=1

Thank you :)

rubys commented 1 year ago

Please try it and let me know whether it works as expected?

Looks good to me. The following works for me (note: must be run on intel hardware as Google doesn't provide Chrome binaries for Linux on ARM):

rails new welcome --minimal
cd welcome
bundle add puppeteer-ruby --version 0.45.2.alpha2

echo 'Rails.application.routes.draw { root "puppeteer#pdf" }' > config/routes.rb

cat << 'EOF' > app/controllers/puppeteer_controller.rb
class PuppeteerController < ApplicationController
  def pdf
    Puppeteer.launch do |browser|
      page = browser.new_page
      page.goto('https://google.com/', wait_until: 'networkidle0')
      render plain: page.pdf(format: 'letter'), content_type: "application/pdf"
    end 
  end 
end
EOF

bundle add dockerfile-rails --git=https://github.com/rubys/dockerfile-rails --branch=main
bin/rails generate dockerfile
docker buildx build . -t rails-welcome
docker run -p 3000:3000 -e RAILS_MASTER_KEY=$(cat config/master.key) rails-welcome
YusukeIwaki commented 1 year ago

0.45.2 will include this feature. https://rubygems.org/gems/puppeteer-ruby/versions/0.45.2

Thank you.