No access permission error with npm global install on docker image - docker

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 boron) and v8.x (latest alpine). Locally, both images build fine, but when I try to build in docker hub only the v6.x builds successfully. With the v8.x it gets trapped in the access permissions for undefined and nobody user. I'm already using the root user (USER root), since without this setting (or using USER node) both images fail to build.
This is my Dockerfile:
FROM node:latest
USER root
RUN npm install --quiet --no-progress -g #angular/cli#latest firebase-tools
RUN npm cache clean --force
And this is the output:
Step 4/5 : RUN npm install --quiet --no-progress -g #angular/cli#latest firebase-tools
---> Running in fce3da11b04e
npm WARN deprecated node-uuid#1.4.8: Use uuid module instead
/usr/local/bin/firebase -> /usr/local/lib/node_modules/firebase-tools/bin/firebase
/usr/local/bin/ng -> /usr/local/lib/node_modules/#angular/cli/bin/ng
> node-sass#4.5.3 install /usr/local/lib/node_modules/#angular/cli/node_modules/node-sass
> node scripts/install.js
Unable to save binary /usr/local/lib/node_modules/#angular/cli/node_modules/node-sass/vendor/linux-x64-57 : { Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/#angular/cli/node_modules/node-sass/vendor'
at Object.fs.mkdirSync (fs.js:890:18)
at sync (/usr/local/lib/node_modules/#angular/cli/node_modules/mkdirp/index.js:71:13)
at Function.sync (/usr/local/lib/node_modules/#angular/cli/node_modules/mkdirp/index.js:77:24)
at checkAndDownloadBinary (/usr/local/lib/node_modules/#angular/cli/node_modules/node-sass/scripts/install.js:111:11)
at Object.<anonymous> (/usr/local/lib/node_modules/#angular/cli/node_modules/node-sass/scripts/install.js:154:1)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
errno: -13,
code: 'EACCES',
syscall: 'mkdir',
path: '/usr/local/lib/node_modules/#angular/cli/node_modules/node-sass/vendor' }
> grpc#1.3.8 install /usr/local/lib/node_modules/firebase-tools/node_modules/grpc > node-pre-gyp install --fallback-to-build --library=static_library
node-pre-gyp ERR! Tried to download(undefined): https://storage.googleapis.com/grpc-precompiled-binaries/node/grpc/v1.3.8/node-v57-linux-x64.tar.gz node-pre-gyp ERR! Pre-built binaries not found for grpc#1.3.8 and node#8.1.2 (node-v57 ABI) (falling back to source compile with node-gyp)
gyp WARN EACCES user "undefined" does not have permission to access the dev dir "/root/.node-gyp/8.1.2" gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp"
gyp WARN EACCES user "nobody" does not have permission to access the dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp/8.1.2" gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp"
gyp WARN EACCES user "nobody" does not have permission to access the dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp/8.1.2" gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp"
gyp WARN EACCES user "nobody" does not have permission to access the dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp/8.1.2" gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp"
gyp WARN EACCES user "nobody" does not have permission to access the dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp/8.1.2" gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/firebase-tools/node_modules/grpc/.node-gyp"
(infinite loop)

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.

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
# 👉 Security: do not use the `root` user.
ENV USER=node
# You can not use `${USER}` here, but reference `/home/node`.
ENV PATH="/home/node/.npm-global/bin:${PATH}"
# 👉 The `--global` install dir
ENV NPM_CONFIG_PREFIX="/home/node/.npm-global"
# All subsequent commands are run as the `node` user.
USER "${USER}"
# Pre-create the target dir for global install.
RUN mkdir -p "${NPM_CONFIG_PREFIX}/lib"
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
# 👉 Configure NPM, so pkg get installed with correct credentials.
# Avoids `chmod u+x $DIR` and other workarounds.
RUN npm --global config set user "${USER}" \
&& npm --global --quiet --no-progress install \
&& npm cache clean --force
Extended version of #gabriel-araujo answer.
You can replace setting the user via the CLI flags (npm --global config set user "${USER}") by configuring this in the .npmrc file. Place user=node in the project roots .npmrc file or just set it directly from the Dockerfile.
RUN echo "user=node" > "${NPM_CONFIG_PREFIX}/etc/.npmrc"
If you use docker-compose.yml, you can add it as environment: - … variable.
Hope that helps you running a more safe NodeJS container somewhere and pull up some awesome stuff.

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

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

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:
create an empty folder on the host environment (will be used as a target for the node js modules). call it node_modules
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
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.

Related

`npm install --force` lockfile not being respected by dockerfile build?

This is confusing so I apologize if I don't word this sufficiently well.
Essentially, I'm leveraging npm's --force flag to bypass a conflicting peer-dependency error with npm#8. Subsequent npm install s of the dependencies complete without any errors. When attempting to install dependencies via docker, however, the original error returns.
So, originally:
encounter error:
npm ERR! ERESOLVE could not resolve
...
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
bypass via npm install --force
subsequent npm installs work without issue in new local environments (e.g. clone into new dir, run npm install)
However, attempting to npm install or npm ci (npm ci ensures a lockfile already exists) in a docker build continues throws the original error:
npm ERR! ERESOLVE could not resolve
...
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
Which, to me, suggests the lockfile isn't being respected. But we know it exists because otherwise npm ci would error.
Does anyone have an idea as to why this might be the case?
Dockerfile I'm testing with:
# // Dockerfile
# ==== CONFIGURE =====
# Use a Node 16 base image
FROM node:16-alpine
# Set the working directory to /app inside the container
WORKDIR /app
# Copy app files
COPY package-lock.json .
RUN echo $(ls)
# ==== BUILD =====
# Install dependencies (npm ci makes sure the exact versions in the lockfile gets installed)
RUN npm ci
# Build the app
RUN npm run build
# ==== RUN =======
# Set the env to "production"
ENV NODE_ENV production
# Expose the port on which the app will be running (3000 is the default that `serve` uses)
EXPOSE 3000
# Start the app
CMD [ "npx", "serve", "build" ]
Local npm is v8.1, docker npm is v8.19. Seems they introduced a breaking change at some point between those two versions.
From official docs:
NOTE: If you create your package-lock.json file by running npm install with flags that can affect the shape of your dependency tree, such as --legacy-peer-deps or --install-links, you must provide the same flags to npm ci or you are likely to encounter errors. An easy way to do this is to run, for example, npm config set legacy-peer-deps=true --location=project and commit the .npmrc file to your repo.

How to install #swc/core-linux-musl on windows, to make it work in docker container?

I'am working on windows. I docernize Next.js with Typescript app.
Here is my dockerfile:
FROM node:alpine
# create directory where our application will be run
RUN mkdir -p /usr/src
WORKDIR /usr/src
# copy our files into directory
COPY . /usr/src
# install dependences
RUN npm install
EXPOSE 3000
ENTRYPOINT ["npm", "run" ,"dev"]
During development I bind host catalogue to container by --mount type=bind,source=d:/apps/library.next.js/,target=/usr/src. When I start container I get error: error - Failed to load SWC binary for linux/x64, see more info here: https://nextjs.org/docs/messages/failed-loading-swc.
That's fine, I understand error and know what to do. To fix this I need to install #swc/cli #swc/core #swc/core-linux-musl, but I can't doing it because npm complain:
ERR! code EBADPLATFORM npm
ERR! notsup Unsupported platform for #swc/core-linux-musl#1.2.42: wanted {"os":"linux","arch":"x64"} (current: {"os":"win32","arch":"x64"})
How to install it on windows, or how to change docker setup to make it work? I must install it locally then it will be linked (by binding !) into container.
My workaround for now is to get into container by docker exec -it <id> /bin/sh then manually type npm install -save-dev #swc/cli #swc/core #swc/core-linux-musl. But doing that every time I recreate container is annoying.
The docs state: The -f or --force will force npm to fetch remote resources even if a local copy exists on disk. And it should be in the docs v6 legacy, the one you posted and v8 version. (See the section after --package-lock-only. It comes with an example npm install sax --force). So, you shouldn't have issues with that every time your container is recreating.

Maximum call stack size exceeded npm install by Docker container

I'm trying to npm install by Docker container:
This is a DockerFile:
# default /var/www/html (mapped to .../code folder with projects)
FROM node
WORKDIR /work
# Additional tools (ng, gulp, bower)
RUN npm install -g #angular/cli bower gulp grunt
CMD while true; do sleep 10000; done
EXPOSE 3002 3003 3004
I run and map it with this command:
docker run -d --name node-cmd -p 3002:3002 -p 3003:3003 -p 3004:3004 -v
/m/dockerlogs/node-cmd/logs:/root/.npm/_logs -v /m/projekty:/work node-cmd
I log in to this container with:
docker exec -it node-cmd bash -c "cd /code; bash"
After I run npm install (https://github.com/gdi2290/angular-starter), I write this from logged in container
But I'm getting this error after installation
npm ERR! Maximum call stack size exceeded
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-09-17T17_38_34_855Z-debug.log
root#08e3cd77fb83:/work/angular-starter-master#
I was try to delete node_modules, but this problem is always.
Sometimes, after this error, when I again try npm install, console show me this:
npm ERR! path /work/angular-starter-
master/node_modules/#schematics/update/packa
ge.json.2932816706
npm ERR! code ETXTBSY
npm ERR! errno -26
npm ERR! syscall rename
npm ERR! ETXTBSY: text file is busy, rename '/work/angular-starter-
master/node_m
odules/#schematics/update/package.json.2932816706' -> '/work/angular-
starter-mas
ter/node_modules/#schematics/update/package.json'
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-09-17T17_14_43_970Z-debug.log
root#08e3cd77fb83:/work/angular-starter-master#
My npm version is 6.4.1
I have a Windows 8.1 and Docker Toolbox
But when I write npm install on Windows without Docker all is OK.
It might be because of different node versions. Check your node version with node -v and use that for the Dockerfile. Check the supported tags and Dockerfile links here: https://hub.docker.com/_/node
In the Dockerfile, I changed from FROM node:alpine AS node_builder to FROM node:lts-alpine AS node_builder and now it works.
Ok, i solved this problem, I was use npm install with --no-bin-links flag. Thanks for answers :)
For me, this ended up being caused by permission issues (I had previously install node_modules outside of docker). Deleting node_modules and only installing from within docker corrected the problem.

How to install global npm dependencies in 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/
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.
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
This is an issue between npm and exp module. You can install the module using yarn instead
MODULE 72: Module._load REQUEST path parent: /usr/local/lib/node_modules/exp/node_modules/decompress-zip/lib/file-details.js
MODULE 72: load native module path
ngrok - error unpacking binary { Error: EACCES: permission denied, open '/usr/local/lib/node_modules/exp/node_modules/#expo/ngrok/bin/ngrok'
errno: -13,
code: 'EACCES',
syscall: 'open',
path: '/usr/local/lib/node_modules/exp/node_modules/#expo/ngrok/bin/ngrok' }
npm info lifecycle #expo/ngrok#2.2.8~postinstall: Failed to exec postinstall script
npm WARN react-redux#5.0.6 requires a peer of react#^0.14.0 || ^15.0.0-0 || ^16.0.0-0 but none was installed.
MODULE 38: Module._load REQUEST os parent: /usr/local/lib/node_modules/npm/lib/utils/error-handler.js
MODULE 38: load native module os
But you can install it using yarn
$ yarn global add exp
See below issue for more detail
https://github.com/expo/exp/issues/59
You can try login as node or root user before install command, add USER before RUN command:
USER node
or
USER root

Docker Container works locally but fails on server

I'm fairly new to docker and I'm kind of experimenting with Angular CLI app. I managed to run it locally through my docker container. It works great, but when I try running it from my server it fails.
Server is hosted on DigitalOcean:
512 MB Memory / 20 GB Disk / FRA1 - Ubuntu Docker 17.03.0-ce on 14.04
I used dockerhub to transfer my container to the server.
When logging the container it gives me this:
** NG Live Development Server is running on http://0.0.0.0:4200. **
63% building modules 469/527 modules 58 active ...s/#angular/compiler/src/assertions.jsKilled
npm info lifecycle angular-test#0.0.0~start: Failed to exec start script
npm ERR! Linux 4.4.0-64-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v6.10.3
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! angular-test#0.0.0 start: `ng serve --host 0.0.0.0`
npm ERR! Exit status 137
npm ERR!
npm ERR! Failed at the angular-test#0.0.0 start script 'ng serve --host 0.0.0.0'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the angular-test package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! ng serve --host 0.0.0.0
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs angular-test
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls angular-test
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /usr/src/app/npm-debug.log
Here is my Dockerfile:
# Create image based on the official Node 6 image from dockerhub
FROM node:6
# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app
# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app
# Copy dependency definitions
COPY package.json /usr/src/app
# Install dependecies
RUN npm install
# Get all the code needed to run the app
COPY . /usr/src/app
# Expose the port the app runs in
EXPOSE 4200
# Serve the app
CMD ["npm", "start"]
How come it runs locally and fails on server? Am I missing some dependencies?
ng serve is an angular-cli command. I'm guessing you need to install it globally in your docker file if you want to start your server like that on digital ocean:
RUN npm i -g angular-cli
I think it would be more typical to simply run the app using the naked node server in production. So your CMD would look more like this:
CMD ["node", "app.js"]

Resources