Docker:未找到npm

xoshrz7s  于 2022-11-28  发布在  Docker
关注(0)|答案(5)|浏览(270)

我有以下Dockerfile:

FROM ubuntu
USER root
RUN apt-get update && apt-get install curl -y
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get update && apt-get upgrade -y && apt-get install nodejs -y
RUN mkdir /opt/public
RUN mkdir /opt/bin
ADD public /opt/public
ADD bin /opt/bin
RUN ls -lah /opt/bin
RUN ls -lah /opt/public
ADD run.sh /bin/run.sh
RUN chmod +x /bin/run.sh
RUN cd /opt/bin && npm install
CMD ["/bin/run.sh"]

生成容器时,出现以下错误:
/bin/sh:1:npm:未找到
有什么问题吗?你能帮帮我吗?

nbysray5

nbysray51#

尝试在构建映像时单独安装npm

RUN apt-get update && apt-get upgrade -y && \
    apt-get install -y nodejs \
    npm                       # note this one
5sxhfpxr

5sxhfpxr2#

Node也打包了npm,所以不需要像Yury提到的那样安装npm
对我来说,答案很简单。我有下面的代码:

# install nodejs
RUN curl --silent --location https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y \
  nodejs
RUN echo "Node: " && node -v
RUN echo "NPM: " && npm -v

但是我必须安装curl,所以它失败了。所以在此之前,你需要安装curl:

RUN apt-get update && apt-get install -y curl
41ik7eoe

41ik7eoe3#

您可能已经在这里安装了nodenpm。在通过curl安装节点包之后,您可能需要在新的交互式环境中运行npm/node相关脚本。因此,在最后一行中,您可以尝试:

CMD cat /bin/run.sh | bash -ic

或者

CMD bash -i /bin/run.sh

或者

CMD ["/bin/bash","-i","/bin/run.sh"]

npm/node的交互式bash在我的示例中工作,并使用bash -i调用

dw1jzc5e

dw1jzc5e4#

在运行任何npm命令之前,尝试在Docker文件中添加这两行。

RUN apt-get install --no-install-recommends apt-utils --yes \
    && apt-get install --no-install-recommends npm --yes
pn9klfpd

pn9klfpd5#

生成Docker容器时出现以下错误:

> [runtime  1/11] RUN curl -sL https://deb.nodesource.com/setup_16.x | -E bash;        apt-get install -y nodejs;        npm i -g npm@8:
#11 0.360 /bin/sh: 1: -E: not found
#11 1.687 Reading package lists...
#11 1.696 Building dependency tree...
#11 1.698 Reading state information...
#11 1.703 E: Unable to locate package nodejs
#11 1.703 /bin/sh: 1: npm: not found

err: appname
/bin/sh: 1: npm: not found

在dockerfile中,我已更改:

RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - \
      && apt-get install -y nodejs \
      && npm i -g npm@8

"到这个"

RUN apt-get update && apt-get upgrade -y && \
    apt-get install -y nodejs \
    npm

和容器生成成功

相关问题