简体   繁体   English

为什么卷中的 vendor/node_modules 映射被认为是不好的做法?

[英]Why a vendor/node_modules mapping in a volume is considered a bad practise?

Could someone explain me what is happening when you map (in a volume) your vendor or node_module files?有人可以解释一下当您 map(在一个卷中)您的供应商或 node_module 文件时发生了什么?

I had some speed problems of docker environment and red that I don't need to map vendor files there, so I excluded it in docker-compose.yml file and the speed was much faster instantly.我有一些 docker 环境的速度问题和红色我不需要 map 供应商文件,所以我将它排除在docker-compose.yml文件中,速度立即快得多。

So I wonder what is happening under the hood if you have vendor files mapped in your volume and what's happening when you don't?所以我想知道如果您的卷中映射了供应商文件,引擎盖下会发生什么,而当您没有映射时会发生什么?

Could someone explain that?有人可以解释一下吗? I think this information would be useful to more than only me.我认为这些信息不仅对我有用。

Docker does some complicated filesystem setup when you start a container. Docker 在启动容器时会进行一些复杂的文件系统设置。 You have your image , which contains your application code;你有你的图像,其中包含你的应用程序代码; a container filesystem , which gets lost when the container exits;容器文件系统,当容器退出时会丢失; and volumes , which have persistent long-term storage outside the container.volumes ,它们在容器外具有持久的长期存储。 Volumes break down into two main flavors, bind mounts of specific host directories and named volumes managed by the Docker daemon.卷分为两种主要形式,特定主机目录的绑定挂载和由 Docker 守护进程管理的命名卷

The standard design pattern is that an image is totally self-contained.标准设计模式是图像是完全独立的。 Once I have an image I should be able to push it to a registry and run it on another machine unmodified.一旦我有一个图像,我应该能够将它推送到注册表并在另一台未经修改的机器上运行它。

git clone git@github.com:me/myapp
cd myapp
docker build -t me/myapp .  # requires source code
docker push me/myapp

ssh me@othersystem
docker run me/myapp         # source code is in the image
                            # I don't need GitHub credentials to get it

There's three big problems with using volumes to store your application or your node_modules directory:使用卷来存储应用程序或node_modules目录存在三个大问题:

  1. It breaks the "code goes in the image" pattern.它打破了“代码进入图像”模式。 In an actual production environment, you wouldn't want to push your image and also separately push the code;在实际的生产环境中,您不会想要推送您的图像并单独推送代码; that defeats one of the big advantages of Docker.这破坏了 Docker 的一大优势。 If you're hiding every last byte of code in the image during the development cycle, you're never actually running what you're shipping out.如果您在开发周期中隐藏图像中的每个最后一个字节的代码,那么您将永远不会真正运行您要发送的内容。

  2. Docker considers volumes to contain vital user data that it can't safely modify. Docker 认为卷包含无法安全修改的重要用户数据。 That means that, if your node_modules tree is in a volume, and you add a package to your package.json file, Docker will keep using the old node_modules directory, because it can't modify the vital user data you've told it is there. That means that, if your node_modules tree is in a volume, and you add a package to your package.json file, Docker will keep using the old node_modules directory, because it can't modify the vital user data you've told it is那里。

  3. On MacOS in particular, bind mounts are extremely slow , and if you mount a large application into a container it will just crawl.特别是在 MacOS 上, 绑定挂载非常慢,如果将大型应用程序挂载到容器中,它只会爬行。

I've generally found three good uses for volumes: storing actual user data across container executions;我通常发现卷有三个很好的用途:跨容器执行存储实际用户数据; injecting configuration files at startup time;在启动时注入配置文件; and reading out log files.并读出日志文件。 Code and libraries are not good things to keep in volumes.代码和库不是保存大量的好东西。

For front-end applications in particular there doesn't seem to be much benefit to trying to run them in Docker.特别是对于前端应用程序,尝试在 Docker 中运行它们似乎没有太多好处。 Since the actual application code runs in the browser, it can't directly access any Docker-hosted resources, and there's no difference if your dev server runs in Docker or not.由于实际的应用程序代码在浏览器中运行,它不能直接访问任何 Docker 托管的资源,并且您的开发服务器是否运行在 Docker 中没有区别。 The typical build chains involving tools like Typescript and Webpack don't have additional host dependencies, so your Docker setup really just turns into a roundabout way to run Node against the source code that's only on your host.涉及 Typescript 和 Webpack 等工具的典型构建链没有额外的主机依赖项,因此您的 Docker 设置实际上只是在您的主机上运行源代码的迂回方式。 The production path of building your application into static files and then using a Web server like nginx to serve them is still right in Docker. The production path of building your application into static files and then using a Web server like nginx to serve them is still right in Docker. I'd just run Node on the host to develop this sort of thing, and not have to think about questions like this one.我只是在主机上运行 Node 来开发这种东西,而不必考虑这样的问题。

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

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