If containers are isolated, how can they communicate to the host machine, perhaps to store data? Because when we create a container from an image, any data generated is lost when the container is removed.
如果容器是隔离的,它们如何与主机通信,也许可以存储数据? 因为当我们从图像创建容器时,删除容器后生成的所有数据都会丢失。
So we need a way to have permanent storage.
因此,我们需要一种永久存储的方法。
We can do so using Bind Mounts and Volumes.
我们可以使用Bind Mounts and Volumes来做到这一点。
There’s not a lot of difference between the two, except Bind Mounts can point to any folder on the host computer, and are not managed by Docker directly.
两者之间没有太大区别,除了Bind Mounts可以指向主机上的任何文件夹,并且不直接由Docker管理。
Let’s start with them. One classic example is logs. Suppose your app creates a log file, inside the container, in /usr/src/app/logs
. You can map that to a folder on the host machine, using the -v
(same as --volume
) flag when you run the container with docker run
, like this: -v ~/logs:/usr/src/app/logs
让我们从他们开始。 一个经典的例子是日志。 假设您的应用在/usr/src/app/logs
的容器内创建了一个日志文件。 您可以映射到主机上的文件夹,使用-v
(同-