Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my non-volume data in Docker container persist even after restarting the container?

In some places when I read about Docker containers, I found some people talking that they lose their data (saved inside the container and not a part of volume data) when they restart the container.

I tried to create a simple Ubuntu container like this: docker run -it ubuntu /bin/bash, and created some files inside the container and then restarted it, but my data still there. Why does that actually happen? why do my data still there? Is this something new in the newer versions of Docker or do I have misunderstanding for something?

like image 855
Mohammed Noureldin Avatar asked Nov 28 '16 17:11

Mohammed Noureldin


People also ask

How do I get rid of unused docker volumes?

Volumes are removed using the docker volume rm command. You can also use the docker volume prune command.

Does docker container data persist?

By default all files created inside a container are stored on a writable container layer. This means that: The data doesn't persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.

Does restarting a container lose data?

Docker does not persist data if a container stop exists, note that many people got confused and believe that docker has data persistence without any configuration because they stop a container and they see that their data is still there when they restart the container, but in docker terms the container exists even if ...

What happens to data of the container when a container exists?

No, you won't lose any data when Docker container exits. Any data that your application writes to the container gets preserved on the disk until you explicitly delete the container. The file system for the container persists even after the container halts.


2 Answers

The data is lost when the container is removed, not when it's stopped or restarted.

Basically, if you do docker ps, if the containers keeps the same id (the big ugly hexadecimal id), the data is not lost.

It gets complicated when somehow your docker containers are not managed by you, but by some kind of automated-managing method. Tools like these usually start new containers if there is failure. In that case you should mount a volume to store your data on the host.

like image 79
HakRo Avatar answered Sep 22 '22 03:09

HakRo


You might want to look at the Container Lifecycle: https://github.com/wsargent/docker-cheat-sheet#lifecycle

docker create creates a container but does not start it.

docker rename allows the container to be renamed.

docker run creates and starts a container in one operation.

docker rm deletes a container.

docker update updates a container's resource limits.

If you do docker rm and docker run again your state will not be there anymore.

If you want a transient container, docker run --rm will remove the container after it stops.

like image 37
Pedro Baptista Afonso Avatar answered Sep 21 '22 03:09

Pedro Baptista Afonso