简体   繁体   中英

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:

  1. We start an Alpine Linux container using docker run <containername> -it /bin/sh
  2. We install the missing nano editor: apk add nano

If we now stop and start this container, it still knows the nano editor.

Is this a special feature in Alpine Linux or does it sounds like our Docker host does something strange?

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. 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. 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. 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.

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. if you stop it and then re-run it- it is still the same one.

(it will be killed only after the grace period)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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