Connect to a browser
By default, chromedp starts Chrome itself and talks to it over a pipe. It can also connect to a Chrome that runs already, on the same computer or on another one, over the WebSocket of its debugging port.
Run Chrome in a container
Section titled “Run Chrome in a container”The image chromedp/headless-shell holds a small build of Chrome that listens on port 9222:
docker run -d -p 9222:9222 --rm --name headless-shell docker.io/chromedp/headless-shellPodman takes the same arguments. A large page can need more shared memory
than the default of the container. Add --shm-size 2G for it.
Connect
Section titled “Connect”The package github.com/chromedp/chromedp/remote connects to the browser.
Give it the address of the debugging port. It asks the browser for the
address of its WebSocket:
import "github.com/chromedp/chromedp/remote"
allocCtx, cancelAlloc := remote.NewAllocator(context.Background(), "ws://127.0.0.1:9222/")
defer cancelAlloc()
ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()The rest of the program does not change. cancel closes the tab, and the
browser keeps running, because the program did not start it.
The browser must reach the page
Section titled “The browser must reach the page”A browser in a container reaches the network of the container. A page on
localhost of your computer is not on localhost of the container. Serve
the page on an address that the container can reach, or run the program in
the same container network.