剧作家无法在 Ubuntu 上打开浏览器

Eri*_*ler 5 linux x11 xserver docker playwright

我试图弄清楚如何打开一个有头浏览器来从在 Docker 容器中运行的 NestJs 应用程序内部执行一些任务。

\n

申请代码:

\n
import { Controller, Get } from '@nestjs/common';\nimport { AppService } from './app.service';\nimport { chromium } from "playwright";\n\n@Controller()\nexport class AppController {\n  constructor(private readonly appService: AppService) {}\n\n  @Get()\n  async getHello(): Promise<string> {\n    const browser = await chromium.launch({ \n      headless: false,\n      env: {\n        "DISPLAY": ":99"\n      }\n    });\n    const context = await browser.newContext({ javaScriptEnabled: true });\n    const page = await context.newPage();\n\n    await page.goto("http://www.google.com");\n    await page.waitForLoadState("load");\n\n    return "Yay";\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Dockerfile:

\n
FROM ubuntu:20.04 AS BUILD_IMAGE\n\n# INSTALL NODE\nRUN apt-get update && apt-get install -y \\\ncurl\nRUN curl -sL https://deb.nodesource.com/setup_14.x | bash -\nRUN apt-get install nodejs -y\n\nENV NODE_ENV production\n\n# Set up our work directory again\nWORKDIR /app\nCOPY package.json package-lock.json ./\nRUN npm ci\n\n# copy the source code to build the next\n# server for production\nCOPY . .\n\nRUN npm run build\n\n# Remove all the development dependencies since we don't\n# need them to run the actual server.\nRUN rm -rf node_modules\nRUN npm ci --production --ignore-scripts\n\n# END OF BUILD IMAGE\n\n\n# This starts our application's run image - the final output of build.\nFROM ubuntu:20.04\n\n# INSTALL NODE\nRUN apt-get update && apt-get install -y \\\ncurl\nRUN curl -sL https://deb.nodesource.com/setup_14.x | bash -\nRUN apt-get install nodejs -y\n\nENV NODE_ENV production\n\nRUN addgroup -gid 1001 --system nodejs\nRUN adduser --system nestjs -u 1001\n\n# Pull the built files out of BUILD_IMAGE - we need:\n# 1. the package.json and package-lock.json\n# 2. the Nest build output\n# 3. the node_modules.\nWORKDIR /app\nCOPY --from=BUILD_IMAGE --chown=nestjs:nodejs /app/package.json /app/package-lock.json ./\nCOPY --from=BUILD_IMAGE --chown=nestjs:nodejs /app/node_modules ./node_modules\nCOPY --from=BUILD_IMAGE --chown=nestjs:nodejs /app/dist ./dist\n\n\nRUN npx playwright install-deps \nRUN apt-get install xvfb \nRUN apt-get install x11-apps -y\n\nRUN apt-get install screen -y\nRUN screen -d -m Xvfb -ac :99 -screen 0 1280x1024x16\nENV DISPLAY :99\n\nUSER nestjs\nWORKDIR /app\nRUN npx playwright install\n\n# Bind the server to 0.0.0.0:3000 on the assumption that\n# firewalls in the VPC and container will prevent abuse of 0.0.0.0.\nENV PORT 3000\nENV HOST 0.0.0.0\n\nEXPOSE 3000\n\nCMD [ "npm", "run", "start:prod" ]\n
Run Code Online (Sandbox Code Playgroud)\n

问题:通过 http 请求调用 playwright 时,输出错误:

\n
\xe2\x95\x94\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x97\n\xe2\x95\x91 Looks like you launched a headed browser without having a XServer running.                      \xe2\x95\x91\n\xe2\x95\x91 Set either 'headless: false' or use 'xvfb-run <your-playwright-app>' before running Playwright. \xe2\x95\x91\n\xe2\x95\x91                                                                                                 \xe2\x95\x91\n\xe2\x95\x91 <3 Playwright Team                                                                              \xe2\x95\x91\n\xe2\x95\x9a\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x9d\n
Run Code Online (Sandbox Code Playgroud)\n

据我了解,RUN screen -d -m Xvfb -ac :99 -screen 0 1280x1024x16应该运行 XServer。通过 bash 检查显示$DISPLAY:99. 为什么剧作家无法看到正在运行的 XServer?

\n

编辑:\n这与从命令行调用 chrome 时的结果相同:

\n
/home/nestjs/.cache/ms-playwright/chromium-1000/chrome-linux/chrome --no-sandbox\n[419:419:0510/011729.759292:ERROR:ozone_platform_x11.cc(247)] Missing X server or $DISPLAY\n[419:419:0510/011729.759331:ERROR:env.cc(225)] The platform failed to initialize.  Exiting.\n
Run Code Online (Sandbox Code Playgroud)\n