@@ -4,8 +4,148 @@ sidebar_position: 22
44
55# Docker
66
7- Chrome runs a bit differently in a container, in ways that are not obvious and that surface as
8- slow leaks rather than errors. This page collects everything Ferrum-specific about it.
7+ There are two ways to use Chrome with Docker:
8+
9+ - ** Our images** , ` ghcr.io/rubycdp/chrome ` and ` ghcr.io/rubycdp/chromium ` : Chrome runs in its own container and Ferrum
10+ connects to it. Use these unless you need Chrome inside your app's container. Process reaping, crashpad, fonts and
11+ flags are taken care of.
12+ - ** Chrome in your own container** , next to your Ruby process. Chrome runs a bit differently in a container, in ways
13+ that are not obvious and that surface as slow leaks rather than errors, and [ Running Chrome yourself] ( #running-chrome-yourself )
14+ covers all of it.
15+
16+ ## Our images (recommended)
17+
18+ Two images, both for amd64 and arm64: ` chromium ` ships Debian's Chromium, ` chrome ` ships Google Chrome. Each
19+ connection gets its own Chrome with a fresh profile, which is stopped as soon as the connection closes. Nothing else
20+ talks to your browser: the driver inside passes CDP through unchanged.
21+
22+ Tags are ` latest ` , the major version (` 155 ` ) and the full version (` 155.0.8059.84 ` ). New browser releases are picked up
23+ daily. Older tags stay available, so you can pin a version.
24+
25+ ### Quick start
26+
27+ ``` shell
28+ docker run --rm -p 3000:3000 ghcr.io/rubycdp/chromium
29+ ```
30+
31+ ``` ruby
32+ browser = Ferrum ::Browser .new (url: " http://localhost:3000" )
33+ browser.go_to(" https://example.com" )
34+ browser.quit
35+ ```
36+
37+ ` ws_url: "ws://localhost:3000" ` works too.
38+
39+ ### Compose and Cuprite
40+
41+ ``` yaml
42+ services :
43+ chrome :
44+ image : ghcr.io/rubycdp/chromium
45+ environment :
46+ CONCURRENT : 4
47+ volumes :
48+ - .:/app
49+
50+ app :
51+ build : .
52+ volumes :
53+ - .:/app
54+ environment :
55+ CHROME_URL : http://chrome:3000
56+ depends_on :
57+ chrome :
58+ condition : service_healthy
59+ ` ` `
60+
61+ ` ` ` ruby
62+ Capybara.register_driver(:cuprite) do |app|
63+ Capybara::Cuprite::Driver.new(app, url : ENV.fetch("CHROME_URL"))
64+ end
65+
66+ Capybara.server_host = "0.0.0.0"
67+ Capybara.server_port = 3001
68+ Capybara.app_host = "http://app:3001"
69+ ```
70+
71+ Chrome has to reach your app, so bind the server to ` 0.0.0.0 ` and point ` app_host ` at the app's name on the compose
72+ network. When the app runs on your machine instead, use ` host.docker.internal ` , which needs
73+ ` extra_hosts: ["host.docker.internal:host-gateway"] ` on Linux.
74+
75+ Cuprite keeps one connection per test process and calls ` reset ` between tests, so every parallel worker gets its own
76+ Chrome. The image has a healthcheck on ` /health ` .
77+
78+ ### Options per connection
79+
80+ Ferrum's ` window_size: ` and ` browser_options: ` only apply to a Chrome that Ferrum starts itself. For a remote one, put
81+ them in the connection URL as ` launch ` , JSON or base64:
82+
83+ ``` ruby
84+ launch = {
85+ defaultViewport: { width: 1920 , height: 1080 },
86+ args: [" --accept-lang=de-DE" ]
87+ }
88+ Ferrum ::Browser .new (url: " http://localhost:3000?launch=#{ CGI .escape(launch.to_json)} " )
89+ ```
90+
91+ - ` args ` : extra Chrome flags. A flag with the same name as a default replaces it.
92+ - ` defaultViewport ` : the window size, 1024x768 by default like Ferrum's.
93+ - ` ignoreDefaultArgs ` : ` true ` , or a list of default flags to drop.
94+ - ` headless: false ` : Chrome gets a window on its own virtual display.
95+
96+ ` timeout=<ms> ` in the same URL ends the session after that long. ` docker run --rm ghcr.io/rubycdp/chromium --help `
97+ lists everything, including the default flags, which are Ferrum's.
98+
99+ ### Settings
100+
101+ - ` CONCURRENT ` : how many sessions run at once, the number of CPUs by default. Set it to at least your number of test
102+ workers.
103+ - ` QUEUED ` : how many connections wait for a free slot, 10 by default. Past that they get ` 429 ` .
104+ - ` TIMEOUT ` : a session timeout in milliseconds for every connection. There is none by default.
105+ - ` EXTERNAL ` : the public URL when the container sits behind a proxy, so the WebSocket address it hands out is
106+ reachable.
107+ - ` PORT ` and ` HOST ` : where it listens, ` 0.0.0.0:3000 ` by default.
108+
109+ ### Files
110+
111+ Chrome resolves file paths inside its own container. Mount the directories that uploads come from and downloads go to
112+ at the same absolute path in both containers, and ` attach_file ` , ` select_file ` and ` save_path ` work unchanged. Run the
113+ container as your user so downloaded files belong to you:
114+
115+ ``` shell
116+ docker run --rm -p 3000:3000 --user " $( id -u) :$( id -g) " -v " $PWD :$PWD " ghcr.io/rubycdp/chromium
117+ ```
118+
119+ Screenshots and PDFs need no mount, they come back over CDP.
120+
121+ ### Watching a test
122+
123+ Open [ http://localhost:3000 ] ( http://localhost:3000 ) while tests run. The dashboard lists running sessions and their
124+ pages, and each page links to DevTools with a live view of it. Pages go away when the test ends, so pause the test
125+ (` binding.irb ` , a breakpoint) to look at one.
126+
127+ ### Security
128+
129+ ::: warning
130+ There is no authentication, and anyone who can connect can pass Chrome flags that run commands in the container. Never
131+ publish the port beyond localhost or a private network.
132+ :::
133+
134+ ### Coming from browserless
135+
136+ Usually you swap the image and connect to ` / ` . The differences that matter:
137+
138+ - There's no session timeout by default, browserless kills sessions after 30 seconds.
139+ - There are no ` /chrome ` or ` /chromium ` paths, no tokens, and no REST APIs (` /screenshot ` , ` /pdf ` , ` /function ` ).
140+ Ferrum does all of that over CDP.
141+ - There's no ` /workspace ` , files go through mounts as described above.
142+ - Nothing resets your viewport or download folder, and ` reset ` really isolates tests.
143+
144+ The full comparison and the reasons behind it are in the [ design notes] ( https://github.com/rubycdp/ferrum/blob/main/docker/DESIGN.md ) .
145+
146+ ## Running Chrome yourself
147+
148+ ### In your app's container
9149
10150::: note
11151When running in docker as root
@@ -18,7 +158,7 @@ Ferrum::Browser.new(dockerize: true)
18158Essentially it just sets CLI flags for a browser to make it start. On CI, you can just set ` FERRUM_CHROME_DOCKERIZE=true `
19159environment variable, and it will be passed to all browser instances.
20160
21- ## An init is required
161+ ### An init is required
22162
23163::: warning
24164If your Ruby process is pid 1, run the container with an init: ` docker run --init ` , ` init: true ` in Compose, or
@@ -63,7 +203,7 @@ its full kill timeout.
63203
64204An init fixes both, and is worth having regardless: every other process your image spawns has the same problem.
65205
66- ## Kubernetes
206+ ### Kubernetes
67207
68208There is no ` init: true ` in a pod spec, so you have two options.
69209
@@ -83,7 +223,7 @@ also stops being pid 1, so anyone doing `kill -HUP 1` now signals `pause`.
83223` initContainers ` are not an init system.
84224:::
85225
86- ## What Ferrum cleans up, and what it does not
226+ ### What Ferrum cleans up, and what it does not
87227
88228Ferrum spawns exactly one process: the browser. On ` #quit ` it signals the browser's process group, escalating from
89229` TERM ` to ` KILL ` , and waits on the browser itself — the one process it is the parent of, and therefore the only one
@@ -98,9 +238,9 @@ library cannot take that liberty, which is why the answer here is an init rather
98238happens, so it is not even in the process group Ferrum signals. See [ the crashpad handler] ( /docs/ferrum/customization#the-crashpad-handler ) ,
99239including why the flag that appears to disable it must not be used.
100240
101- ## Running Chrome in a separate container
241+ ### Another remote browser service
102242
103- If Chrome lives in its own container and Ferrum connects over a websocket, Ferrum did not spawn that browser and
243+ Any service that speaks CDP works the same way, with ` url: ` or ` ws_url: ` . Ferrum did not spawn that browser and
104244couldn't kill it:
105245
106246``` ruby
@@ -116,7 +256,7 @@ browser.close
116256browser.quit
117257```
118258
119- ## Diagnosing
259+ ### Diagnosing
120260
121261Check whether the processes piling up are dead or alive:
122262
0 commit comments