Zenika / alpine-chrome

Chrome Headless docker images built upon alpine official image
https://hub.docker.com/r/zenika/alpine-chrome
Apache License 2.0
1.83k stars 242 forks source link

Crashes on Apple Silicon #152

Closed jackHedaya closed 2 years ago

jackHedaya commented 3 years ago

Describe the bug On Apple Silicon, Chromium will crash on launch with the following message:

app_1 | Error: Protocol error (Target.setDiscoverTargets): Target closed.

app_1 | at /usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:71:63

app_1 | at new Promise (<anonymous>)

app_1 | at Connection.send (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:70:16)

app_1 | at Function.create (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Browser.js:95:26)

app_1 | at ChromeLauncher.launch (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/Launcher.js:101:56)

app_1 | at processTicksAndRejections (internal/process/task_queues.js:93:5)

To Reproduce

  1. Execute $ docker-compose up --build -d
  2. See error

What is the expected behavior? Chrome not to crash

What is the actual behavior? Chrome crashes

Possible solution Not sure –– hoping someone here might know what's going on

Logs If applicable, add logs to help explain your problem.

Versions

Additional context This happens very rarely when running the program locally. When running through the image, this happens every time.

jlandure commented 3 years ago

Hi @jackHedaya 👋

Thanks for the feedback. 👍

Can you share information about your docker-compose setup? What's the Docker command line or options used? Are you using the puppeteer example available here ?

Thanks!

jackHedaya commented 3 years ago

Thank you for reaching out and sure!

Dockerfile

FROM zenika/alpine-chrome:with-node

# Prevent yarn from reinstalling Chrome
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD 1
ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium-browser

# Set Node to production
ENV NODE_ENV production

# Set app working directory
WORKDIR /usr/src/app

# Copy dependency list and install
COPY --chown=chrome package.json yarn.lock ./
RUN yarn install --unsafe-perm=true --production --frozen-lockfile 

# Install pm2 globally as root then switch back
USER root
RUN npm install -g pm2
USER chrome

# Copy files to working directory
COPY --chown=chrome ./dist .

# Set up process manager and run Express
ENTRYPOINT [ "tini", "--" ]
CMD [ "pm2-runtime", "npm", "--", "start" ]

docker-compose.yml

version: '3'
services:
  app:
    build: .
    env_file:
      - .env
    ports:
      - '3001:3001'

Puppeteer Initializer

protected async createBrowser(
    args?: import('puppeteer').LaunchOptions
  ): Promise<import('puppeteer').Browser> {
    return puppeteer.launch({
      headless: true,
      args: ['--no-sandbox', '--disable-setuid-sandbox'],
      ...args,
    })
  }
jlandure commented 3 years ago

Hi @jackHedaya

Thanks for the example! 👍 Did you succeed on a classic machine? not an Apple Silicon?

jackHedaya commented 3 years ago

I've never encountered this problem before while using an Intel Mac

Joebayld commented 3 years ago

I have the same issue on apple silicon. Any news on this?

jlandure commented 3 years ago

Hi @jackHedaya @Joebayld 👋

Sorry but I didn't have a Apple Silicon. 😊

Have you seen this issue from the Puppeteer side? puppeteer issue: puppeteer/puppeteer#6622

Please notice that I don't know how Docker is working on Apple Silicon. It's a ARM based computer.

Perhaps you could make some tests to discover where is the problem...

  1. Does your code work well outside of this Docker image?
  2. Do you succeed to launch a simple Docker container like docker container run docker/whalesay cowsay Hello ?
Joebayld commented 3 years ago

I only have Apple Silicon so that's all I can test at the moment! Lol

The issue doesn't exist when running outside the docker image (npm start), and it doesn't exist when deploying to a Google Cloud Run server. So it seems to only be Docker on macOS AS.

I can run that example container just fine. And I can also run the alpine-chrome container as well - I just get an error when making calls to puppeteer in the container.

I'll poke the puppeteer folks as it might be an issue with the library - I'm not sure!

jlandure commented 3 years ago

Hi @Joebayld 👋

Thanks for the update! 🙌 Please keep me updated!

jlandure commented 3 years ago

Hi @Joebayld @jackHedaya 👋

Does it work now on Apple Silicon? 😊

jlandure commented 3 years ago

Any news @Joebayld @jackHedaya ? 😊 Thanks!

jackHedaya commented 3 years ago

Hi! As of now it is not working, I believe it may be a puppeteer problem though. It seems to be attached to this issue

jlandure commented 3 years ago

Hi @jackHedaya

Thanks for the update! 👍

vityas-off commented 3 years ago

https://github.com/puppeteer/puppeteer/issues/1947#issuecomment-815808274 helped me solve this issue

drpayyne commented 3 years ago

Hi @jlandure, are there any plans to publish a multi-arch image on Docker Hub for with-puppeteer?

ssoper commented 3 years ago

I was running into crashes using my new MacBook M1 Air whereas my previous Intel MacBook had no issues. Ended up building my own version of this with an arm64v8 alpine image and my crash issue was resolved. You can find the image at avidtraveler/alpine-chrome.

drpayyne commented 3 years ago

That's great, @ssoper! Could you please publish your customized Dockerfile and its context files if possible?

jlandure commented 3 years ago

Hi @ssoper 👋

Thanks for the update! And congrats to make it working with M1.

Do you think you can contribute to this image by adding your arm64v8 version? 👍 It could be interesting for @drpayyne and the rest of the community 😊

drpayyne commented 3 years ago

Hi @jlandure, what's the image build process that you use? I'd love to contribute to making this repo multi-arch as well :)

jlandure commented 3 years ago

Hi @drpayyne

Thanks for your motivation 💪

Very simple: the build process is done using Docker Hub AutoBuild.

💬 If you're interested, please create a PR and we will discuss about making the repo multi-arch!

ssoper commented 3 years ago

@drpayyne @jlandure the only difference is the very first line FROM arm64v8/alpine:latest, everything else in the Dockerfile is identical.

Here is my fork, lmk what next steps are, happy to help out.

I am running the ARM native version of Docker Desktop v20.10.8 on Big Sur.

drpayyne commented 3 years ago

Is there a reason we need to use that specific image? alpine:latest already provides multi-arch images at https://hub.docker.com/_/alpine?tab=tags&page=1&ordering=last_updated&name=latest.

I've been working on rebuilding existing images for both architectures so all that we need to do is rebuild our images for both platforms. We have to maybe customize the images a little, but I'll need to look into the specifics for this repository. Let me get back on this with a PR.

ssoper commented 3 years ago

@drpayyne when I specified the platform in the docker command to retrieve and run the image, it claimed there was no such platform.

docker: Error response from daemon: image with reference zenika/alpine-chrome was found but does not match the specified platform: wanted linux/arm64/v8, actual: linux/amd64.
drpayyne commented 3 years ago

Yes, you're right. This repository's images (zenika/*) do not provide ARM builds. But the source image you had mentioned that you modified isn't required is what I mean.

the only difference is the very first line FROM arm64v8/alpine:latest, everything else in the Dockerfile is identical.

So we can continue using alpine:latest as the source image and we have to build the same Dockerfile for multiple platforms. I use this command to build the Dockerfile for multiple platforms - docker buildx build --platforms=linux/arm64,linux/amd64 ..

So effectively we just need to rebuild the same Dockerfile for ARM.

Note: Some Dockerfiles here like puppeteer need some customization which I'll be looking into once I fix my PHP rebuild :)

drpayyne commented 3 years ago

@ssoper - I've just rebuilt the base image of this repository for both architecture at https://github.com/drpayyne/chrome/pkgs/container/chrome. All users can pull this tag and it'll download the appropriate architecture.

ssoper commented 3 years ago

@drpayyne did it resolve your crash issue?

drpayyne commented 3 years ago

I'm not sure as to what exactly is causing the crash reported here, but you could test it out by running docker run -it --rm ghcr.io/drpayyne/chrome bash. Any feedback is appreciated! I'm currently building the rest of the images like Node and Puppeteer, so I'll test them all together.

ssoper commented 3 years ago

the crashes I was seeing locally were related to being unable to parse /proc/self/maps along with a host of GPU errors.

drpayyne commented 3 years ago

Any specific commands that I need to test? @ssoper

drpayyne commented 3 years ago

I'm publishing the multi-arch packages at https://github.com/drpayyne?tab=packages&repo_name=chrome. Please test anything you can

ssoper commented 3 years ago

@drpayyne ran your image against my complex series of browser interactions (primarily with the E*TRADE website) and it worked perfect 💯

drpayyne commented 3 years ago

That's awesome! Let me check if all the Dockerfiles in this repo build, and then I'll create a PR

cc/ @jlandure

drpayyne commented 3 years ago

@ssoper, I'm getting the below error if I run the base chrome image. Any idea what's causing this in my machine since it seems to work for you? I'm seeing a few answers online, but not sure which is the best approach to fix this.

docker run -it --rm ghcr.io/drpayyne/chrome bash

Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
[0907/154002.918286:FATAL:zygote_host_impl_linux.cc(191)] Check failed: ReceiveFixedMessage(fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid).
Received signal 6
[end of stack trace]
ssoper commented 3 years ago

might come down to what arguments you are launching with, here is my run command. could also depend on what version of Docker you’re running (I noted in a prev comment what I’m running)

docker container run -d -p 9222:9222 ghcr.io/drpayyne/chrome --no-sandbox --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" about:blank
drpayyne commented 3 years ago

Ah! Thank you. Just found https://github.com/Zenika/alpine-chrome/issues/33 too. My bad, apologies. My multi-arch image builds work good in that case.

ssoper commented 3 years ago

great to hear @drpayyne! @jlandure what work remains so that we can pull zenika/alpine-chrome with a --platform linux/arm64/v8 switch to get the image we need?

drpayyne commented 3 years ago

Discussion PR opened at https://github.com/Zenika/alpine-chrome/pull/182.