简体   繁体   English

Docker 安装 nvm

[英]Docker install nvm

I'm trying to install nvm like this:我正在尝试像这样安装 nvm:

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:我希望 NVM 可以访问,因为我运行了这一行:

RUN source ~/.nvm/nvm.sh

What am I doing wrong here?我在这里做错了什么? When I run this manually in my docker container it works.当我在我的 docker 容器中手动运行它时,它起作用了。

Each RUN statement is executed in its own shell, therefore the source command does not affect the subsequent shells.每个RUN语句都在其自己的 shell 中执行,因此source命令不会影响后续的 shell。

To fix it, use a single RUN command:要修复它,请使用单个RUN命令:

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

The source command will not have effect on the next RUN command. source 命令不会对下一个RUN命令产生影响。 You need to have all the nvm commands in the same layer like this:您需要将所有 nvm 命令都放在同一层中,如下所示:

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).或者,如果您想手动执行此操作,源命令应该添加环境变量(您可以使用env命令查看它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM