Persistent storage for containers
Containers are designed to be transient; they run for a while, and then disappear. Anything inside the container disappears with it, including files and data created during the container's run. This isn't always what we want, of course. If you're running a database inside a container, for example, you usually want that data to persist when the container goes away.
There are two ways of persisting data in a container: the first is to mount a directory from the host machine inside the container, known as a host-mounted volume, and the second is to use what's called a Docker volume. We'll look at both of these in the following sections.
Host-mounted volumes
If you want a container to be able to access files on the host machine's filesystem (such as application code that you're working on and you want to test, for example), the easiest way to do that is to mount a directory from the host on the container. The following example...