Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The npm command is not found in my NodeJS docker container

I created a Docker image:

$ docker build -t stephaneeybert/nodejs .
Sending build context to Docker daemon  2.56 kB
Step 1 : FROM debian
 ---> 1b088884749b
Step 2 : RUN apt-get clean && apt-get update
 ---> Using cache
 ---> b12133d6342f
Step 3 : RUN apt-get install -y curl
 ---> Using cache
 ---> 22dfb4882b12
Step 4 : RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
 ---> Using cache
 ---> 27f2fac45254
Step 5 : RUN . ~/.nvm/nvm.sh; nvm install stable
 ---> Using cache
 ---> 20d99d545755
Step 6 : RUN . ~/.nvm/nvm.sh; nvm use stable
 ---> Using cache
 ---> 9ec14efb2407
Step 7 : RUN . ~/.nvm/nvm.sh; npm install -g npm
 ---> Using cache
 ---> d264d38565f3
Step 8 : EXPOSE 9001
 ---> Using cache
 ---> 29e3589557e1
Step 9 : ENTRYPOINT /usr/bin/tail -f /dev/null
 ---> Using cache
 ---> 2ce499300fe1
Successfully built 2ce499300fe1

The image script is:

FROM debian

RUN apt-get clean && apt-get update
RUN apt-get install -y curl

# Installing nodesjs

RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

RUN . ~/.nvm/nvm.sh; nvm install stable
RUN . ~/.nvm/nvm.sh; nvm use stable
RUN . ~/.nvm/nvm.sh; npm install -g npm

EXPOSE 9001

ENTRYPOINT ["/usr/bin/tail", "-f", "/dev/null"]

Then I run the container and open a bash shell:

$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
c6dddf0a5eb0f11c897f63910eb01f2868fe0f39a80e5e2a580ef3a82935b27b
[stephane@stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root@c6dddf0a5eb0:/# 

Once in there, I try to get the version:

root@c6dddf0a5eb0:/# npm -v
bash: npm: command not found

But npm is not found.

When typing the command nvm use stable in the interactive shell, it give the following error: N/A: version "N/A" is not yet installed.

I understand there is an alias against a non existant node version.

The nvm ls command shows:

root@60446f9286d0:/# nvm ls
            N/A
node -> stable (-> N/A) (default)
iojs -> N/A (default)

The debugger has this to show:

root@60446f9286d0:/# nvm debug
nvm --version: v0.32.1
$SHELL: /bin/bash
$HOME: /root
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: none
which node: 
which iojs: 
which npm: 
npm config get prefix: bash: npm: command not found
npm root -g: bash: npm: command not found

1- How come I need to source this script . ~/.nvm/nvm.sh; on each command?

2- Why is my Node package manager not found in the bash shell?

EDIT: I changed a bit the content of the Dockerfile file:

RUN curl -o-https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash \
  && . ~/.nvm/nvm.sh \
  && nvm install stable \
  && nvm alias default stable \
  && nvm use default

And building it now shows this:

Step 4 :RUN curl -o https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash   && . ~/.nvm/nvm.sh   && nvm install stable   && nvm alias default stable   && nvm use default
 ---> Running in 7d2c404135dd
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10250  100 10250    0     0  18258      0 --:--:-- --:--:-- --:--:-- 18238
=> Downloading nvm as script to '/root/.nvm'

=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v7.2.0 (npm v3.10.9)
Creating default alias: default -> stable (-> v7.2.0 *)
default -> stable (-> v7.2.0 *)
Now using node v7.2.0 (npm v3.10.9)
 ---> ad960a4addbe
Removing intermediate container 7d2c404135dd
Step 5 : EXPOSE 9001
 ---> Running in df9284421302
 ---> 14d386f009fb
Removing intermediate container df9284421302
Step 6 : ENTRYPOINT /usr/bin/tail -f /dev/null
 ---> Running in fa2d71b6dfdf
 ---> d02c8e88eb7f
Removing intermediate container fa2d71b6dfdf
Successfully built d02c8e88eb7f

I can see it installed node v7.2.0 and is using it.

But when I log into the container with the command docker exec -it nodejs bash it does not see any node anywhere:

root@f8f2a32b462a:/# nvm --version   
0.32.1
root@f8f2a32b462a:/# npm --version   
bash: npm: command not found
root@f8f2a32b462a:/# echo $NVM_DIR   
/root/.nvm
root@f8f2a32b462a:/# ls -l /root/.nvm
total 100
-rwxr-xr-x 1 root root   313 Nov 26 13:01 nvm-exec
-rw-r--r-- 1 root root 95660 Nov 26 13:01 nvm.sh
root@f8f2a32b462a:/# ls -l /root/.npm
ls: cannot access /root/.npm: No such file or directory
like image 770
Stephane Avatar asked Sep 07 '25 07:09

Stephane


1 Answers

I changed the way I install Node and did it without the nvm tool:

RUN curl -sL https://deb.nodesource.com/setup_7.x | bash
RUN apt-get install -y nodejs

Now, when logging in the container, it can find the Node executable:

$ docker run -d -p 127.0.0.1:9001:9001 --name nodejs stephaneeybert/nodejs
f3a2f054934ef92a9b05486b6f6dbe53abd4390826c06d1b7a490d671d8e3422
[stephane@stephane-ThinkPad-X301 nodejs]
$ docker exec -it nodejs bash
root@f3a2f054934e:/# npm --version                                                                                                           
3.10.9

Maybe, when I was using the nvm tool, I should have sourced the npm command in the client shell . ~/.nvm/nvm.sh npm --version.

like image 51
Stephane Avatar answered Sep 09 '25 05:09

Stephane