Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the data when a docker container crashes

We are trying to move onto Docker for deployment purpose. Our architecture requires to have a redis, a mongodb and several nodejs and java based Docker containers.

So my question is, if suppose the redis/mongodb docker container crashes, do we loose all the data that it had?

We want isolation, but at the same time we don't want to loose data due to malfunction/crashes. Is this even possible to achieve this with Docker or is it something not relevant here?

Any help or comments will be greatly appreciated.

Thanks

like image 353
Sohail Avatar asked Dec 30 '14 07:12

Sohail


People also ask

What happens when docker container crashes?

If you've got a initial command or entrypoint that immediately crashes, Docker will immediately shut it back down for you. This can make your container unstartable, so you can't shell in any more, which really gets in the way.

Do I lose my data when the docker container exits?

Do I lose my data when the container exits? 🔗 Not at all! Any data that your application writes to disk gets preserved in its container until you explicitly delete the container.

What happens to the data inside a container when we stop it?

If such power disconnects or crash happens to your system the container goes into stop state and whatever data you saved before power disconnect will stay inside the container file system as long as it is not terminated. You can as access your data again by re-starting that container.

Does docker container persist data?

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.


1 Answers

The answer is: YES - If a container crashes so that it can not be restored/restarted the data is gone. But, normally containers can be restarted and continued - in that case the data is not lost.

E.g. - the following sequence from the docker docs illustrates how container startup work. Note that the data is not lost here until the container is removed.

# Start a new container
$ JOB=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")

# Stop the container
$ sudo docker stop $JOB

# Start the container
$ sudo docker start $JOB

# Restart the container
$ sudo docker restart $JOB

# SIGKILL a container
$ sudo docker kill $JOB

# Remove a container
$ sudo docker stop $JOB # Container must be stopped to remove it
$ sudo docker rm $JOB

Whenever you execute a docker run command you start a new container with fresh data. The data is based on the image you provide and that data is consistent (unless you rebuild the image of course).

So, how should you setup docker to keep your data intact? I think that a good approach is to keep the important data mounted in a volume. Volumes are simply external folders (i.e. a folder from the host system) that holds the data and this data will not be lost even if you reinstall the entire docker daemon.

Example:

docker run -v /some/local/dir:/some/dir/in/redis-container my/redis

This mounts the host folder /some/local/dir as the folder /some/dir/in/redis-container in the running container. If e.g. redis stores its data in that folder you're all set to go and reboots/crashes can be survived.

More info about docker volumes check out the docs. Another great article is the also from the docker website, Managing Data in Containers.

EDIT: After comments I clarified the answer - the data is lost if the container can't be restarted (total crash).

like image 182
wassgren Avatar answered Sep 21 '22 11:09

wassgren