简体   繁体   English

当docker容器崩溃时,数据会发生什么

[英]What happens to the data when a docker container crashes

We are trying to move onto Docker for deployment purpose. 我们正试图迁移到Docker以进行部署。 Our architecture requires to have a redis, a mongodb and several nodejs and java based Docker containers. 我们的架构需要有一个redis,一个mongodb和几个nodejs以及基于java的Docker容器。

So my question is, if suppose the redis/mongodb docker container crashes, do we loose all the data that it had? 所以我的问题是, 如果假设redis / mongodb docker容器崩溃了,我们是否会丢失它拥有的所有数据?

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? 这甚至可以通过Docker实现这一点,还是与此无关?

Any help or comments will be greatly appreciated. 任何帮助或评论将不胜感激。

Thanks 谢谢

If a container crashes, you won't lose any data - at least not more than with a regular application crash. 如果容器崩溃,您将不会丢失任何数据 - 至少不会超过常规应用程序崩溃。

The container itself is unlikely to crash (after all, it's only an envelope for your application(s)). 容器本身不太可能崩溃(毕竟,它只是你的应用程序的信封)。 Your application(s) running in a container can crash, and if they do, their data will still be on the container filesystem . 您在容器中运行的应用程序可能会崩溃,如果这样,他们的数据仍将在容器文件系统上 All you have to do in such a situation is to restart the failed container. 在这种情况下,您所要做的就是重新启动发生故障的容器。

One case where you could lose something is if you explicitly tell Docker to remove the container when it's not running anymore ( --rm option). 如果您明确告诉Docker在不再运行时删除容器( --rm选项),则可能会丢失一些内容。

That being said, for IO-intensive applications such as databases, it is highly recommended to host data on Docker volumes , for performance reasons (a docker volume is a traditional filesystem, while the container default filesystem is a stack of layers and will be slower). 话虽如此,对于IO密集型应用程序(如数据库),强烈建议在Docker 上托管数据,这是出于性能原因(docker 是传统的文件系统,而容器默认文件系统是一堆层,速度会慢一些)。

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. 但是,通常容器可以重新启动并继续 - 在这种情况下,数据不会丢失。

Eg - the following sequence from the docker docs illustrates how container startup work. 例如,docker文档中的以下序列说明了容器启动的工作原理。 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. 每当执行docker run命令时,都会启动一个包含新数据的新容器。 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? 那么,你应该如何设置docker以保持数据的完整性? I think that a good approach is to keep the important data mounted in a volume . 我认为一个好方法是将重要数据保存在volume Volumes are simply external folders (ie 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. 卷只是保存数据的外部文件夹(即来自主机系统的文件夹),即使重新安装整个docker守护程序,这些数据也不会丢失。

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. 这将主机文件夹/some/local/dir为正在运行的容器中的文件夹/some/dir/in/redis-container container。 If eg redis stores its data in that folder you're all set to go and reboots/crashes can be survived. 如果例如redis将其数据存储在该文件夹中,那么您将全部设置为启动并重新启动/崩溃。

More info about docker volumes check out the docs . 有关docker volumes的更多信息,请查看文档 Another great article is the also from the docker website, Managing Data in Containers . 另一篇很棒的文章也是来自docker网站的“ 管理容器中的数据”

EDIT: After comments I clarified the answer - the data is lost if the container can't be restarted (total crash). 编辑:评论后我澄清了答案 - 如果容器无法重新启动(完全崩溃),数据将丢失。

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

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