Search code examples
node.jslinuxdockerdockerfiledebian

www-data user cant access node inside of docker container terminal


I installed node and npm using nvm in my Dockerfile when running the build method everything runs smoothly with no problems going inside of my docker container terminal though and trying to access node, npm or nvm I get a error message that neither node npm or nvm to be found.

At first I thought its something wrong with the way I am downloading it but after some testing I found out that I can access node only with other users such as root or my other user not with www-data

I tried these solutions and none of them worked:

  1. Changing the permissions of the files to 777, 755 or 640.
  2. Checked the users and group owners of the files and they are all set to www-data www-data .
  3. Added sudo to www-data

(I am using: nginx debian php8.2 and I am trying to download node 18 ) (inside of my shell I am using bin/bash and I am running the source /var/www/.bashrc source /etc/bash.bashrc)

RUN ls -la \
    && mkdir "/var/www/.nvm" \
    && export NVM_DIR="/var/www/.nvm" \
    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \
    && [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" \
    && [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  \
    && nvm install 18 \
    && nvm use 18 && npm install -g yarn \
    && nvm use 18 \
    && nvm alias default 18

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/v$NODE_VERSION/bin:$PATH

Solution

  • I fixed the problem where I inserted a soft link inside of the /usr/local/bin that leads to the node and npm inside of the : /var/www/.nvm/versions/node/myNodeVersion/bin/ with this I was able to access the npm and node inside of the linux shell system (POSIX).

    and I inserted the code that creates the soft links inside of my entrypoint shell:

    #!/bin/bash
    
    
    set -e
    
    source /var/www/.bashrc
    
    export BASH_ENV=/var/www/.bashrc
    
    if [[ ! -z "$NODE_VERSION" ]]; then
       nvm alias default ${NODE_VERSION}
       sudo rm -f /usr/local/bin/node
       sudo rm -f /usr/local/bin/npm
       sudo ln -s "$(which node)" "/usr/local/bin/node"
       sudo ln -s "$(which npm)" "/usr/local/bin/npm"
    fi