Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer in docker container: Chromium revision is not downloaded

I'm trying to launch puppeteer in an express app that's run in a docker container, using docker-compose.

The line that should launch puppeteer const browser = await puppeteer.launch({args: ['--no-sandbox']}); throws the following error:

(node:28) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError [ERR_ASSERTION]: Chromium revision is not downloaded. Run "npm install"

I've tried adding a yarn add puppeteer after the yarn install and also replacing yarn install in the Dockerfile with npm install .

What needs to change, so that I can use puppeteer with chromium as expected?

Express app's Dockerfile:

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

docker-compose.yml:

app:
    restart: always
    build: ${REPO}
    volumes:
        - ${REPO}/code:/usr/src/app:ro
    working_dir: /usr/src/app
    ports:
        - "8087:5000"

index.js route:

app.post('/img', function (req, res) {
  const puppeteer = require('puppeteer');
  (async() => {
    const browser = await puppeteer.launch({args: ['--no-sandbox']});
  })();
});
like image 743
ebbishop Avatar asked Jan 10 '18 17:01

ebbishop


People also ask

How do I start Docker chromium?

Launch from KDE Start Menu: Internet -> Chromium Docker, and after a terminal window flashes on screen and a few seconds pass, Docker will launch with Chromium Media Edition within it.

How do I run a Docker command in VS Code?

You can add Docker files to your workspace by opening the Command Palette (Ctrl+Shift+P) and using Docker: Add Docker Files to Workspace command. The command will generate Dockerfile and . dockerignore files and add them to your workspace.


2 Answers

I ran into this problem and I wanted to leave the simple solution. The reason it couldn't find my chrome install is that I had mounted my local volume into the container for testing. I use a mac, so the local npm install gave me the mac version of chromium. When that node_modules folder was mounted into the linux container, it expected to find the linux version which was not there.

To get around this, you need to exclude your node_modules folder when you're doing a volume mount into the container. I was able to do that by passing another volume parameter.

docker run -rm \
  --volume ~/$project:/dist/$project \
  --volume /dist/$project/node_modules
like image 95
Marco Avatar answered Oct 14 '22 17:10

Marco


The docker volume I was using mapped the entire local code directory to the docker container's /usr/src/app directory.

This is great for allowing quick code updates during development.

However, it also overwrites the version of chromium previously installed on the docker container via the yarn install in the Dockerfile with the version of chromium installed on my machine via yarn install in the command line.

Each machine needs its own, correct, os-specific version of chromium. The docker container needs a linux-specific chromium (linux-515411), my laptop needs a mac-specific chromium (mac-508693). Simply running yarn install (or npm install) with puppeteer in your package.json will handle installing the correct version of chromium.

Previous project structure:

.
├── 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

Partial Dockerfile

This is where the container gets its own version of .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

Previous volumes from docker-compose.yml

This copies everything from the local ${REPO}/code to the docker container's /usr/src/app directory. Including the wrong version of chromium.

volumes:
    - ${REPO}/code:/usr/src/app:ro

Updated project structure:

.
├── 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

Updated docker volume maps the entire contents of the local ./code/src to the docker container's /usr/src/app. This does NOT include the node_modules directory:

volumes:
    - ${REPO}/code/src:/usr/src/app:ro
like image 32
ebbishop Avatar answered Oct 14 '22 16:10

ebbishop