From 98ecb0ea09c329f3c94ad072f8224fdd0d753bee Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Fri, 16 Mar 2018 17:10:18 +1100 Subject: Remove the custom user and add example usage --- Dockerfile | 61 +++++++++++++------------------------------- README.md | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 94 insertions(+), 52 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0e46b41..eb38190 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,47 +1,20 @@ -# Copy and pasted from: -# https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-in-docker +# A minimal Docker image with Node 8 and Puppeteer # -# The only change is locking it down to a specific version of Puppeteer in the -# `npm install` line. +# Based upon: +# https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-in-docker FROM node:8-slim - -# See https://crbug.com/795759 -RUN apt-get update && apt-get install -yq libgconf-2-4 - -# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others) -# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer -# installs, work. -RUN apt-get update && apt-get install -y wget --no-install-recommends \ - && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ - && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \ - && apt-get update \ - && apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \ - --no-install-recommends \ - && rm -rf /var/lib/apt/lists/* \ - && apt-get purge --auto-remove -y curl \ - && rm -rf /src/*.deb - -# It's a good idea to use dumb-init to help prevent zombie chrome processes. -ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-init_1.2.0_amd64 /usr/local/bin/dumb-init -RUN chmod +x /usr/local/bin/dumb-init - -# Uncomment to skip the chromium download when installing puppeteer. If you do, -# you'll need to launch puppeteer with: -# browser.launch({executablePath: 'google-chrome-unstable'}) -# ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true - -# Install puppeteer so it's available in the container. -RUN npm i puppeteer@1.1.1 - -# Add user so we don't need --no-sandbox. -RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \ - && mkdir -p /home/pptruser/Downloads \ - && chown -R pptruser:pptruser /home/pptruser \ - && chown -R pptruser:pptruser /node_modules - -# Run everything after as non-privileged user. -USER pptruser - -ENTRYPOINT ["dumb-init", "--"] -CMD ["google-chrome-unstable"] \ No newline at end of file + +RUN apt-get update \ + # See https://crbug.com/795759 + && apt-get install -yq libgconf-2-4 \ + # Install latest chrome dev package, which installs the necessary libs to + # make the bundled version of Chromium that Puppeteer installs work. + && apt-get install -y wget --no-install-recommends \ + && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ + && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \ + && apt-get update \ + && apt-get install -y google-chrome-unstable --no-install-recommends \ + && rm -rf /var/lib/apt/lists/* \ + # Finally, install Puppeteer under /node_modules so it's available system-wide + && npm i puppeteer@1.1.1 \ No newline at end of file diff --git a/README.md b/README.md index 114bba8..5e308e8 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,86 @@ # Docker Puppeteer -A Puppeteer Docker image based on [Puppeteer’s own recommendations](https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-in-docker). +Node 8 + Puppeteer docker base image. Use it to add your own tools (such as a testing framework), configure any linked containers via your Docker Compose config, and run your Puppeteer scripts. -An example for how to use: +## Usage example + +Dockerfile.integration-tests: ```Dockerfile -FROM buildkite/puppeteer +FROM buildkite/puppeteer:1.1.1 +RUN npm i mocha@5 +ENV PATH="${PATH}:/node_modules/.bin" +``` + +docker-compose.integration-tests.yml: + +```yml +version: '3' +services: + tests: + build: + context: . + dockerfile: Dockerfile.integration-tests + volumes: + - "./integration-tests:/integration-tests" + - "/screenshots" + command: mocha --recursive /integration-tests + links: + - app + app: + image: tutum/hello-world + expose: + - "80" +``` + +integration-tests/index.test.js: + +```js +const assert = require('assert') +const puppeteer = require('puppeteer') -RUN npm install mocha -ENV PATH="${PATH}:/node_modules/.bin" +let browser +let page + +before(async() => { + browser = await puppeteer.launch({ + args: [ + // Required for Docker version of Puppeteer + '--no-sandbox', + '--disable-setuid-sandbox', + // This will write shared memory files into /tmp instead of /dev/shm, + // because Docker’s default for /dev/shm is 64MB + '--disable-dev-shm-usage' + ] + }) + + const browserVersion = await browser.version() + console.log(`Started ${browserVersion}`) +}) + +beforeEach(async() => { + page = await browser.newPage() +}) + +afterEach(async() => { + await page.close() +}) + +after(async() => { + await browser.close() +}) + +describe('App', () => { + it('renders', async() => { + const response = await page.goto('http://app/') + assert(response.ok()) + await page.screenshot({ path: `/screenshots/app.png` }) + }) +}) +``` -# The `tests` dir is expected to be mounted into here -WORK_DIR /home/pptr +Running: -CMD ["mocha", "--recursive", "tests"] ``` +docker-compose -f integration-tests run tests +``` \ No newline at end of file -- cgit v1.2.3