# Connect to a browser

> Drive a Chrome that runs already, such as the container image chromedp/headless-shell, over a WebSocket.

The page: https://chromedp.dev/guides/remote-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

The image [chromedp/headless-shell](https://hub.docker.com/r/chromedp/headless-shell)
holds a small build of Chrome that listens on port 9222:

```sh
docker run -d -p 9222:9222 --rm --name headless-shell docker.io/chromedp/headless-shell
```

Podman 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

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:

```go
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.

:::caution
The debugging port gives full control of the browser to anyone who reaches
it. Do not open it to a network that you do not trust.
:::

## 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.
