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?
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
app:
restart: always
build: ${REPO}
volumes:
- ${REPO}/code:/usr/src/app:ro
working_dir: /usr/src/app
ports:
- "8087:5000"
app.post('/img', function (req, res) {
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
})();
});
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.
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.
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
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.
.
├── 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
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
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
.
├── 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With