Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install global module in docker?

Tags:

npm

docker

Getting Error: EACCES: permission denied, open '/usr/local/lib/node_modules when trying to install a global module in docker:

FROM node:latest
RUN mkdir -p /code
RUN npm i -g npm
WORKDIR /code

RUN npm set progress=false &&  npm install -g exp

There is no information about it in the official docs for node or in https://forums.docker.com/

like image 472
locropulenton Avatar asked Sep 08 '17 08:09

locropulenton


2 Answers

Per the current docker-node documentation, you can set the location of the global npm dependencies to a user-writeable location by adding these lines to the Dockerfile:

ENV NPM_CONFIG_PREFIX=/home/node/.npm-global

# optionally if you want to run npm global bin without specifying path
ENV PATH=$PATH:/home/node/.npm-global/bin
like image 77
tephyr Avatar answered Oct 26 '22 17:10

tephyr


Npm does not allow running as root by default for security reasons. When you run npm as root (this is the default user in Docker build) and install a global package, npm installs and executes binaries as user nobody instead, who doesn't have any permissions.

You can avoid this by adding the --unsafe-perm flag:

RUN npm install --global --unsafe-perm exp

or by setting the global user explicitly to root:

RUN npm --global config set user root && \
    npm --global install exp

source

or by switching to a non-root USER during docker build. Be aware that this will affect ownership of files in your container.

The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.

like image 36
Blaise Avatar answered Oct 26 '22 18:10

Blaise