简体   繁体   English

Docker 安装 npm 后找不到图像错误 bin

[英]Docker image erorr bin not found after npm installation

I need to extend a Dockerfile and add grunt to it.我需要扩展Dockerfile 并向其添加grunt I did the following:我做了以下事情:

This docker run as-is此 docker 按原样运行

    FROM openjdk:8-jdk-slim
    ARG ND=v12.13.0

    RUN apt-get update && \
        apt-get install --yes --no-install-recommends curl  && \

        NODE_H=/opt/nodejs; mkdir -p ${NODE_H} && \
        curl --fail --silent --output - "http://nodejs.org/dist/${ND}/node-${ND}-linux-x64.tar.gz" \
         | tar -xzv -f - -C "${NODE_H}" && \
        ln -s "${NODE_H}/node-${ND}-linux-x64/bin/npm" /usr/local/bin/npm && \
        ln -s "${NODE_H}/node-${ND}-linux-x64/bin/node" /usr/local/bin/node && \
        ln -s "${NODE_H}/node-${ND}-linux-x64/bin/npx" /usr/local/bin/ && \



    npm install grunt-cli -g

    RUN grunt -v

I've put also the following which doesn't help...我还放了以下无济于事的...

ENV PATH="$PATH:/usr/local/bin"

When I run the command grunt-v I get the following error:当我运行命令grunt-v时,出现以下错误:

/bin/sh: 1: grunt: not found . /bin/sh: 1: grunt: not found

I try also to install grunt through npm install grunt -g without success.我也尝试通过npm install grunt -g ,但没有成功。 Any idea how to fix it?知道如何解决吗?

grunt output from docker build从 docker 构建的咕噜声 output

/opt/nodejs/node-v12.13.0-linux-x64/bin/grunt -> /opt/nodejs/node-v12.13.0-linux-x64/lib/node_modules/grunt-cli/bin/grunt
+ grunt-cli@1.3.2

I need the grunt command to be available in this docker image我需要grunt命令在此 docker 图像中可用

I cannot change the docker image, ie form jdk...this is given我无法更改 docker 图像,即形成 jdk ......这是给定的

update更新

I've also tried with what VonC suggested but still have issue,我也尝试过 VonC 的建议,但仍有问题,

FROM openjdk:8-jdk-slim


ARG ND=v12.13.0

RUN apt-get update && \
    apt-get install --yes --no-install-recommends curl  && \

    # install node
    NODE_HOME=/opt/nodejs; mkdir -p ${NODE_HOME} && \
    curl --fail --silent --output - "http://nodejs.org/dist/${ND}/node-${ND}-linux-x64.tar.gz" \
     | tar -xzv -f - -C "${NODE_HOME}" && \
    ln -s "${NODE_HOME}/node-${ND}-linux-x64/bin/node" /usr/local/bin/node && \
    ln -s "${NODE_HOME}/node-${ND}-linux-x64/bin/npm" /usr/local/bin/npm && \
    ln -s "${NODE_HOME}/node-${ND}-linux-x64/bin/npx" /usr/local/bin/ && \



    npm install -g grunt-cli

ENV PATH="${PATH}:/usr/local/bin"
RUN ls /usr/local/bin/
RUN grunt -v

the ls command returns ls 命令返回


docker-java-home
node
npm
npx

Any idea what is missing?知道缺少什么吗?

this will work:这将起作用:

FROM openjdk:8-jdk-slim


ARG ND=v12.13.0

RUN apt-get update && \
    apt-get install --yes --no-install-recommends curl \
    && NODE_HOME=/opt/nodejs; mkdir -p ${NODE_HOME} \
    && curl --fail --silent --output - "http://nodejs.org/dist/${ND}/node-${ND}-linux-x64.tar.gz" \
     | tar -xzv -f - -C "${NODE_HOME}" \
    &&  ln -s "${NODE_HOME}/node-${ND}-linux-x64/bin/node" /usr/local/bin/node \
    && ln -s "${NODE_HOME}/node-${ND}-linux-x64/bin/npm" /usr/local/bin/npm \
    && ln -s "${NODE_HOME}/node-${ND}-linux-x64/bin/npx" /usr/local/bin/ \
    && npm install --prefix /usr/local/ -g grunt-cli

ENV PATH="${PATH}:/usr/local/bin"
RUN ls /usr/local/bin
RUN grunt -v

using --prefix will tell npm to install grunt in /usr/local/bin使用--prefix将告诉 npm 在/usr/local/bin中安装 grunt

ls output: ls output:

Step 5/6 : RUN ls /usr/local/bin
 ---> Running in 96493743512d
docker-java-home
grunt
node
npm
npx

grunt -v output:咕噜声-v output:

Step 6/6 : RUN grunt -v
 ---> Running in c6248c4fce6c
grunt-cli: The grunt command line interface (v1.3.2)

I've put also the following which doesn't help...我还放了以下无济于事的...

 ENV PATH="$PATH:/usr/local/bin"

As illustrated here , that should be enough, also the exact syntax would be (to be sure)如此处所示,这应该足够了,确切的语法也是(可以肯定的)

ENV PATH="${PATH}:/usr/local/bin"

But:但:

  • make sure to add it just before your last RUN grunt确保在最后一次RUN grunt之前添加它
  • add a RUN ls /usr/local/bin/ to see if your install command worked添加RUN ls /usr/local/bin/以查看您的安装命令是否有效
  • try and use the syntax npm instal -g grunt , instead of npm instal grunt -g尝试使用语法npm instal -g grunt ,而不是npm instal grunt -g

Another approach:另一种方法:

The Docker image openjdk:8-jdk-slim is based on debian:buster-slim Docker 镜像openjdk:8-jdk-slim基于debian:buster-slim

So try and install node through its installation script, as seen here :所以尝试通过它的安装脚本安装节点,如下所示

# install node.js environment
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      gnupg && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*
RUN curl -sL https://deb.nodesource.com/setup_${NODEJS_VERSION}.x | bash -
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      nodejs && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    npm install -g grunt

You can still use the same base image openjdk:8-jdk-slim , but you just extend it with a regular node installation, rather than fiddling with symbolic links.您仍然可以使用相同的基础映像openjdk:8-jdk-slim ,但您只需使用常规节点安装扩展它,而不是摆弄符号链接。

In your case, add ENV NODEJS_VERSION 12 first.在您的情况下,首先添加ENV NODEJS_VERSION 12

This works这有效

FROM openjdk:8-jdk-slim

ARG NODE_HOME=/opt/nodejs
ARG ND=v12.13.0
ENV PATH=${PATH}:${NODE_HOME}/node-${ND}-linux-x64/bin/

RUN apt-get update && \
    apt-get install --yes --no-install-recommends curl  && \
    # install node
    mkdir -p ${NODE_HOME} && \
    curl --fail --silent --output - "http://nodejs.org/dist/${ND}/node-${ND}-linux-x64.tar.gz" \
     | tar -xzv -f - -C "${NODE_HOME}" && \
    npm install -g grunt-cli

RUN grunt -v

First, move NODE_HOME up in your dockerfile and set it as a build arg.首先,在 dockerfile 中向上移动 NODE_HOME 并将其设置为构建参数。

That way we can already set the PATH early on.这样我们就可以尽早设置 PATH 了。

By setting the path to the node bin folder we can use all binaries in that location without manually linking each.通过设置节点 bin 文件夹的路径,我们可以使用该位置的所有二进制文件,而无需手动链接每个文件。 That translates to grunt being available after installation without additional black magic.这意味着安装后可以使用 grunt,而无需额外的黑魔法。

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

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