LoginSignup
3
3

More than 1 year has passed since last update.

puppeteer用のDockerFile (alpine)

Last updated at Posted at 2021-09-15

ブラウザー操作を自動化して情報取得するためにpuppeteerを使いはじめました。alpineベースで日本語対応まで動作確認したものをここで公開しておきます(2021/9現在)

いろいろやっていたら今はPlaywrightの方が流行のようなのでそちらも書きました(2021/9/15)

Dockerファイル

  • 日本語対応のためにフォントを追加しています。
DockerFile
FROM alpine

# Installs latest Chromium package.
RUN apk add --no-cache \
      chromium \
      nss \
      freetype \
      harfbuzz \
      ca-certificates \
      ttf-freefont \
      nodejs \
      yarn

RUN apk add --no-cache curl fontconfig \
  && curl -O https://noto-website.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip \
  && mkdir -p /usr/share/fonts/NotoSansCJKjp \
  && unzip NotoSansCJKjp-hinted.zip -d /usr/share/fonts/NotoSansCJKjp/ \
  && rm NotoSansCJKjp-hinted.zip \
  && fc-cache -fv

# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
    PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

# Puppeteer v10.0.0 works with Chromium 92.
RUN yarn add puppeteer@10.0.0

サンプル

yahoo.js
const puppeteer = require('puppeteer')

;(async () => {
  const browser = await puppeteer.launch({
    headless: true,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage'
    ]
  })

  try {
    const page = await browser.newPage()
    await page.goto('https://www.yahoo.co.jp/')
    await page.screenshot({ path: 'yahoo.png', fullPage: true })
  } catch (e) {
    console.error(e)
  } finally {
    browser.close()
  }
})()

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3