Reputation: 501
Good evening.
We've learned that a Docker container is stateless, but it looks like an Alpine Linux container breaks this rule:
docker run <containername> -it /bin/sh
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.
Upvotes: 3
Views: 4640
Reputation: 3758
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:
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, e.g. 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.
Upvotes: 7
Reputation: 5612
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)
Upvotes: 2