ebb*_*hop 7 node.js docker docker-compose puppeteer
我正在尝试使用docker-compose在运行于docker容器中的快速应用程序中启动puppeteer。
应该启动puppeteer的行将const browser = await puppeteer.launch({args: ['--no-sandbox']});引发以下错误:
(node:28) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError [ERR_ASSERTION]: Chromium revision is not downloaded. Run "npm install"
我试着加入yarn add puppeteer后yarn install,也更换yarn install在Dockerfile用npm install。
需要进行哪些更改,以便我可以按预期使用木偶和铬?
FROM node:8
RUN apt-get update
# for https
RUN apt-get install -yyq ca-certificates
# install libraries
RUN apt-get install -yyq libappindicator1 libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6
# tools
RUN apt-get install -yyq gconf-service lsb-release wget xdg-utils
# and fonts
RUN apt-get install -yyq fonts-liberation
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY code/package.json /usr/src/app
COPY code/index.js /usr/src/app
RUN mkdir -p /usr/src/app/views
COPY code/views/ /usr/src/app
# install the necessary packages
RUN yarn install
CMD npm run start:dev
Run Code Online (Sandbox Code Playgroud)
app:
restart: always
build: ${REPO}
volumes:
- ${REPO}/code:/usr/src/app:ro
working_dir: /usr/src/app
ports:
- "8087:5000"
Run Code Online (Sandbox Code Playgroud)
app.post('/img', function (req, res) {
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
})();
});
Run Code Online (Sandbox Code Playgroud)
我使用的 docker 卷将整个本地code目录映射到 docker 容器的/usr/src/app目录。
这对于在开发期间允许快速代码更新非常有用。
然而,它也覆盖上以前安装的铬版泊坞窗容器通过yarn install在Dockerfile与安装在铬的版本我的机器通过yarn install在命令行中。
每台机器都需要自己的、正确的、特定于操作系统的铬版本。docker 容器需要一个 linux 专用的 Chromium ( linux-515411),我的笔记本电脑需要一个 mac 专用的 Chromium ( mac-508693)。简单地运行yarn install(或npm install)与 puppeteer 一起package.json安装正确版本的铬。
.
??? Dockerfile
??? README.md
??? code
??? index.js
??? package.json
??? node_modules
??? lots of other node packages
??? puppeteer
??? .local-chromium
? ??? mac-508693 <--------good for macs, bad for linux!
??? package.json
??? all the other puppeteer files
Run Code Online (Sandbox Code Playgroud)
这是容器获得自己版本的地方.local-chromium:
FROM node:8
RUN apt-get update
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY code/package.json /usr/src/app
COPY code/index.js /usr/src/app
# install the necessary packages <------ including puppeteer, with the correct chromium
RUN yarn install
CMD npm run start:dev
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml这会将所有内容从本地复制${REPO}/code到 docker 容器的/usr/src/app目录。包括错误版本的铬。
volumes:
- ${REPO}/code:/usr/src/app:ro
Run Code Online (Sandbox Code Playgroud)
.
??? Dockerfile
??? README.md
??? code
??? src
? ??? index.js
? ??? package.json
??? node_modules
??? lots of other node packages
??? puppeteer
??? .local-chromium
??? package.json
??? all the other puppeteer files
Run Code Online (Sandbox Code Playgroud)
更新的 docker 卷将本地的全部内容映射./code/src到 docker 容器的/usr/src/app. 但这不包括node_modules目录:
volumes:
- ${REPO}/code/src:/usr/src/app:ro
Run Code Online (Sandbox Code Playgroud)
我遇到了这个问题,我想留下简单的解决方案。它找不到我的 chrome 安装的原因是我已将本地卷安装到容器中进行测试。我用的是mac,所以当地npm install给了我mac版本的chromium。当该node_modules文件夹被安装到 Linux 容器中时,它期望找到不存在的 Linux 版本。
为了解决这个问题,您需要node_modules在将卷挂载到容器中时排除文件夹。我可以通过传递另一个volume参数来做到这一点。
docker run -rm \
--volume ~/$project:/dist/$project \
--volume /dist/$project/node_modules
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5653 次 |
| 最近记录: |