简体   繁体   English

Dockerizing Node.js 应用程序 - 有什么作用:ENV PATH /app/node_modules/.bin:$PATH

[英]Dockerizing Node.js app - what does: ENV PATH /app/node_modules/.bin:$PATH

I went through one of very few good dockerizing Vue.js tutorials and there is one thing I don't understand why is mandatory in Dockerfile :我经历了为数不多的很好的 dockerizing Vue.js 教程之一,有一件事我不明白为什么在Dockerfile是强制性的:

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /usr/src/app/package.json #not sure though how it relates to PATH...

I found only one explanation here which says:我在这里只找到了一种解释它说:

We expose all Node.js binaries to our PATH environment variable and copy our projects package.json to the app directory.我们将所有 Node.js 二进制文件暴露给我们的 PATH 环境变量,并将我们的项目 package.json 复制到 app 目录。 Copying the JSON file rather than the whole working directory allows us to take advantage of Docker's cache layers.复制 JSON 文件而不是整个工作目录允许我们利用 Docker 的缓存层。

Still, it doesn't made me any smarter.尽管如此,它并没有让我变得更聪明。 Anyone able to explain it in plain english?谁能用简单的英语解释一下?

So I think the benefit of this line is to add the node_modules path from the Docker container to the list of PATH s on the relevant container.所以我认为这一行的好处是将来自 Docker 容器的 node_modules 路径添加到相关容器上的PATH列表中。 If you're on a Mac (or Linux I think) and run:如果您使用的是 Mac(或我认为是 Linux)并运行:

$ echo $PATH  

You should see a list of paths which are used to run global commands from your terminal ie gulp , husky , yarn and so on.您应该会看到用于从终端运行全局命令的路径列表,例如gulphuskyyarn等。

The above command will add node_modules path to the list of PATHs in your docker container so that such commands if needed can be run globally inside the container they will work.上面的命令会将 node_modules 路径添加到 docker 容器中的 PATH 列表中,以便这些命令可以在需要时在容器内全局运行,它们将起作用。

Error prevention错误预防

I think this is just a simple method of preventing an error where Docker wasn't able to find the correct executables (or any executables at all).我认为这只是防止 Docker 无法找到正确的可执行文件(或根本无法找到任何可执行文件)的错误的简单方法。 Besides adding another layer to your image, there is in general as far as I know no downside in adding that line to your Dockerfile .除了向您的图像添加另一层之外,据我所知,将该行添加到您的Dockerfile没有任何缺点。

How does it work?它是如何工作的?

Adding node_modules/bin to the PATH environment variable ensures that the executables created during the npm build or the yarn build processes can be found.node_modules/bin添加到PATH环境变量可确保可以找到在npm buildyarn build过程中创建的可执行文件。 You could also COPY your locally builded node_modules folder to the image but it's advised to build it inside the Docker container to ensure all binaries are adapted to the underlying OS running in the container.你也可以COPY你的本地建造node_modules文件夹中的图像,但它建议建立它的泊坞窗容器内,以确保所有的二进制文件都适合于容器中运行的基本操作系统。 The best practice would be to use multistage builds .最佳实践是使用多阶段构建

Furthermore, adding the node_modules/bin at the beginning of the PATH environment variable ensures that exactly these executables (from the node_modules folder) are used instead of any other executables which might also be installed on the system inside the Docker image.此外,在PATH环境变量的开头添加node_modules/bin可确保使用这些可执行文件(来自node_modules文件夹),而不是任何其他可能安装在 Docker 映像内的系统上的可执行文件。

Do I need it?我需要吗?

Short answer: Usually no.简短回答:通常不会。 It should be optional.应该是可选的。

Long answer: It should be enough to set the WORKDIR to the path where the node_modules is located for the issued RUN , CMD or ENTRYPOINT commands in your Dockerfile to find the correct binaries and therefore to successfully get executed.龙答:这应该足以设置WORKDIR到的路径node_modules所在的发行RUNCMDENTRYPOINT在你的命令Dockerfile找到正确的二进制文件,并因此成功地得到执行。 But I for example had a case where Docker wasn't able to find the files (I had a pretty complex setup with a so called devcontainer in VSCode ).但是,例如,我遇到了 Docker 无法找到文件的情况(我在devcontainer中使用所谓的devcontainer进行了非常复杂的设置)。 Adding the line ENV PATH /app/node_modules/.bin:$PATH solved my problem.添加行ENV PATH /app/node_modules/.bin:$PATH解决了我的问题。

So, if you want to increase the stability of your Docker setup in order to make sure that everything works as expected, just add the line.因此,如果您想提高 Docker 设置的稳定性以确保一切按预期工作,只需添加该行。

For each command, like FROM , COPY , RUN , CMD , ..., Docker creates a image with the result of this command, and this images are called as layers.对于每个命令,如FROMCOPYRUNCMD 、...,Docker 使用此命令的结果创建一个图像,这些图像被称为层。 The final image is the result of merge of all layers.最终图像是所有层合并的结果。

If you use the COPY command to store all the code in one layer, it will be greater than store a environment variable with path of the code.如果使用COPY命令将所有代码存储在一层中,它将大于存储带有代码路径的环境变量。

That's why the cache layers is a benefit.这就是缓存层是一个好处的原因。

For more info about layers, take a look at this very good article.有关图层的更多信息,请查看这篇非常好的文章。

.bin (short for 'binaries') is a hidden directory , the period before the bin indicates that it is hidden. .bin('binaries' 的缩写)是一个隐藏目录,bin 前的句点表示它是隐藏的。 This directory contains executable files of your app's modules.此目录包含应用程序模块的可执行文件。

PATH is just a collection of directories/folders that contains executable files. PATH 只是包含可执行文件的目录/文件夹的集合。 When you try to do something that requires a specific executable file, the shell looks for it in the collection of directories in PATH.当您尝试执行需要特定可执行文件的操作时,shell 会在 PATH 中的目录集合中查找它。

ENV PATH /app/node_modules/.bin:$PATH adds the .bin directory to this collection, so that when node tries to do something that requires a specific module's executable, it will look for it in the .bin folder. ENV PATH /app/node_modules/.bin:$PATH将 .bin 目录添加到此集合中,以便当节点尝试执行需要特定模块可执行文件的操作时,它将在 .bin 文件夹中查找。

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

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