Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome inside docker ERR_CONNECTION_REFUSED

We're trying to run chrome inside a docker container.

The docker run command after the build is the following:

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 is an example to indicate the ip of another container in the same network.

Once inside the container the host file look something like this:

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

and as expected running:

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

result in a correct connection

running a connection in the browser, however :

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

result in a ERR_CONNECTION_REFUSED.

The browser is able to navigate to any other website.

thanks.

EDIT: Please note that both the wget and the opening of the browser are launched inside the container, as we use chrome headless for test purposes.

like image 417
Francesco Panina Avatar asked Aug 14 '26 03:08

Francesco Panina


1 Answers

In my case I'm using spatie/browsershot, but I believe the underlying issues are the same, so I hope my answer is useful.

My ERR_CONNECTION_REFUSED error resolved when the following pieces were in place:

1) The docker container hosting your site needs a network alias.

This is instead of adding a hosts file entry in the Puppeteer container, which Chrome did not appear to honour in my testing.

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) Add the following Puppeteer args

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

I realise the original question was not asking about Browsershot, but for that the args are added like so:

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

Doing so allowed me to run both Browsershot and Puppeteer inside a container, and for Chrome to be able to access sites hosted inside other containers, by referencing their domain name.

like image 178
jeff-h Avatar answered Aug 16 '26 17:08

jeff-h