This is a personal memo.
How to use the volume specified by docker or docker-compose.
There is a difference between (1) outer volumes and (2) volumes in services in docker-compose.yml.
docker-compose.yml example
version: "3.7"
volumes:
todo-mysql-data:
services:
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
Volumes in the same hierarchy as version prepares volumes with the specified name.
This is called a named volume
.
volumes:
todo-mysql-data:
--Volumes can be shared by multiple containers. --It does not disappear even if you delete the container (it does not depend on the container)
Volumes in each service are called ** bind mount ** and are distinguished from volumes.
** Represents the process of mounting (sharing) the directory inside the container and the directory on the host side **.
services:
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
In the above example, the volume todo-mysql-data
is mounted on / var / lib / mysql
in the container.
Now you can sync the data in / var / lib / mysql to the volume.
item | Bind mount | Contents |
---|---|---|
Named volume | datavolume:/var/lib/mysql | Mount the specified volume on the specified directory in the container |
Anonymous volume | /var/lib/data | Specify only the directory in the container. A hash value volume is assigned. (It disappears when the container is deleted) |
Share root directory(Relative path) | .:app | Mounts the specified directory in the container on the running directory on the host side. |
Specified by absolute path | /opt/data:/var/lib/mysql | Mount the specified directory in the container to the specified directory on the host side. |
I often see descriptions like .: App
.
By doing this, the data in the container can be reproduced on the host side. (Without this, the data cannot be seen on the host side)
item | Named volume | Anonymous volume |
---|---|---|
Main applications | ・(1)Sharing a volume with multiple containers (example: db)(2)Exclude from synchronization to host side(Example: node_module) | Data that you don't need to share with other containers but want to keep (eg log) |
Delete container | Remains (container independent) | Disappear |
Recommended Posts