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.
2
Answers
Each
RUN
statement is executed in its own shell, therefore thesource
command does not affect the subsequent shells.To fix it, use a single
RUN
command: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).