Introduce persistent volumes

As we’ve said before, containers are stored independently, and changes within containers are not persisted.

In the previous article, we said that the operation of the container is to put a container layer around the image. Every modification is made on the container layer, not on the inner image, so when the container stops, the modification disappears.

So let’s say I’m running an application in a container, and its database is in that container, and if the container is unexpectedly stopped, then the data disappears, which is definitely not what we want. How about two or three applications running in separate containers, but their data is connected, that is, they need to use the same database?

This brings us to persistence volumes.

How to play the volume

Start a process as a daemon and map a virtual address to a real address on the machine.

After we start, we use the Docker inspect command to view the details of a container. Because there’s too much information, I’ve just taken some of it and made do with it.

Let’s see if the path on the host and the path inside the container refer to the same memory.

First, let’s take a look at the host’s path and look at its index.html file, which is obviously the welcome page for Nginx.

Then we modify the file to “I am modified ha”, check, it is changed.

Next, we look inside the container and see that index.html has also been modified.

Let’s change the direction, first modify in the container, and then go to the actual address of the machine to see, found that the actual address has also been modified.

Specific principles

What is the reason? Let’s make this very clear.

The address in the container is not a real physical path, but a virtual path. When we first change the actual path, we actually change the contents of the physical address in the actual path. When we look at the contents from inside the container, we actually get the contents from the actual path, the contents of the physical address in the actual path. And vice versa. So basically, they’re pointing to the same piece of memory.

Long press the qr code below, immediate attention [learning Java little sister] receive more learning materials oh!