[Docker-compose] How to use unnamed and named volumes of volumes. Bind mount

This is a personal memo.

How to use the volume specified by docker or docker-compose.

Difference between two volumes in docker-compose.yml

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

① Outer volumes

Volumes in the same hierarchy as version prepares volumes with the specified name.

This is called a named volume.

volumes:
  todo-mysql-data:

Features (merits) of named volume

--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 services

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.


## How to write a bind mount There are several ways to write a bind mount.
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.

・ Share the root directory

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)

-Main uses of named volumes and named volumes

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

[Docker-compose] How to use unnamed and named volumes of volumes. Bind mount
How to use StringBurrer and Arrays.toString.
How to use EventBus3 and ThreadMode
How to use equality and equality (how to use equals)
How to use setDefaultCloseOperation () of JFrame
How to use named volume in docker-compose.yml
How to set up and use kapt
[Java] [Maven3] Summary of how to use Maven3
How to use substring and substr methods
How to write and explain Dockerfile, docker-compose
How to use @Builder and @NoArgsConstructor together
How to use args :, environment :, env_file: and .env files with docker-compose command
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
JDBC promises and examples of how to write
[Java] How to use FileReader class and BufferedReader class
[Ruby] How to use gsub method and sub method
Output of how to use the slice method
How to use Segmented Control and points to note
How to use enum (introduction of Japanese notation)
How to use scope and pass processing (Jakarta)
How to use JQuery in js.erb of Rails6
[Java] How to use Calendar class and Date class
How to use Eclipse on my PC with 32bit and 2GB of memory
[Java] How to use compareTo method of Date class
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to write docker-compose
How to use identity
How to use hashes
How to use CommandLineRunner in Spring Batch of Spring Boot
Summary of Java communication API (1) How to use Socket
How to create docker-compose
Summary of Java communication API (3) How to use SocketChannel
How to use JUnit 5
Summary of Java communication API (2) How to use HttpUrlConnection
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
How to use and apply Java's JFrame / Canvas class
How to use Map
How to use OpenCV 4 on Android and view camera live view
How to use trained model of tensorflow2.0 with Kotlin / Java
How to use Font Awesome icon for ul and li
How to call and use API in Java (Spring Boot)
Procedures for passing RealmObject to Fragment and how to use Parceler