Docker CMD没有看到安装的组件

我正在尝试使用以下泊坞窗文件来构build泊坞窗图像。

FROM ubuntu:latest # Replace shell with bash so we can source files RUN rm /bin/sh && ln -s /bin/bash /bin/sh # Update packages RUN apt-get -y update && apt-get install -y \ curl \ build-essential \ libssl-dev \ git \ && rm -rf /var/lib/apt/lists/* ENV APP_NAME testapp ENV NODE_VERSION 5.10 ENV SERVE_PORT 8080 ENV LIVE_RELOAD_PORT 8888 # Install nvm, node, and angular RUN (curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash -) \ && source /root/.nvm/nvm.sh \ && nvm install $NODE_VERSION \ && npm install -g angular-cli \ && ng new $APP_NAME \ && cd $APP_NAME \ && npm run postinstall EXPOSE $SERVE_PORT $LIVE_RELOAD_PORT WORKDIR $APP_NAME EXPOSE 8080 CMD ["node", "-v"] 

但是当我试图运行它时,我总是收到一个错误:

 docker: Error response from daemon: Container command 'node' not found or does not exist.. 

我知道节点正在正确安装,因为如果我通过从docker文件注释CMD线重build图像

 #CMD ["node", "-v"] 

然后开始一个shell会话

 docker run -it testimage 

我可以看到,我所有的依赖都在那里,并返回适当的结果

 node -v v5.10.1 

…..

 ng -v angular-cli: 1.0.0-beta.5 node: 5.10.1 os: linux x64 

所以我的问题是。 为什么Dockerfile中的CMD无法运行,我该如何解决?

当通过nvm使用shell到RUN node ,你已经获得了nvm.sh文件,它将在它的环境中设置一个$PATHvariables来通过nvmsearch可执行文件。

当你通过docker run运行命令时,它只会注入一个默认的PATH

 docker run <your-ubuntu-image> echo $PATH docker run <your-ubuntu-image> which node docker run <your-ubuntu-image> nvm which node 

用数组指定一个CMD直接执行一个二进制文件,不需要shell或$PATH来查找。

提供您的node二进制文件的完整path。

 CMD ["/bin/node","-v"] 

由于nvm 信号处理的方式,最好使用node二进制而不是nvm helper脚本。 在docker中使用节点apt包比在nvm中更容易。