1

I'm trying to install nvm like this:

FROM maven:3-jdk-8

RUN rm /bin/sh && ln -s /bin/bash /bin/sh

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

RUN source ~/.nvm/nvm.sh

RUN nvm install 16

RUN nvm use 16

However I keep getting this error:

 => [internal] load build definition from Dockerfile                                                                                                                  0.0s
 => => transferring dockerfile: 253B                                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                                     0.0s
 => => transferring context: 2B                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/maven:3-jdk-8                                                                                                      1.1s
 => [1/6] FROM docker.io/library/maven:3-jdk-8@sha256:ff18d86faefa15d1445d0fa4874408cc96dec068eb3487a0fc6d07f359a24607                                                0.0s
 => CACHED [2/6] RUN rm /bin/sh && ln -s /bin/bash /bin/sh                                                                                                            0.0s
 => CACHED [3/6] RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash                                                                  0.0s
 => CACHED [4/6] RUN source ~/.nvm/nvm.sh                                                                                                                             0.0s
 => ERROR [5/6] RUN nvm install 16                                                                                                                                    0.1s
------
 > [5/6] RUN nvm install 16:
#7 0.128 /bin/sh: line 1: nvm: command not found
------
executor failed running [/bin/sh -c nvm install 16]: exit code: 127

I would expect NVM is accessible because I run this line:

RUN source ~/.nvm/nvm.sh

What am I doing wrong here? When I run this manually in my docker container it works.

Jamie
  • 10,302
  • 32
  • 103
  • 186
  • [How to install nvm in docker?](https://stackoverflow.com/questions/25899912/how-to-install-nvm-in-docker) seems to address the problem more generally, and seems to have advice similar to the answers you've gotten here; do the (higher-scored) answers there help you? – David Maze Jan 25 '23 at 12:08
  • speaking of docker, just use a nodejs image that corresponds to the version that you need rather then installing nvm and install the desired node version within the image. you can read more about it https://stackoverflow.com/a/64820435/1184717 – Mr. Jan 25 '23 at 17:39

2 Answers2

1

Each RUN statement is executed in its own shell, therefore the source command does not affect the subsequent shells.

To fix it, use a single RUN command:

FROM maven:3-jdk-8

RUN rm /bin/sh && ln -s /bin/bash /bin/sh

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

RUN source ~/.nvm/nvm.sh && nvm install 16 && nvm use 16
Paolo
  • 21,270
  • 6
  • 38
  • 69
0

The source command will not have effect on the next RUN command. You need to have all the nvm commands in the same layer like this:

RUN source ~/.nvm/nvm.sh && nvm install 16 && nvm use 16

Or, if you would like to do it manually, the source command should be adding env variables (you can view them using the env command).

akathimi
  • 1,393
  • 11