2

我们正在尝试在 docker 容器中运行 chrome。

构建后的docker run命令如下:

docker run \
 --rm \
 -ti \
 --add-host=example.my_domain.localhost:172.21.0.13 \
 --env="APP_ENV=test" \
 --privileged \
 --volume "$volumeDir:/app" \
 --cap-add SYS_ADMIN \
 --net custom_network \
 built_image_tag bash

172.21.0.13是指示同一网络中另一个容器的 ip 的示例。

进入容器后,主机文件看起来像这样:

127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.21.0.13 example.my_domain.localhost
172.21.0.15 7229a8eac11e

并按预期运行:

$ wget example.my_domain.localhost
Connecting to example.my_domain.localhost (example.my_domain.localhost)|172.21.0.13|:80... connected.

导致正确的连接

但是,在浏览器中运行连接:

$ google-chrome http://example.my_domain.localhost

导致ERR_CONNECTION_REFUSED

浏览器能够导航到任何其他网站。

谢谢。

编辑: 请注意,wget和浏览器的打开都是在容器启动的,因为我们使用 chrome headless 进行测试。

4

1 回答 1

0

在我的情况下,我使用spatie/browsershot,但我相信潜在的问题是相同的,所以我希望我的回答是有用的。

ERR_CONNECTION_REFUSED当以下部分到位时,我的错误得到解决:

1)托管您站点的 docker 容器需要一个网络别名。

这不是在 Puppeteer 容器中添加 hosts 文件条目,Chrome 在我的测试中似乎没有遵守该条目。

services:
  my-nginx-container:
    ...
    networks:
      internal:
        aliases:
          # so we can access our sites directly (by their domain) from other containers.
          - my-docker-site.com

2) 添加以下 Puppeteer 参数

--disable-web-security'--enable-features=NetworkService

我意识到最初的问题不是询问 Browsershot,而是像这样添加 args:

$browsershot = Browsershot::url('https://my-docker-site.com')
    ->setOption('args', ['--disable-web-security', '--enable-features=NetworkService'])

这样做让我可以在一个容器中同时运行 Browsershot 和 Puppeteer,并且让 Chrome 能够通过引用它们的域名来访问托管在其他容器中的站点。

于 2021-03-25T00:19:48.407 回答