Connect Rails to Browserless Chrome: Step-by-Step
In this article
Connecting a Rails app to a Browserless Chrome container lets you run headless Chrome tasks—like generating PDFs, taking screenshots, or scraping JavaScript-heavy pages—without managing Chrome yourself. You send HTTP requests to a Dockerized Browserless service, and it returns the results. This guide walks you through setting up the container, configuring your Rails app, and making your first call.
What You'll Need
- A Rails app (any version, but 6+ recommended) with internet access.
- Docker installed on your development machine or server.
- Basic familiarity with Rails routes, controllers, and environment variables.
- An HTTP client gem like
net/http(built-in) orhttparty(optional).
Step-by-Step
- Pull and run the Browserless Chrome Docker image.
Open a terminal and run:docker run -d -p 3000:3000 browserless/chrome
This starts a container with Browserless listening on port 3000. You can change the host port (e.g.,-p 8080:3000) if 3000 conflicts with your Rails server. - Verify the container is running.
Rundocker psto see the container status. Then test the API by visitinghttp://localhost:3000/json/versionin a browser—you should see a JSON response with Chrome version info. - Add a gem for HTTP requests (optional).
If you prefer a clean syntax, addgem 'httparty'to your Gemfile, then runbundle install. Otherwise, use Ruby's built-innet/http. - Create a service object in Rails.
Createapp/services/browserless_client.rbwith a method to send requests. For example:class BrowserlessClient def self.screenshot(url, options = {}) query = { url: url }.merge(options).to_query uri = URI("http://localhost:3000/screenshot?#{query}") Net::HTTP.get_response(uri) end end - Set the Browserless URL via environment variable.
Don't hardcodelocalhost. Inconfig/application.rbor an initializer, define:BROWSERLESS_URL = ENV.fetch('BROWSERLESS_URL', 'http://localhost:3000')
Then useBROWSERLESS_URLin your service. - Create a controller action to use the client.
For example, inapp/controllers/screenshots_controller.rb:def create response = BrowserlessClient.screenshot(params[:url]) send_data response.body, type: 'image/png', disposition: 'inline' end
- Add a route.
Inconfig/routes.rb, addpost '/screenshot', to: 'screenshots#create'. - Test the integration.
Start your Rails server (rails s) and send a POST request tohttp://localhost:3000/screenshotwith aurlparameter. You should receive a PNG image in response.
Common Mistakes
- Port conflict: Rails and Browserless both default to port 3000. Change one of them.
- Using
localhostfrom a different machine: If your Rails app runs on a remote server, use the container's IP or a Docker network. - Not URL-encoding parameters: Always use
.to_queryorURI.encode_www_formto encode URLs passed as parameters. - Ignoring response errors: Browserless returns non-200 status codes for invalid requests. Check
response.codebefore processing the body.
Tips
- Use Docker Compose for local development: Define both your Rails app and Browserless in a
docker-compose.ymlto manage them together. - Set a token for security: Browserless supports a
--tokenflag. Add it to your Docker run command and pass it as a query parameter to every request. - Increase timeout for heavy pages: Browserless has a default timeout of 30 seconds. You can pass
timeoutas a query parameter (e.g.,&timeout=60000). - Use the
waitForoption: For pages that load content dynamically, passwaitForwith a selector to ensure the element appears before capturing.
Troubleshooting
- Connection refused: Ensure the container is running and the port is mapped correctly. Run
docker psto confirm. - Timeout errors: Increase the timeout in your request or pass a longer
timeoutparameter to Browserless. - Empty or corrupt response: Check the response status code. If it's 400, your URL parameter is likely malformed. If 500, the page might be unreachable or too heavy.
- Container stops unexpectedly: Check Docker logs with
docker logs <container_id>to see error messages.
Frequently Asked Questions
Can I use Browserless without Docker?
Yes, you can install Chrome and run the Browserless Node.js server directly, but Docker is the recommended way because it handles dependencies and isolation.
How do I generate a PDF instead of a screenshot?
Use the /pdf endpoint instead of /screenshot. For example: http://localhost:3000/pdf?url=...
Is it safe to expose Browserless to the public internet?
No. Always run it behind a firewall or use a token. The default setup has no authentication, so anyone could use it to make requests from your server.
Can I use a headless Chrome binary instead of Browserless?
Yes, but Browserless simplifies the process by providing an HTTP API and handling concurrency, so you don't have to manage Chrome processes yourself.
How do I pass custom Chrome flags?
Use the launchOptions parameter in the query string, e.g., &launchOptions[args][]=--no-sandbox.
Does Browserless support authentication?
Yes, you can set a token when starting the container and then include ?token=your-token in every request.