Google Cloud Run的docker映像中的Chromium [英] Chromium inside docker image on Google Cloud Run

查看:106
本文介绍了Google Cloud Run的docker映像中的Chromium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google Cloud上运行一个Docker容器,该容器包含一个简单的nodejs应用和无头的Google Chrome,以便从HTML来源创建PDF.不幸的是,无论我尝试哪种解决方案,Google Cloud Run似乎都存在问题.

I'm trying to have a docker container running on google cloud run, which is containing a simple nodejs app and google chromium headless to create a PDF from HTML source. Unfortunately, Google Cloud Run seems to have issues with thatever solution I try.

我的Docker映像在本地和其他提供程序(例如Azure)上运行完美,但是GCP不能正常工作.

My Docker image(s) run perfectly locally and on other providers (i.E. Azure), but GCP just does not work.

我尝试什么:

基本上构建任何docker映像,安装节点,npm,chr​​ome,然后在后台运行铬-无头.然后运行节点应用程序.Node应用程序只是试图连接到127.0.0.1:9222 =>在GCP上不起作用,但在其他任何地方.

Basically building any docker image, installing node, npm, chromium, then running chromium --headless in the background. Then running the node app. The Node app is simply trying to connect to 127.0.0.1:9222 => that doesn't work on GCP, but anywhere else.

我尝试使用docker hub的官方节点图像我尝试了高山图片我尝试过使用debian图片所有这些都可以在本地正常运行,但不能在Google Cloud上运行.

I tried with the official node images of docker hub I tried with an alpine image I tried with a debian image All of these run fine locally, but not on google cloud run.

这是我用debian图片进行的最新测试:

Here's my latest test with a debian image:

FROM debian:latest


RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
        nodejs yarn npm chromium \
        && apt-get clean \
        && apt-get autoclean
RUN adduser --home /home/node --disabled-password --gecos "" node \
        && mkdir /home/node/app \
        && chown -R node:node /home/node/app
RUN apt-get install -y wget
RUN npm cache clean -f
RUN npm i -g n
RUN n stable
RUN node --version
RUN npm --version

USER node
WORKDIR /home/node/app
ENV CHROME_BIN=/usr/bin/chromium \
        CHROME_PATH=/usr/lib/chromium/

ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}


COPY --chown=node . .

RUN npm install

RUN npm run build

ENV HOST=0.0.0.0 PORT=3000

EXPOSE ${PORT}

ENTRYPOINT [ "sh", "-c", "/home/node/app/docker-inside-start.sh" ]

入口点sh是这样:

#!/bin/sh
exec $CHROME_BIN --headless --use-gl=swiftshader \
        --disable-software-rasterizer --disable-dev-shm-usage --remote-debugging-port=9222 \
        --remote-debugging-address=0.0.0.0 --no-sandbox --disable-gpu \
        --no-first-run --no-crash-upload --no-pings --no-wifi &
node .

我的Node代码看起来像这样.没有什么花哨..APP_CONFIG_CHROME_HOST设置为localhost或127.0.0.1-无效

My Node code looks like this. Nothing fancy.. APP_CONFIG_CHROME_HOST is set to localhost or 127.0.0.1 - nothing works

import * as htmlPdf from 'html-pdf-chrome';
// ...
private createPdf(html: string): Promise<string> {
    return new Promise((resolve, reject) => {
      try {
        const options: htmlPdf.CreateOptions = {
          host: process.env.APP_CONFIG_CHROME_HOST,
          port: 9222, // port Chrome is listening on
        };
        htmlPdf.create(html, options)
          .then((pdf) => resolve(pdf.toBase64()))
          .catch(e => reject(e));
      } catch (err) {
        reject(err);
      }
    });
  }

最后我看到的错误/输出:

Finally the errors/output I see :

2020-09-12 15:26:02.786 MESZ[0912/132602.785920:WARNING:discardable_shared_memory_manager.cc(194)] Less than 64MB of free space in temporary directory for shared memory files: 0

2020-09-12 15:26:38.827 MESZ[0912/132638.826713:ERROR:address_tracker_linux.cc(201)] Could not bind NETLINK socket: Permission denied (13)

2020-09-12 15:26:38.828 MESZ[0912/132638.828564:ERROR:file_path_watcher_linux.cc(71)] Failed to read /proc/sys/fs/inotify/max_user_watches
...
2020-09-12 15:28:28.364 MESZ[2020-09-12T13:28:26.764Z] [ERROR] <mail.service.js> Error: socket hang up

有任何想法吗?

注意:例如,使用高山图像时的错误是不同的.谷歌搜索表明gcp与那里的其他部分存在一般性问题.具体来说,在gvisor中: https://github.com/google/gvisor/issues/1739#issuecomment-673861090 如果您想体验一下,这里是一个与上述相同的docker映像,但在高山上执行:chrome不在那儿运行,并因抱怨缺少系统调用而退出

Note: the errors when using for example an alpine image are different. Googling points to hints that gcp has a general issue with some other part there. To be specific, in gvisor: https://github.com/google/gvisor/issues/1739#issuecomment-673861090 In case you want to experience that, heres a docker image doing the same like the above one but on alpine: chrome doesn't run there and exits with complaining about missing membarrier syscall

FROM alpine:latest

ARG BUILD_DATE
ARG VCS_REF

LABEL org.label-schema.build-date=$BUILD_DATE \
        org.label-schema.description="Chrome running in headless mode in a tiny Alpine image" \
        org.label-schema.name="alpine-chrome" \
        org.label-schema.schema-version="1.0.0-rc1" \
        org.label-schema.usage="https://github.com/Zenika/alpine-chrome/blob/master/README.md" \
        org.label-schema.vcs-url="https://github.com/Zenika/alpine-chrome" \
        org.label-schema.vcs-ref=$VCS_REF \
        org.label-schema.vendor="Zenika" \
        org.label-schema.version="latest"

# Installs latest Chromium package.
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" > /etc/apk/repositories \
        && echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
        && echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories \
        && echo "http://dl-cdn.alpinelinux.org/alpine/v3.11/main" >> /etc/apk/repositories \
        && apk upgrade -U -a \
        && apk add --no-cache \
        libstdc++ \
        chromium \
        harfbuzz \
        nss \
        freetype \
        ttf-freefont \
        wqy-zenhei \
        tini make gcc g++ python git nodejs nodejs-npm yarn \
        && rm -rf /var/cache/* /var/lib/apt/lists/* /usr/share/man /tmp/*

RUN mkdir -p /home/node/app \
        && adduser -D node \
        && chown -R node:node /home/node/app

USER node
WORKDIR /home/node/app

ENV CHROME_BIN=/usr/bin/chromium-browser \
        CHROME_PATH=/usr/lib/chromium/


# set ENV to development if NOT given in command line
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}

COPY --chown=node package*.json ./

RUN npm install

COPY --chown=node . .

RUN npm run build

ENV HOST=0.0.0.0 PORT=3000

EXPOSE ${PORT}
ENTRYPOINT ["/sbin/tini", "--"]
CMD [ "sh", "-c", "/home/node/app/docker-inside-start.sh" ]

推荐答案

我正在Cloud Run中运行chrome,以将网页转换为PDF(十个转换为SVG).在此处

I am running chrome inside Cloud Run to transform webpages to PDF (and ten to SVG). Find my repo here

这是我的Dockerfile:

Here is my Dockerfile:

FROM node:14-slim

    # Add contrib packages for ms fonts
RUN echo "deb http://http.debian.net/debian/ stretch main contrib non-free" > /etc/apt/sources.list && \
    echo "deb http://http.debian.net/debian/ stretch-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb http://security.debian.org/ stretch/updates main contrib non-free" >> /etc/apt/sources.list && \
    apt-get update && \
    # Adds required libs for Headless Chrome
    apt-get install -yq gconf-service 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 \
    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 \
    ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget \
    # fonts
    ttf-mscorefonts-installer fontconfig && \
    fc-cache -f

# Start the app
WORKDIR /usr/src/app
COPY package*.json ./
ENV NODE_ENV=production
RUN npm install --production
COPY . .
CMD [ "npm", "start" ]

这是我的Node.js代码的relevanr部分,使用"puppeteer":"^ 3.0.4" :

And here is the relevanr part of my Node.js code using "puppeteer": "^3.0.4":

    if(!browser) {
        browser = await puppeteer.launch({
            args: ['--no-sandbox']
        });
    }
    if(!page) {
        page = await browser.newPage();
    }

    // https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagepdfoptions
    await page.emulateMedia('screen');
    let printParams = {
        path: inputPDFFilename,
        width,
        printBackground: true
    };
    if(height) {
        printParams.height = height;
    }
    await page.pdf(printParams);

这篇关于Google Cloud Run的docker映像中的Chromium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆