Playwright: [Question] how to run playwright script in docker

Created on 16 Jul 2020  路  2Comments  路  Source: microsoft/playwright

I am still new to Docker so I am wondering how to execute the simplest playwright JS with node as the dockerfile provided in https://github.com/microsoft/playwright/blob/master/docs/docker/Dockerfile.bionic does not npm install playwright globally.

I ran this script:
const playwright = require('playwright'); (async () => { for (const browserType of ['chromium', 'firefox', 'webkit']) { const browser = await playwright[browserType].launch({headless: true, args: ['--no-sandbox']}); const context = await browser.newContext(); const page = await context.newPage(); await page.goto('http://whatsmyuseragent.org/'); await page.screenshot({ path:example-${browserType}.png}); await browser.close(); } })();
but I get error Error: Cannot find module 'playwright'

Any help is much appreciated fellow coders.

Most helpful comment

As you pointed out the Docker image does not include Playwright itself, so you'll have to install it separately, e.g. when running the Docker container you can do something like this:

npm init -y
npm install playwright --save

And then run your script from the same directory.

Alternatively you could bake Playwright into your docker image, e.g. by adding the following line to Docker.bionic:

RUN mkdir -p /home/pwuser/example && cd /home/pwuser/example && npm init -y && install playwright --save

All 2 comments

As you pointed out the Docker image does not include Playwright itself, so you'll have to install it separately, e.g. when running the Docker container you can do something like this:

npm init -y
npm install playwright --save

And then run your script from the same directory.

Alternatively you could bake Playwright into your docker image, e.g. by adding the following line to Docker.bionic:

RUN mkdir -p /home/pwuser/example && cd /home/pwuser/example && npm init -y && install playwright --save

thanks Yury. I got it working.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kblok picture kblok  路  4Comments

shirshak55 picture shirshak55  路  3Comments

jperl picture jperl  路  3Comments

etnbrd picture etnbrd  路  4Comments

juliomatcom picture juliomatcom  路  3Comments