Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running node from php via exec #1789

Closed
aborzanovic opened this issue Sep 9, 2018 · 7 comments
Closed

Running node from php via exec #1789

aborzanovic opened this issue Sep 9, 2018 · 7 comments
Labels

Comments

@aborzanovic
Copy link

Info:

  • Docker version ($ docker --version): 18.06.1-ce, build e68fc7a
  • Laradock commit ($ git rev-parse HEAD): 85c57a0
  • System info (Mac, PC, Linux): linux
  • System info disto/version: Ubuntu 16.04 LTS

Is it possible to run a node script from php via exec() or something?
Node is installed in the workspace container so it's not accessible from php-fpm and even if I install it there as well, I still can't access it from web php as www-data user.
I've tried installing node as www-data in php-fpm (unsuccessfully).
Maybe there's an easier way that I'm just not seeing?

@MoogyG
Copy link

MoogyG commented Sep 10, 2018

I resolved this by using Task Scheduling.

@vv12131415
Copy link
Contributor

@dehumanise you have said that you can't run node script, but you don't say anything about the error you had. Can you please add some stack trace or something like that so we can help you more.
Is it permissions issue? Is it an issue that php-fpm can't find node executable?
Sorry, but it's not clear for me.

Also there is a table

type container
job (which is internally an artisan command) php-worker
php-fpm web
artisan command workspace

@sysoce
Copy link

sysoce commented Feb 11, 2019

PROBLEM:
If you get the error:
/usr/bin/env: 'node': No such file or directory
when trying to run a node script from the web app, it is due to that node is not installed in the container running the web app (php-fpm).

FIX:
Install node in the php-fpm container by copying the following slightly modified script found in the workspace/Dockerfile to the php-fpm/Dockerfile which will add a non-root user node and install node with npm. And rebuild the php-fpm image with docker-compose build php-fpm. Then recreate the container with docker-compose up -d.

###########################################################################
# Laradock non-root user:
###########################################################################

# Add a non-root user to prevent files being created with root permissions on host machine.
ARG PUID=1001
ENV PUID ${PUID}
ARG PGID=1001
ENV PGID ${PGID}

# always run apt update when start and after add new source list, then clean up at end.
RUN apt-get update -yqq && \
    pecl channel-update pecl.php.net && \
    groupadd -g ${PGID} node && \
    useradd -u ${PUID} -g node -m node && \
    usermod -p "*" node

###########################################################################
# Node / NVM:
###########################################################################

USER node

# Check if NVM needs to be installed
ARG NODE_VERSION=node
ENV NODE_VERSION ${NODE_VERSION}
ARG INSTALL_NODE=true
ARG INSTALL_NPM_GULP=false
ARG INSTALL_NPM_BOWER=false
ARG INSTALL_NPM_VUE_CLI=false
ARG NPM_REGISTRY
ENV NPM_REGISTRY ${NPM_REGISTRY}
ENV NVM_DIR /home/node/.nvm

RUN if [ ${INSTALL_NODE} = true ]; then \
    # Install nvm (A Node Version Manager)
    mkdir -p $NVM_DIR && \
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash \
        && . $NVM_DIR/nvm.sh \
        && nvm install ${NODE_VERSION} \
        && nvm use ${NODE_VERSION} \
        && nvm alias ${NODE_VERSION} \
        && if [ ${NPM_REGISTRY} ]; then \
        npm config set registry ${NPM_REGISTRY} \
        ;fi \
        && if [ ${INSTALL_NPM_GULP} = true ]; then \
        npm install -g gulp \
        ;fi \
        && if [ ${INSTALL_NPM_BOWER} = true ]; then \
        npm install -g bower \
        ;fi \
        && if [ ${INSTALL_NPM_VUE_CLI} = true ]; then \
        npm install -g @vue/cli \
        ;fi \
        && ln -s `npm bin --global` /home/node/.node-bin \
;fi

# Wouldn't execute when added to the RUN statement in the above block
# Source NVM when loading bash since ~/.profile isn't loaded on non-login shell
RUN if [ ${INSTALL_NODE} = true ]; then \
    echo "" >> ~/.bashrc && \
    echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc && \
    echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> ~/.bashrc \
;fi

# Add NVM binaries to root's .bashrc
USER root

RUN if [ ${INSTALL_NODE} = true ]; then \
    echo "" >> ~/.bashrc && \
    echo 'export NVM_DIR="/home/node/.nvm"' >> ~/.bashrc && \
    echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> ~/.bashrc \
;fi

# Add PATH for node
ENV PATH $PATH:/home/node/.node-bin

# Make it so the node modules can be executed with 'docker-compose exec'
# We'll create symbolic links into '/usr/local/bin'.
RUN if [ ${INSTALL_NODE} = true ]; then \
    find $NVM_DIR -type f -name node -exec ln -s {} /usr/local/bin/node \; && \
    NODE_MODS_DIR="$NVM_DIR/versions/node/$(node -v)/lib/node_modules" && \
    ln -s $NODE_MODS_DIR/bower/bin/bower /usr/local/bin/bower && \
    ln -s $NODE_MODS_DIR/gulp/bin/gulp.js /usr/local/bin/gulp && \
    ln -s $NODE_MODS_DIR/npm/bin/npm-cli.js /usr/local/bin/npm && \
    ln -s $NODE_MODS_DIR/npm/bin/npx-cli.js /usr/local/bin/npx && \
    ln -s $NODE_MODS_DIR/vue-cli/bin/vue /usr/local/bin/vue && \
    ln -s $NODE_MODS_DIR/vue-cli/bin/vue-init /usr/local/bin/vue-init && \
    ln -s $NODE_MODS_DIR/vue-cli/bin/vue-list /usr/local/bin/vue-list \
;fi

RUN if [ ${NPM_REGISTRY} ]; then \
    . ~/.bashrc && npm config set registry ${NPM_REGISTRY} \
;fi

You can also disable/enable node installation via the .env file the same way as in the workspace. Just add the following to the php-fpm: build: args: in your docker-compose.yml

- INSTALL_NODE=${PHP_FPM_INSTALL_NODE}
- NODE_VERSION=${PHP_FPM_NODE_VERSION}

Note that the docker_env group is not added to the node user because there is no such group in the php-fpm container. I do not know what consequences this may have so I would appreciate if someone would shed some light.

@stale
Copy link

stale bot commented Feb 2, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale label Feb 2, 2020
@stale
Copy link

stale bot commented Feb 23, 2020

Hi again 👋 we would like to inform you that this issue has been automatically closed 🔒 because it had not recent activity during the stale period. We really really appreciate your contributions, and looking forward for more in the future 🎈.

@vbaseghyanupwork
Copy link

PROBLEM:
If you get the error:
/usr/bin/env: 'node': No such file or directory
when trying to run a node script from the web app, it is due to that node is not installed in the container running the web app (php-fpm).

FIX:
Install node in the php-fpm container by copying the following slightly modified script found in the workspace/Dockerfile to the php-fpm/Dockerfile which will add a non-root user node and install node with npm. And rebuild the php-fpm image with docker-compose build php-fpm. Then recreate the container with docker-compose up -d.

###########################################################################
# Laradock non-root user:
###########################################################################

# Add a non-root user to prevent files being created with root permissions on host machine.
ARG PUID=1001
ENV PUID ${PUID}
ARG PGID=1001
ENV PGID ${PGID}

# always run apt update when start and after add new source list, then clean up at end.
RUN apt-get update -yqq && \
    pecl channel-update pecl.php.net && \
    groupadd -g ${PGID} node && \
    useradd -u ${PUID} -g node -m node && \
    usermod -p "*" node

###########################################################################
# Node / NVM:
###########################################################################

USER node

# Check if NVM needs to be installed
ARG NODE_VERSION=node
ENV NODE_VERSION ${NODE_VERSION}
ARG INSTALL_NODE=true
ARG INSTALL_NPM_GULP=false
ARG INSTALL_NPM_BOWER=false
ARG INSTALL_NPM_VUE_CLI=false
ARG NPM_REGISTRY
ENV NPM_REGISTRY ${NPM_REGISTRY}
ENV NVM_DIR /home/node/.nvm

RUN if [ ${INSTALL_NODE} = true ]; then \
    # Install nvm (A Node Version Manager)
    mkdir -p $NVM_DIR && \
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash \
        && . $NVM_DIR/nvm.sh \
        && nvm install ${NODE_VERSION} \
        && nvm use ${NODE_VERSION} \
        && nvm alias ${NODE_VERSION} \
        && if [ ${NPM_REGISTRY} ]; then \
        npm config set registry ${NPM_REGISTRY} \
        ;fi \
        && if [ ${INSTALL_NPM_GULP} = true ]; then \
        npm install -g gulp \
        ;fi \
        && if [ ${INSTALL_NPM_BOWER} = true ]; then \
        npm install -g bower \
        ;fi \
        && if [ ${INSTALL_NPM_VUE_CLI} = true ]; then \
        npm install -g @vue/cli \
        ;fi \
        && ln -s `npm bin --global` /home/node/.node-bin \
;fi

# Wouldn't execute when added to the RUN statement in the above block
# Source NVM when loading bash since ~/.profile isn't loaded on non-login shell
RUN if [ ${INSTALL_NODE} = true ]; then \
    echo "" >> ~/.bashrc && \
    echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc && \
    echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> ~/.bashrc \
;fi

# Add NVM binaries to root's .bashrc
USER root

RUN if [ ${INSTALL_NODE} = true ]; then \
    echo "" >> ~/.bashrc && \
    echo 'export NVM_DIR="/home/node/.nvm"' >> ~/.bashrc && \
    echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> ~/.bashrc \
;fi

# Add PATH for node
ENV PATH $PATH:/home/node/.node-bin

# Make it so the node modules can be executed with 'docker-compose exec'
# We'll create symbolic links into '/usr/local/bin'.
RUN if [ ${INSTALL_NODE} = true ]; then \
    find $NVM_DIR -type f -name node -exec ln -s {} /usr/local/bin/node \; && \
    NODE_MODS_DIR="$NVM_DIR/versions/node/$(node -v)/lib/node_modules" && \
    ln -s $NODE_MODS_DIR/bower/bin/bower /usr/local/bin/bower && \
    ln -s $NODE_MODS_DIR/gulp/bin/gulp.js /usr/local/bin/gulp && \
    ln -s $NODE_MODS_DIR/npm/bin/npm-cli.js /usr/local/bin/npm && \
    ln -s $NODE_MODS_DIR/npm/bin/npx-cli.js /usr/local/bin/npx && \
    ln -s $NODE_MODS_DIR/vue-cli/bin/vue /usr/local/bin/vue && \
    ln -s $NODE_MODS_DIR/vue-cli/bin/vue-init /usr/local/bin/vue-init && \
    ln -s $NODE_MODS_DIR/vue-cli/bin/vue-list /usr/local/bin/vue-list \
;fi

RUN if [ ${NPM_REGISTRY} ]; then \
    . ~/.bashrc && npm config set registry ${NPM_REGISTRY} \
;fi

You can also disable/enable node installation via the .env file the same way as in the workspace. Just add the following to the php-fpm: build: args: in your docker-compose.yml

- INSTALL_NODE=${PHP_FPM_INSTALL_NODE}
- NODE_VERSION=${PHP_FPM_NODE_VERSION}

Note that the docker_env group is not added to the node user because there is no such group in the php-fpm container. I do not know what consequences this may have so I would appreciate if someone would shed some light.

@sysoce I've tried this, but getting error upon php-fpm container re-building with --no-cache option.

#7 0.598 export NVM_DIR="$HOME/.nvm"
#7 0.598 [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
#7 0.598 [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion
#7 0.609 No .nvmrc file found
#7 0.611
#7 0.611 Node Version Manager
#7 0.611
#7 0.611 Note: refers to any version-like string nvm understands. This includes:
#7 0.611 - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
#7 0.611 - default (built-in) aliases: node, stable, unstable, iojs, system
#7 0.611 - custom aliases you define with nvm alias foo
#7 0.611
#7 0.611 Any options that produce colorized output should respect the --no-colors option.
#7 0.611
#7 0.611 Usage:
#7 0.611 nvm --help Show this message
#7 0.611 nvm --version Print out the installed version of nvm
#7 0.611 nvm install [-s] Download and install a , [-s] from source. Uses .nvmrc if available
#7 0.611 --reinstall-packages-from= When installing, reinstall packages installed in <node|iojs|node version number>
#7 0.611 --lts When installing, only select from LTS (long-term support) versions
#7 0.611 --lts= When installing, only select from versions for a specific LTS line
#7 0.611 --skip-default-packages When installing, skip the default-packages file if it exists
#7 0.611 --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version
#7 0.611 nvm uninstall Uninstall a version
#7 0.611 nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias lts/*, if available.
#7 0.612 nvm uninstall --lts= Uninstall using automatic alias for provided LTS line, if available.
#7 0.612 nvm use [--silent] Modify PATH to use . Uses .nvmrc if available
#7 0.612 --lts Uses automatic LTS (long-term support) alias lts/*, if available.
#7 0.612 --lts= Uses automatic alias for provided LTS line, if available.
#7 0.612 nvm exec [--silent] [] Run on . Uses .nvmrc if available
#7 0.612 --lts Uses automatic LTS (long-term support) alias lts/*, if available.
#7 0.612 --lts= Uses automatic alias for provided LTS line, if available.
#7 0.612 nvm run [--silent] [] Run node on with as arguments. Uses .nvmrc if available
#7 0.612 --lts Uses automatic LTS (long-term support) alias lts/*, if available.
#7 0.612 --lts= Uses automatic alias for provided LTS line, if available.
#7 0.612 nvm current Display currently activated version
#7 0.612 nvm ls List installed versions
#7 0.612 nvm ls List versions matching a given
#7 0.612 nvm ls-remote List remote versions available for install
#7 0.612 --lts When listing, only show LTS (long-term support) versions
#7 0.612 nvm ls-remote List remote versions available for install, matching a given
#7 0.612 --lts When listing, only show LTS (long-term support) versions
#7 0.612 --lts= When listing, only show versions for a specific LTS line
#7 0.612 nvm version Resolve the given description to a single local version
#7 0.612 nvm version-remote Resolve the given description to a single remote version
#7 0.612 --lts When listing, only select from LTS (long-term support) versions
#7 0.612 --lts= When listing, only select from versions for a specific LTS line
#7 0.612 nvm deactivate Undo effects of nvm on current shell
#7 0.612 nvm alias [] Show all aliases beginning with
#7 0.612 nvm alias Set an alias named pointing to
#7 0.612 nvm unalias Deletes the alias named
#7 0.612 nvm install-latest-npm Attempt to upgrade to the latest working npm on the current node version
#7 0.612 nvm reinstall-packages Reinstall global npm packages contained in to current version
#7 0.612 nvm unload Unload nvm from shell
#7 0.612 nvm which [current | ] Display path to installed node version. Uses .nvmrc if available
#7 0.612 nvm cache dir Display path to the cache directory for nvm
#7 0.612 nvm cache clear Empty cache directory for nvm
#7 0.612
#7 0.612 Example:
#7 0.612 nvm install 8.0.0 Install a specific version number
#7 0.612 nvm use 8.0 Use the latest available 8.0.x release
#7 0.612 nvm run 6.10.3 app.js Run app.js using node 6.10.3
#7 0.612 nvm exec 4.8.3 node app.js Run node app.js with the PATH pointing to node 4.8.3
#7 0.612 nvm alias default 8.1.0 Set default node version on a shell
#7 0.612 nvm alias default node Always default to the latest available node version on a shell
#7 0.612
#7 0.612 Note:
#7 0.612 to remove, delete, or uninstall nvm - just remove the $NVM_DIR folder (usually ~/.nvm)
#7 0.612
#7 ERROR: executor failed running [/bin/sh -c if [ ${INSTALL_NODE} = true ]; then mkdir -p $NVM_DIR && curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash && . $NVM_DIR/nvm.sh &amp;&amp; nvm install ${NODE_VERSION} && nvm use ${NODE_VERSION} && nvm alias ${NODE_VERSION} && if [ ${NPM_REGISTRY} ]; then npm config set registry ${NPM_REGISTRY} ;fi && ln -s npm bin --global /home/node/.node-bin ;fi]: exit code: 127


[ 4/74] RUN if [ true = true ]; then mkdir -p /home/node/.nvm && curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash && . /home/node/.nvm/nvm.sh && nvm install && nvm use && nvm alias && if [ ]; then npm config set registry ;fi && ln -s npm bin --global /home/node/.node-bin ;fi:


executor failed running [/bin/sh -c if [ ${INSTALL_NODE} = true ]; then mkdir -p $NVM_DIR && curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash && . $NVM_DIR/nvm.sh &amp;&amp; nvm install ${NODE_VERSION} && nvm use ${NODE_VERSION} && nvm alias ${NODE_VERSION} && if [ ${NPM_REGISTRY} ]; then npm config set registry ${NPM_REGISTRY} ;fi && ln -s npm bin --global /home/node/.node-bin ;fi]: exit code: 127
Service 'php-fpm' failed to build

Any Ideas how to resolve this ?

@litvinenkow
Copy link

you just need to add one line making a new directory like this

&& mkdir -p /home/node/.node-bin \

after

mkdir -p $NVM_DIR \

full listing that works below

###########################################################################
# Laradock non-root user:
###########################################################################

# Add a non-root user to prevent files being created with root permissions on host machine.
ARG PUID=1001
ENV PUID ${PUID}
ARG PGID=1001
ENV PGID ${PGID}

# always run apt update when start and after add new source list, then clean up at end.
RUN apt-get update -yqq && \
    pecl channel-update pecl.php.net && \
    groupadd -g ${PGID} node && \
    useradd -u ${PUID} -g node -m node && \
    usermod -p "*" node

###########################################################################
# Node / NVM:
###########################################################################

USER node

# Check if NVM needs to be installed
ARG NODE_VERSION=node
ENV NODE_VERSION ${NODE_VERSION}
ARG INSTALL_NODE=true
ARG INSTALL_NPM_GULP=false
ARG INSTALL_NPM_BOWER=false
ARG INSTALL_NPM_VUE_CLI=false
ARG NPM_REGISTRY
ENV NPM_REGISTRY ${NPM_REGISTRY}
ENV NVM_DIR /home/node/.nvm

RUN if [ ${INSTALL_NODE} = true ]; then \
    # Install nvm (A Node Version Manager)
    mkdir -p $NVM_DIR \
	&& mkdir -p /home/node/.node-bin \
	&& curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash \
	&& . $NVM_DIR/nvm.sh \
	&& nvm install ${NODE_VERSION} \
	&& nvm use ${NODE_VERSION} \
	&& nvm alias ${NODE_VERSION} \
	&& if [ ${NPM_REGISTRY} ]; then \
	npm config set registry ${NPM_REGISTRY} \
	;fi \
	&& if [ ${INSTALL_NPM_GULP} = true ]; then \
	npm install -g gulp \
	;fi \
	&& if [ ${INSTALL_NPM_BOWER} = true ]; then \
	npm install -g bower \
	;fi \
	&& if [ ${INSTALL_NPM_VUE_CLI} = true ]; then \
	npm install -g @vue/cli \
	;fi \
	&& ln -s `npm bin --global` /home/node/.node-bin \
;fi

RUN if [ ${NPM_REGISTRY} ]; then \
    . ~/.bashrc && npm config set registry ${NPM_REGISTRY} \
;fi

but this don't adds node to the path or /usr/bin i don't know why and you need to call it with this full string

/home/node/.node-bin/bin/node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants