admin管理员组

文章数量:1402350

I have a simple script that launches a chromium playwright browser instance. Running the script locally with --remote-debugging-port=9222 as an argument works fine, and visiting http://127.0.0.1:9222/json/version returns info about the browser instance in JSON format as expected. So far so good.

However, when I run the exact same script inside a docker container with a playwright server running (and -p 9222:9222 to port forward), and visit http://127.0.0.1:9222/json/version or http://localhost:9222/json/version there is nothing there. Any advice would be very appreciated.

This is my script.js file:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    headless: true,
    args: [
      '--remote-debugging-port=9222',
      '--remote-debugging-address=0.0.0.0',
      '--no-sandbox',
      '--disable-gpu',
      '--disable-software-rasterizer',
      '--disable-dev-shm-usage',
      '--disable-background-networking'
    ]
  });

  // Optionally, open a page
  const page = await browser.newPage();
  await page.goto('');  // example, you can visit any page you like
})();

Dockerfile:

# Use the Playwright base image
FROM mcr.microsoft/playwright:v1.50.0-noble

# Copy your script.js into the container
COPY script.js .

RUN npm install [email protected]

# Command to run your script.js when the container starts
CMD ["sh", "-c", "npx -y [email protected] run-server --port 3000 --host 0.0.0.0 & node script.js"]

package.json:

{
  "dependencies": {
    "playwright": "^1.50.0"
  },
  "scripts": {
    "start": "node script.js"
  }
}

I then build the image and run it with "docker run -p 3000:3000 -p 9222:9222 ".

本文标签: Playwright remote debugging port 9222 inaccessible when running inside a docker containerStack Overflow