cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG]node:16-alpine3.16 and npm -v and other npm options return no output when using image in kubernetes

Open mrdeshapiro opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] I am using the latest npm

Current Behavior

Hello,

With docker image creation, I updated to use FROM node:16-alpine3.16 for the image and used that image with kubernetes, but now for some reason npm -v does not return information.

Any command options I try does nothing. It pauses for a few seconds like it is thinking and then drops down to the command prompt with no output. I can even put in fake options and it does the same thing.

It worked fine with our previous use of node:14:alpine.

Note: that /usr/src/app becomes readonly once the container is made and used in kubernetes.

Using the image outside of kubernetes still works fine. Is npm trying to write now to /usr/src/app? How do I address this? In that, if I make do a docker build and then try and connect to the container to get to a comand prompt, I can npm -v fine. It is when I use this image in kubernetes that I have the bad behavior.

Finding who can help with this is kind of unclear. I am not sure if maybe something is going on with the read-only filesystem once in kubernetes. Have you seen this issue and do you have a suggestion on what might be going on and how to fix? Is there a way to have some debugging when I run the npm command so that I can see what it is doing while it pauses before dropping down to the command prompt?

Sincerely,

David

Expected Behavior

I expect to see npm -v return a version

Steps To Reproduce

create a standard dockerfile that uses:

FROM node:16-alpine3.16 You can do the typical WORKDIR /usr/src/app and copy your package* and code into it I created a user with the same uid/gid I set as runasuser (1001)/runasgroup (3000) in my kubernetes deployment yaml have CMD "./loop.sh" so it just loops and does hello world message during this troubleshooting. It is actually supposed to launch a web page with:

EXPOSE 3000

CMD ["npm", "run", "start:activity"]

I use kubectl exec -it /bin/sh to get on the pod and run npm -v.

Environment

npm config is not returning anything as it is now. No options sent to npm work.

npm is supposed to be 8.15.0
It is using alpine 3.16 (tried 3.15 too)

Note: node -v does return a version just fine.  It is whatever version is in the alpine build.

mrdeshapiro avatar Aug 26 '22 13:08 mrdeshapiro

I got pod up again and ran node -v:

node -v v16.17.0

mrdeshapiro avatar Aug 26 '22 13:08 mrdeshapiro

I was able to reproduce the same issue when moving from node 14.20.0 to 16.17.0. It seems there are two aspects to reproduce the issue:

  • Use a recent NodeJS version, i.e 16.17.0
  • and use a non-privileged user (see below description)

Using a privileged (root) user ✅

Creating a Dockerfile with the USER directive set to root

USER root

so the container is built and executed with that root user everything works as expected:

root@fb304889a320:/# npm --version
8.15.0

root@fb304889a320:/# npm --help
npm <command>

Usage:

npm install        install all the dependencies in your project
npm install <foo>  add the <foo> dependency to your project
npm test           run this project's tests
npm run <foo>      run the script named <foo>
npm <command> -h   quick help on <command>
npm -l             display usage info for all commands
npm help <term>    search for help on <term>
npm help npm       more involved overview

All commands:

    access, adduser, audit, bin, bugs, cache, ci, completion,
    config, dedupe, deprecate, diff, dist-tag, docs, doctor,
    edit, exec, explain, explore, find-dupes, fund, get, help,
    hook, init, install, install-ci-test, install-test, link,
    ll, login, logout, ls, org, outdated, owner, pack, ping,
    pkg, prefix, profile, prune, publish, rebuild, repo,
    restart, root, run-script, search, set, set-script,
    shrinkwrap, star, stars, start, stop, team, test, token,
    uninstall, unpublish, unstar, update, version, view, whoami

Specify configs in the ini-formatted file:
    /.npmrc
or on the command line via: npm <command> --key=value

More configuration info: npm help config
Configuration fields: npm help 7 config

[email protected] /opt/bitnami/node/lib/node_modules/npm

root@fb304889a320:/#

Please note the root@... prompt which indicates the container is being executed with the root user. For the above tests, everything is working fine and both commands (npm --version and npm --help) return the expected output.

Using a non-privileged (1001) user 🔴

Creating a Dockerfile (the same as above) but with the USER directive set to 1001

USER 1001

so the container is built and executed with that 1001 user the npm binary is not working as expected:

I have no name!@9341b6941d34:/$ npm --version

I have no name!@9341b6941d34:/$ npm --help

I have no name!@9341b6941d34:/$ echo $?
243
I have no name!@9341b6941d34:/$ 

Please note the I have no name!@... prompt which indicates the container is being executed as a non-privileged/undefined user. For the above tests, now both commands (npm --version and npm --help) return an empty answer, the exit code 243 in both cases.


Doing the same exercise (root and non-root user) but using node 14.20.0 (and others) everything works as expected

carrodher avatar Sep 01 '22 08:09 carrodher

There were similar issues in the past related to other tools, see https://github.com/nodejs/node-gyp/pull/2333#discussion_r610383278

carrodher avatar Sep 01 '22 08:09 carrodher

Certainly, you should be able to run this as a non-privileged (non-root) user, right? I hope this issue can get resolved soon.

On Thu, Sep 1, 2022 at 4:12 AM Carlos Rodríguez Hernández < @.***> wrote:

I was able to reproduce the same issue when moving from node 14.20.0 to 16.17.0. It seems there are two aspects to reproduce the issue:

  • Use a recent NodeJS version, i.e 16.17.0
  • and use a non-privileged user (see below description)

Using a privileged (root) user ✅

Creating a Dockerfile with the USER directive set to root

USER root

so the container is built and executed with that root user everything works as expected:

@.***:/# npm --version 8.15.0

@.***:/# npm --help npm

Usage:

npm install install all the dependencies in your project npm install add the dependency to your project npm test run this project's tests npm run run the script named npm -h quick help on npm -l display usage info for all commands npm help search for help on npm help npm more involved overview

All commands:

access, adduser, audit, bin, bugs, cache, ci, completion,
config, dedupe, deprecate, diff, dist-tag, docs, doctor,
edit, exec, explain, explore, find-dupes, fund, get, help,
hook, init, install, install-ci-test, install-test, link,
ll, login, logout, ls, org, outdated, owner, pack, ping,
pkg, prefix, profile, prune, publish, rebuild, repo,
restart, root, run-script, search, set, set-script,
shrinkwrap, star, stars, start, stop, team, test, token,
uninstall, unpublish, unstar, update, version, view, whoami

Specify configs in the ini-formatted file: /.npmrc or on the command line via: npm --key=value

More configuration info: npm help config Configuration fields: npm help 7 config

@.*** /opt/bitnami/node/lib/node_modules/npm

@.***:/#

Please note the @.*** prompt which indicates the container is being executed with the root user. For the above tests, everything is working fine and both commands (npm --version and npm --help) return the expected output. Using a non-privileged (1001) user 🔴

Creating a Dockerfile (the same as above) but with the USER directive set to 1001

USER 1001

so the container is built and executed with that 1001 user the npm binary is not working as expected:

I have no @.***:/$ npm --version

I have no @.***:/$ npm --help

I have no @.:/$ echo $? 243 I have no @.:/$

Please note the I have no @.*** prompt which indicates the container is being executed as a non-privileged/undefined user. For the above tests, now both commands (npm --version and npm --help) return an empty answer, the exit code 243 in both cases.

Doing the same exercise (root and non-root user) but using node 14.20.0 (and others) everything works as expected

— Reply to this email directly, view it on GitHub https://github.com/npm/cli/issues/5428#issuecomment-1233911552, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2X3SMAZXPYG4NZP4U6EUX3V4BQOFANCNFSM57WWCRBQ . You are receiving this because you authored the thread.Message ID: @.***>

mrdeshapiro avatar Oct 11 '22 07:10 mrdeshapiro