Docker 主机重启时容器数据丢失
Docker container data lost on host restart
我在 ubuntu 服务器上有 docker 带有 redis 服务器 运行 的容器,它跟踪由节点 js 应用程序递增的计数器也在 运行容器。
当我启动我的 ubuntu 主机时它运行良好,所有容器 redis + nodeapp 都会自动启动。
访问 url,我可以使用节点应用程序增加计数器。
我看到 redis 服务器中的计数器递增并抛出一个 redis 客户端和网页。
我有个问题:
如果我重启主机然后当容器重启时(我使用restart:always
构建我的容器以在主机重启时重启它们)redis 服务器中的计数器降至 0(计数器未保存)。
我注意到,如果我将计数器增加到某个值,比如说 20,如果我 从主机停止容器 redis 和 启动那个容器,计数器再次保存到20。并且主机重启后计数器没有从 0 开始,但之前的值是 20,因此计数器已保存。
所以我的问题是当主机重启时如何保存数据(redis容器中的计数器)?为什么在我的例子中,保存数据的唯一方法是使用 docker 命令 stop 停止容器?
你见过http://redis.io/topics/persistence吗?
By default Redis saves snapshots of the dataset on disk, in a binary
file called dump.rdb. You can configure Redis to have it save the
dataset every N seconds if there are at least M changes in the
dataset, or you can manually call the SAVE or BGSAVE commands. For
example, this configuration will make Redis automatically dump the
dataset to disk every 60 seconds if at least 1000 keys changed:
save 60 1000
我在 ubuntu 服务器上有 docker 带有 redis 服务器 运行 的容器,它跟踪由节点 js 应用程序递增的计数器也在 运行容器。
当我启动我的 ubuntu 主机时它运行良好,所有容器 redis + nodeapp 都会自动启动。
访问 url,我可以使用节点应用程序增加计数器。 我看到 redis 服务器中的计数器递增并抛出一个 redis 客户端和网页。
我有个问题:
如果我重启主机然后当容器重启时(我使用
restart:always
构建我的容器以在主机重启时重启它们)redis 服务器中的计数器降至 0(计数器未保存)。我注意到,如果我将计数器增加到某个值,比如说 20,如果我 从主机停止容器 redis 和 启动那个容器,计数器再次保存到20。并且主机重启后计数器没有从 0 开始,但之前的值是 20,因此计数器已保存。
所以我的问题是当主机重启时如何保存数据(redis容器中的计数器)?为什么在我的例子中,保存数据的唯一方法是使用 docker 命令 stop 停止容器?
你见过http://redis.io/topics/persistence吗?
By default Redis saves snapshots of the dataset on disk, in a binary file called dump.rdb. You can configure Redis to have it save the dataset every N seconds if there are at least M changes in the dataset, or you can manually call the SAVE or BGSAVE commands. For example, this configuration will make Redis automatically dump the dataset to disk every 60 seconds if at least 1000 keys changed:
save 60 1000