Puppeteer on Docker M1 Mac fails to open new page (`Target.createTarget`)
Asked Answered
H

2

7

I'm unable to launch a new page using Puppeteer within Docker, on an M1 Mac.

Versions

Docker: version 20.10.11, build dea9396
Node: 16.13.1
MacOS: 12.0.1 (21A559)
Puppeteer: 13.5.1
M1 Chip

Error Message

ProtocolError: Protocol error (Target.createTarget): Target closed.
    at /home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:75:24
    at new Promise (<anonymous>)
    at Connection.send (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:71:16)
    at Browser._createPageInContext (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:261:53)
    at BrowserContext.newPage (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:520:30)
    at Browser.newPage (/home/node/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:254:37)
    at /home/node/app/src/test.js:17:19
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Script

const puppeteer = require('puppeteer');
(async function () {
  const browser = await puppeteer.launch({
    headless: true,
    args: [
      '--disable-gpu', // if i remove this, strangely the process exits unexpectedly
      '--disable-dev-shm-usage',
      '--no-sandbox',
      '--disable-setuid-sandbox',
    ],
  });
  await browser.newPage();
})();

Dockerfile (relevant info)

FROM --platform=linux/amd64 node:16.13.1-slim

# Server Dependencies (changed less often)
RUN apt-get update && \
    apt-get -y install vim xvfb gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 \
      libdbus-1-3 libexpat1 libfontconfig1 libgbm1 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

RUN apt-get install -y python make g++

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 /home/node/app

USER pptruser
❯ docker version
Client:
 Cloud integration: v1.0.22
 Version:           20.10.11
 API version:       1.41
 Go version:        go1.16.10
 Git commit:        dea9396
 Built:             Thu Nov 18 00:36:09 2021
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.11
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       847da18
  Built:            Thu Nov 18 00:34:44 2021
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
Heptane answered 20/12, 2021 at 21:49 Comment(2)
This not an reproducible example. Apparently you are running also npm install --save [email protected], but this command is not included in your Dockerfile in the question.Sociology
Could you provide also docker version, pls?Sociology
S
1

I know that I'm late to the party but this issue was getting me mad. After many hours of trial and error I managed to build a proper Dockerfile for using Puppeteer inside docker in my Apple Silicon M2: https://github.com/juananpe/puppeteer-in-docker-in-m1 Hope it helps.

Sophronia answered 7/12, 2022 at 11:25 Comment(4)
Today, you are my hero! It finally works, natively on ARMWarlike
Coming to this late, but where does example.png get saved to? I don't see it in ls -a, but the code executes with no errors. If I modify test.js to specify a path for example.png, Puppeteer fails with a "target closed" error. Running on Colima with x86_64 architecture and Rosetta.Buryat
@Buryat The screenshot is saved to /app/example.png (inside the container)gist.github.com/juananpe/fbd58094bfdae51392dca6ff6f8b7702Sophronia
@Sophronia Cool, I figured it was that directory, but there's no example.png in there in my container after running the code (which exits with a "Target closed" error). Thank you very much for clarifying. I think this could be caused by a regression on either Puppeteer 21 or Colima, so I'm raising issues about it with those repos.Buryat
S
0

node:16.13.1-slim available in linux/arm64/v8, just modify the line

FROM --platform=linux/amd64 node:16.13.1-slim

to

FROM --platform=linux/arm64/v8 node:16.13.1-slim

to test it out.

Scheld answered 27/12, 2021 at 22:13 Comment(2)
would this cause issues when running the image on non apple silicon machines?Heptane
> [ 8/18] RUN npm install --save [email protected]: #11 4.759 npm ERR! code 1 #11 4.759 npm ERR! path /tmp/node_modules/puppeteer #11 4.760 npm ERR! command failed #11 4.760 npm ERR! command sh -c node install.js #11 4.760 npm ERR! The chromium binary is not available for arm64.Heptane

© 2022 - 2024 — McMap. All rights reserved.