如果机器重新启动,避免容器关闭

Avoid containers to shutdown if machine is rebooted

我有一个 OSX,我想知道是否可以在 OS 重新启动之间保留一个容器。我目前正在使用我的机器来托管我的代码,并使用它来安装平台或语言,如 Node.js 和 Golang。我想在一个容器内创建我的环境,并将我的代码留在其中,但如果我的机器重新启动也不会丢失容器。可能吗?我没有找到任何相关内容。

  1. 如果您的系统重新启动,您的容器永远不会被杀死,除非您使用 --rm 启动容器,它将在停止时删除。
  2. 如果您使用 docker run -dit --restart always my_container
  3. 启动容器,您的容器将自动重启
  4. 根据 " 也将我的代码留在其中" 这个问题令人担忧,有两种解决方案可以避免丢失数据或代码以及任何其他配置。

您丢失数据是因为

It is possible to store data within the writable layer of a container, but there are some downsides:

The data doesn’t persist when that container is no longer running, and it can be difficult to get the data out of the container if another process needs it.

https://docs.docker.com/storage/

所以这是解决方案。

Docker offers three different ways to mount data into a container from the Docker host: volumes, bind mounts, or tmpfs volumes. When in doubt, volumes are almost always the right choice. Keep reading for more information about each mechanism for mounting data into containers.

https://docs.docker.com/storage/#good-use-cases-for-tmpfs-mounts

在这里你可以如何持久化 nodejs 代码和 golang 代码

docker run -v /nodejs-data-host:/nodejs-container -v /go-data-host:/godata-container -dit  your_image

根据 packages|runtimes(nodejs 和 go),如果你的容器被杀死或停止,它们会持续存在,因为它们存储在 docker 图像中。