简体   繁体   English

运行 docker 容器时未找到 npm 命令错误

[英]npm command not found error while running docker container

I am trying some something out with gitlab-runner image,我正在用 gitlab-runner 图像尝试一些东西,

    FROM gitlab/gitlab-runner:alpine
    WORKDIR /app
    COPY . /app
    RUN apk add yarn && yarn install

    RUN yarn --version        # this layer prints 1.16.0

    RUN ng build --prod
    EXPOSE 3000
    CMD ["yarn", "run", "start"]

above is the docker file I have created以上是我创建的 docker 文件

    docker build -t runner:1 .

I was able to build the image successfully我能够成功构建图像

    docker run -p 3000:3000 runner:1

but when I try to run the container it gives me below error但是当我尝试运行容器时,它给了我以下错误

`*FATAL: Command yarn not found.*`

not sure about the behavior, if it is able to install yarn ( apk add yarn ) in base images and install the dependencies using yarn install then how it is not able to find the yarn command while running the container?不确定行为,如果它能够在基础映像中安装 yarn( apk add yarn )并使用yarn install安装依赖项,那么它如何在运行容器时无法找到 yarn 命令? Where I am going wrong.我要去哪里错了。

Also at which directory yarn is installed in the alpine?同样在高山中安装纱线的目录是什么?

I know it is not an efficient docker file, but I am trying to run the container first before optimizing it.我知道它不是一个高效的 docker 文件,但我试图在优化之前先运行容器。

It outputs the version.它输出版本。 It means the yarn is installed already.这意味着已经安装了纱线。 You could find the path the same as you find the version.您可以找到与找到版本相同的路径。

RUN which yarn

Step 6/10 : RUN which yarn
 ---> Running in 0f633b81f2ed
/usr/bin/yarn

We can see the /usr/bin/ has added to PATH .我们可以看到/usr/bin/已添加到PATH中。

 Step 7/11 : RUN echo $PATH
 ---> Running in fc3f40b6bfd9
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

But I couldn't figure out why isn't reading yarn from the PATH.但我不明白为什么不从 PATH 读取纱线。

So, we have set the PATH explicitly in our Dockerfile.因此,我们在 Dockerfile 中明确设置了 PATH。

ENV PATH=${PATH} 

But still, the issue persists.但是,问题仍然存在。 Now we have to separate yarn and commands as ENTRYPOINT and CMD respectively in the Dockerfile.现在我们必须在 Dockerfile 中分别将纱线和命令分开为ENTRYPOINTCMD

ENTRYPOINT ["yarn"]
CMD ["run", "start"]

Updated Dockerfile更新 Dockerfile

FROM gitlab/gitlab-runner:alpine

ENV PATH=${PATH}

WORKDIR /app
COPY . /app
RUN apk add yarn && yarn install

RUN yarn --version        # this layer prints 1.16.0
RUN ng build --prod

EXPOSE 3000
ENTRYPOINT ["yarn"]
CMD ["run", "start"]
---

$ docker run -p 3000:3000 harik8/yarn:latest 
yarn run v1.16.0
error Couldn't find a package.json file in "/app"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The behaviours of the base image look unusual.基本图像的行为看起来不寻常。 It'd be better to go through it. go 最好通过它。

To build your app you shouldn't use gitlab-runner image, but a 'node' one.要构建您的应用程序,您不应使用 gitlab-runner 映像,而应使用“节点”映像。

Gilab-runner image is for running a gitlab agent which can be connected to docker engine and spawn the node container in which you will execute your build, in your case a docker image build. Gilab-runner 映像用于运行 gitlab 代理,该代理可以连接到 docker 引擎并生成节点容器,您将在其中执行构建,在您的情况下为 Z05B6053C41A2130AFD6FC3B158BDA46。

To use gilab you need to prepare a gitlab-ci file where you will define what steps and which 'services' you need to do your build.要使用 gilab,您需要准备一个 gitlab-ci 文件,您将在其中定义构建所需的步骤和“服务”。

Tl;dr: change base image to node:latest and as a entirely separate work setup gitlab runner. Tl;dr:将基础图像更改为 node:latest 并作为完全独立的工作设置 gitlab 运行器。

However if your aim is to have your application to extend gitlab runner, try docker multistage builds.但是,如果您的目标是让您的应用程序扩展 gitlab 运行器,请尝试 docker 多级构建。

First, use node:latest image to build your app, and then copy the build output into gitlab-runner.首先,使用 node:latest 镜像构建你的应用,然后将构建 output 复制到 gitlab-runner。

Runtime images such as gitlab-runner are stripped from build tools like yarn or npm, that's why your image fails.诸如 gitlab-runner 之类的运行时映像已从 yarn 或 npm 等构建工具中剥离,这就是您的映像失败的原因。 Main goal is to keep runtime images as small as possible and sdk's are not needed and sometimes dangerous when it comes to production-level work.主要目标是保持运行时映像尽可能小,并且在生产级工作中不需要 sdk,有时甚至是危险的。

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

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