我应该在 docker 镜像中还是在卷中使用应用程序代码?

发布于 2025-01-10 16:04:35 字数 80 浏览 4 评论 0原文

我正在从事 DevOps 项目。我想找到完美的解决方案。 我的两种解决方案之间存在冲突。我应该在 docker 镜像中还是在卷中使用应用程序代码?

I am working on a Devops project. I want to find the perfect solution.
I have a conflict between two solutions. should I use the application code inside the docker images or in volumes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬心 2025-01-17 16:04:35

您的代码几乎不应该成卷,除了开发人员专用的设置(即使如此)。如果您有一个像频繁的仅限开发人员的 Node 设置这样的设置,将 node_modules 目录放入 Docker 管理的匿名卷中,则更是如此:因为 Docker 将拒绝自行更新该目录,因此这样做的主要效果是导致 Docker 忽略对 package.json 文件的任何更改。

更一般地说,在这种情况下,您应该将图像视为分发应用程序代码的一种方式。考虑像 Kubernetes 这样的集群环境:集群管理器知道如何自行拉取版本化的 Docker 镜像,但您需要解决许多标准机制才能尝试将代码推送到卷中。您不需要既分发 Docker 映像,又单独分发映像中的代码。

我建议使用主机目录挂载来注入配置文件和存储基于文件的日志(如果容器无法配置为记录到标准输出)。使用主机目录或命名卷挂载来存储有状态容器的数据(主机目录更容易备份,命名卷在非 Linux 平台上速度更快)。根本不要对应用程序代码或库使用卷。

(考虑一下,如果您只是使用卷挂载覆盖所有应用程序代码,您也可以只使用基本 node 映像而不构建自定义映像;如果您这样做,您也可以使用自动化系统(Salt Stack、Ansible、Chef 等)来安装 Node 并完全忽略 Docker。)

Your code should almost never be in volumes, developer-only setups aside (and even then). This is doubly true if you have a setup like a frequent developer-only Node setup that puts the node_modules directory into a Docker-managed anonymous volume: since Docker will refuse to update that directory on its own, the primary effect of this is to cause Docker to ignore any changes to the package.json file.

More generally, in this context, you should think of the image as a way to distribute the application code. Consider clustered environments like Kubernetes: the cluster manager knows how to pull versioned Docker images on its own, but you need to work around a lot of the standard machinery to try to push code into a volume. You should not need to both distribute a Docker image and also separately distribute the code in the image.

I'd suggest using host-directory mounts for injecting configuration files and for storing file-based logs (if the container can't be configured to log to stdout). Use either host-directory or named-volume mounts for stateful containers' data (host directories are easier to back up, named volumes are faster on non-Linux platforms). Do not use volumes at all for your application code or libraries.

(Consider that, if you're just overwriting all of the application code with volume mounts, you may as well just use the base node image and not build a custom image; and if you're doing that, you may as well use your automation system (Salt Stack, Ansible, Chef, etc.) to just install Node and ignore Docker entirely.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文