Launch browser using puppeteer on docker container

Viewed 134

I am trying to launch the browser using puppeteer on docker container.

However, when I am trying load the browser by hitting the API, I am seeing the following error enter image description here

localhost:3000 is my client running locally. I am not sure if docker can access this address. I am thinking maybe this could be the reason for the connection failure. Please correct me if I am wrong.

When I try the above scenario without docker, it is working fine, I am able to see the puppeteer opening chromium browsers and show the page. To make it work on the docker container, what should I do?

1 Answers

The localhost on your host, where your application running on port 3000 is, is in a different namespace compared to the localhost for your puppeteer instance running in docker.

To fix this, you can either:

  • Make them be in the same namespace (Host Mode)
  • Create a bridge between them (Bridge Mode)

Host Mode

This puts your container in the same network namespace as the host. localhost will refer to the same thing inside and outside the container. Add --net-host to your docker run command

Bridge Mode

You can form a bridge between the container and host, by adding --add-host host.docker.internal:host-gateway to the docker run command, and changing puppeteer to use host.docker.internal:3000

Related