简体   繁体   English

Docker:Alpine Linux:为什么要添加apk <package> 持久在容器中?

[英]Docker: Alpine Linux: why is apk add <package> persistent in a container?

Good evening. 晚上好。

We've learned that a Docker container is stateless, but it looks like an Alpine Linux container breaks this rule: 我们已经了解到Docker容器是无状态的,但它看起来像一个Alpine Linux容器违反了这个规则:

  1. We start an Alpine Linux container using docker run <containername> -it /bin/sh 我们使用docker run <containername> -it /bin/sh启动Alpine Linux容器
  2. We install the missing nano editor: apk add nano 我们安装了缺少的纳米编辑器: apk add nano

If we now stop and start this container, it still knows the nano editor. 如果我们现在停止并启动此容器,它仍然知道nano编辑器。

Is this a special feature in Alpine Linux or does it sounds like our Docker host does something strange? 这是Alpine Linux中的一个特殊功能还是听起来我们的Docker主机做了一些奇怪的事情?

Thanks a lot for any light and help! 非常感谢任何光明和帮助! Kind regards, 亲切的问候,
Thomas. 托马斯。

It looks like there is a fundamental misunderstanding of the concept here: Docker containers are not stateless by themselves. 看起来这里对这个概念有一个根本的误解:Docker容器本身并不是无状态的。 If you make any change to the container file system, it is persisted as long as the container lives. 如果对容器文件系统进行任何更改,则只要容器存在,它就会保留。 Only when the container is destroyed (removed), its writable layer of the file system, which contains all changes made since the container creation, gets deleted. 只有当容器被销毁(删除)时,文件系统的可写层才会被删除,该层包含自容器创建以来所做的所有更改。

When we say that "Docker containers are stateless", we refer not to the fact that they are stateless by definition , but rather to the best practice and a guideline about how containers should be used. 当我们说“Docker容器是无状态的”时,我们不是指它们根据定义是无状态的事实,而是关于如何使用容器的最佳实践和指南。 Statelessness is a pattern of using the containers, which allows to consider them as throwaway entities, for example: 无状态是使用容器的模式 ,允许将它们视为一次性实体,例如:

  • if you lose a stateless container because of application crash or system/hardware failure, you don't care about this and just create a new one, which continues working as its predecessor. 如果由于应用程序崩溃或系统/硬件故障导致丢失无状态容器,则您不关心这一点,只需创建一个新的容器,它将继续作为其前身工作。
  • If you want to offload some containerized application from one very busy server to another, underutilized one (eg you just purchased and installed new hardware), you kill the container on the original server and recreate it on the new one. 如果要将一些容器化应用程序从一个非常繁忙的服务器卸载到另一个非常繁忙的服务器(例如,您刚刚购买并安装了新硬件),则会终止原始服务器上的容器并在新服务器上重新创建容器。

That said, you have to understand that Docker does not enforce statelessness - you , the creator and user of the container, have to care about it. 也就是说,您必须了解Docker不会强制执行无状态 - ,容器的创建者和用户必须关心它。 You have to store all persistent application data on external storage, mounted into the container from outside, or in the networked storage, eg in a database. 您必须将所有持久性应用程序数据存储在外部存储器上,从外部安装到容器中,或者存储在网络存储器中,例如存储在数据库中。

So, answering the initial question - there is nothing special in Alpine. 所以,回答最初的问题 - 阿尔卑斯山没什么特别的。 You add files to the container - you have them there until the container is destroyed. 您将文件添加到容器中 - 您将它们放在容器中,直到容器被销毁。 This is true for any container, not only for those which are created from the Alpine image. 这适用于任何容器,不仅适用于从Alpine图像创建的容器。

the container still exists when you stop it. 当你停止它时,容器仍然存在。 only when you delete the container ( docker container rm <id> ) and the launch it again via docker container run than it will be a fresh new container out of the specified image. 只有当您删除容器docker container rm <id>并通过docker container run再次启动它时,它才会成为指定映像之外的新容器。 if you stop it and then re-run it- it is still the same one. 如果你停止然后重新运行它 - 它仍然是同一个。

(it will be killed only after the grace period) (它只会在宽限期后被杀死)

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

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