No access permission error with npm global install on docker image

前端 未结 5 1819
小鲜肉
小鲜肉 2021-01-31 09:19

I\'m trying to build a docker image with a global install of firebase-tools and angular-cli. I\'m building the same image for two versions of node: 6.x (LTS bor

相关标签:
5条回答
  • 2021-01-31 09:22

    I was able to get it working by changing the default npm-global directory.

    This is my dockerfile now:

    FROM node:latest
    USER node
    
    RUN mkdir /home/node/.npm-global
    ENV PATH=/home/node/.npm-global/bin:$PATH
    ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
    
    RUN npm install --quiet --no-progress -g @angular/cli@latest firebase-tools
    RUN npm cache clean --force
    
    0 讨论(0)
  • 2021-01-31 09:25

    Use the --unsafe-perm flag:

    npm install --quiet --no-progress --unsafe-perm -g @angular/cli@latest firebase-tools
    

    I think that's still better than setting the npm user permanently to root. You can use --unsafe-perm only with the packages which cause problem

    0 讨论(0)
  • 2021-01-31 09:34

    instead of forcing NPM's hand to install the package inside the container I bypassed this issue by mapping/referncing a host folder for the missing module this also prevents future headaches for when I replace the docker image with the latest version, I won't have to repeat the stages to reinstall the missing module inside the container:

    the steps i took:

    1. create an empty folder on the host environment (will be used as a target for the node js modules). call it node_modules

    2. when launching running the docker container use the --volume and --env switches

    -- volume to pass map the new host folder (from step 1) to a folder accessible inside docker

    --env to define/set an environment variable NPM_CONFIG_PREFIX from inside the docker to the /node_modules folder we created in step 1

    1. access the container using :

      sudo docker exec -i -t sh

    and go to the folder above the folder right above the local /node_modules folder (this not the folder we mapped to the environment variable, but rather the preexisting folder that came with the docker image)

    then run the command:

    > npm install -g <mdule-name>
    

    example

    > npm install -g request
    

    this will install the module in the host folder we've created. this module will also be accessible from both docker and host.

    0 讨论(0)
  • 2021-01-31 09:35

    The problem is because while NPM runs globally installed module scripts as the nobody user, which kinds of makes sense, recent versions of NPM started setting the file permissions for node modules to root. As a result module scripts are no longer allowed to create files and directories in their module.

    See discussion in NPM issue #3849, for some references.

    A simple workaround, which makes sense in a docker environment, is to set the NPM default global user back to root, like so:

    npm -g config set user root
    

    After which you shouldn't have any more EACCES errors.

    0 讨论(0)
  • 2021-01-31 09:38

    Please DO NOT set the user to root or use --unsafe-perm.

    Simply use the node user, provided with the current official (e.g. alpine) images.

    Commented Dockerfile below:

    FROM node:15.5-alpine
    
    #                                                                     
    0 讨论(0)
提交回复
热议问题