簡體   English   中英

Docker: npm 未找到

[英]Docker: npm not found

我有以下 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: 未找到

問題是什么? 請你幫助我好嗎?

嘗試在構建映像時單獨安裝npm

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

Node還打包了npm ,所以不需要像 Yury 提到的那樣安裝npm 這樣做通常是個壞主意,因為您無法控制nodejsnpm版本

對我來說,答案很簡單。 我有以下代碼:

# 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

在運行任何 npm 命令之前,嘗試在 Docker 文件中添加這兩行。

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

您可能已經在此處安裝了nodenpm 在通過curl安裝節點包之后,您可能需要在新的交互式 shell 中運行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調用

構建 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   

並且容器構建成功

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM