In order to eliminate the following experiences and selfish beliefs I wrote it from the perspective that it was easy to understand if I thought about it this way.
.:! The initial confusion
--All volume is for mounting the host side
--anonymous volume is degraded named volume?
There are the following mechanisms of Docker, and I think that the mechanism to solve each is volume. --Normally, when the container is stopped, the data in the container is not retained. --Host-Container is treated as an independent environment and files cannot be shared --Containers-Containers are treated as independent environments and data cannot be shared.
--host volume? (I don't know the exact name) --anonymous volume (I often see the description anonymous volume? Anonymous volume) --named volume
Let's start with the cases where each is needed
Normally, when the container is stopped, the data in the container is not retained (= not persisted) The mechanism to make this persistent is volume Example) For everything that should not be lost each time, including DB data
While Docker treats the host-container as an independent environment, it is possible to mount a specific directory on the host side and synchronize data between the host and container. Example) When the file edited on the host is reflected in the container at any time, such as in the development environment.
Normally, containers are treated as independent environments, and files are not shared. Easy container-to-container file sharing by using named volume Example) When sharing with static content between Web server (container) and application server (container)
I think this is enough for cases where you only want to ensure persistence, do not need to synchronize with the host, and do not need to share with other containers. Also, as part of a persistent, shared volume, I feel that it is effective in cases where you dare to maintain independence between containers. For example, some directories under the volume on which the host is mounted can be separated as anonymous volume, and the path specified by anonymous volume can ensure independence between containers.
host mount: <host_path>:<container_path> Mount the host directory and synchronize with the path in the container
volumes: - .:/myapp # .(Current directory)Mount and inside the container/Sync with myapp
named volume: <volume_name>:<container_path> It is easy to share with other containers because it is named, which treats the path in the container as volume and makes it persistent. ** Even under the host mounted path, the named volume is treated as another volume and separated from the host side **
volumes: - node_modules:/myapp/node_modules #Persist path in the container as volume, node_Manage with the name modules
anonymous volume: <container_path> The difference from named volume is that it is anonymous, it has no name (but it is assigned a random hash), and it should be fine if you just want to make it persistent without assuming it to be shared with other containers.
volumes: - /myapp/node_modules #Describe only the path in the container and make it persistent as volume
(Scheduled to be updated at any time)
Frequent options you should know to become "Docker Compose, I understand" --Compose file version 3 (3.8) reference --- Qiita