Steveorevo / node-red-contrib-nbrowser

Provides a virtual web browser (a.k.a. "headless browser") appearing as a node.
34 stars 13 forks source link

run it in node-red docker? #5

Open d0uub opened 6 years ago

d0uub commented 6 years ago

can run it in node-red docker? i installed but nothing come out (error message or something), of course node-red docker does not have GUI to show the browser so i dont know any status inside

Steveorevo commented 6 years ago

The system must be capable of running the NightmareJS engine based on Electron. To do this, the docker container would need additional libraries to create enough of a "window manager" to run "headless". Specifically Xvfb. More information can be found here in this closed issue:

https://github.com/segmentio/nightmare/issues/224

Steveorevo commented 6 years ago

Confirmed, Xvfb resolves this issue. Closing.

philipsd6 commented 6 years ago

@Steveorevo, can you show how you confirmed this in node-red-docker? I'm not clear on how to adjust my Dockerfile to run both Xvfb and node-red in a way that keeps it up and running for multiple flows.

Steveorevo commented 6 years ago

Sorry @philipsd6. That machine has since been recycled. I'll re-open this ticket if anyone has the resources (time & hardware) to provide a step by step.

philipsd6 commented 6 years ago

I got it working, but it was a chore. After many iterations, here's what I came up with. I created a files/entrypoint script:

#!/usr/bin/env bash
set -e

# Start Xvfb
Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
export DISPLAY=:9.0

exec "$@"

Then I created a Dockerfile:

FROM nodered/node-red-docker

# Switch back to root user to install packages and configure entrypoint
USER root
RUN apt-get update && apt-get install -y \
    xvfb \
    x11-xkb-utils \
    xfonts-100dpi \
    xfonts-75dpi \
    xfonts-scalable \
    xfonts-cyrillic \
    x11-apps \
    clang \
    libdbus-1-dev \
    libgtk2.0-dev \
    libnotify-dev \
    libgnome-keyring-dev \
    libgconf2-dev \
    libasound2-dev \
    libcap-dev \
    libcups2-dev \
    libxtst-dev \
    libxss1 \
    libnss3-dev \
    gcc-multilib \
    g++-multilib \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY files/entrypoint /
RUN chmod +x /entrypoint
ENTRYPOINT ["/entrypoint"]

RUN mkdir -m 1777 /tmp/.X11-unix

# Switch back to node-red user to install the nbrowser module and run as non-root user
USER node-red
RUN npm install node-red-contrib-nbrowser

ENV DEBUG=nightmare
CMD ["npm", "start", "--", "--userDir", "/data"]

I wasn't happy with how much I had to override from the nodered-docker base image -- it's almost more worthwhile to just create the Dockerfile from scratch.

mukowman commented 6 years ago

Tried the above dockerfile but was still not able to get it to work. https://hub.docker.com/r/mukowman/nodered-nbrowser-docker/ Get the following error in Docker Log

Thu, 17 May 2018 02:12:55 GMT nightmare queuing process start Thu, 17 May 2018 02:12:55 GMT nightmare queueing child action addition for "downloadManager" Thu, 17 May 2018 02:12:55 GMT nightmare queueing child action addition for "upload" Thu, 17 May 2018 02:12:55 GMT nightmare running Thu, 17 May 2018 02:12:55 GMT nightmare electron child process exited with code 1: general error - you may need xvfb

Steveorevo commented 6 years ago

No question about it; if you don't have a window manager you'll need xvfb. See the error message.

stephenhouser commented 5 years ago

On macOS, with XQuartz installed I have been able to Show browser window instance? and run headless by modifying @philipsd6 's entrypoint as follows:

#!/usr/bin/env bash
set -e

# Start Xvfb if no DISPLAY
if [ -z ${DISPLAY+x} ]
then
    Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
    export DISPLAY=:9.0
fi

exec "$@"

To run headless I use the following script:

#!/bin/bash
export DISPLAY=
docker run \
    -p 1880:1880 \
    nb-docker

To run and send output to XQuartz, using help from Fredrick Averpil I use the following startup script:

#!/bin/bash
ip=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
xhost + $ip
docker run -it \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -e DISPLAY=$ip:0 \
    -p 1880:1880 \
    nb-docker

NOTE: I named my built docker image nb-docker in these examples.

GitHub Gist with all this -- NBrowser-docker