Docker learning notes – Chapter 3: container volume

Time:2021-6-13

Recently, when the epidemic broke out, I went home to study the docker of CC teacher, which I bought online in private school. In order to deepen my impression and encourage me to change my salary, I insisted on taking a series of notes to record my growth. However, it’s reported that the latest senior architect course of CC teacher will also be launched, which contains the current mainstream architecture and the latest technology. It’s said that from theory to application, it’s all done with, such as netty, redis, Kafka, zookeeper, Dubbo, nginx + openresty + Kong + Lua, elasticsearch, etc., which are commonly used in interviews, all go deep into the source code level, Now I can’t afford to go out for an interview. I’m really excited to see it. If you have any interested partners, you can go together【Private school online 】 study

Container volume management

brief introduction

If the data generated by the docker container does not generate a new image through the docker commit, so that the data is saved as part of the image, then the data will be lost naturally after the container is deleted.
In order to save data, we use volumes.

Data volume

A volume is a directory or file. It exists in one or more containers and is mounted to the container by docker, but it does not belong to the federated file system. Therefore, it can bypass the union file system and provide some features for continuous storage or sharing of data

  • Data volumes can share or reuse data between containers
  • Changes in the volume can take effect directly
  • Changes in the data volume are not included in the update of the mirror
  • The life cycle of a data volume continues until no container uses it

Add data volume

In docker run, use the – V path, for example: – V / TMP / data, which creates a new data volume under the / TMP / data path of the container.
Similarly, you can use the volume instruction in dockerfile to add one or more data volumes to the image
Information can be viewed through docker inspect.

Mount host directory as data volume

Or use – V Host Directory: container directory, the host directory must be an absolute path

  • For the sake of portability and sharing, this method cannot be directly implemented in dockerfile. Since the host directory is dependent on a specific host, it is not guaranteed that such a specific directory exists on all hosts.
  • By default, docker has read-write access to the data volume, but the data volume can be read-only in this way:
    -v /src/webapp:/opt/webapp:ro

Cannot open directory.: permission denied appears when docker accesses the host directory

Solution: add a -- privileged = true parameter after mounting the directory

Create and mount data volume container

If you want to share data between containers, or use some persistent data from non persistent containers, you’d better create a data volume container with a specified name, and then use it to mount data.

  • Create a data volume container, for example:

       docker run -d -v /dbdata --name dbdata mysql
  • You can use the — volumes from identifier in another container to mount the newly created data volume container

      docker run -d --volumes-from dbdata --name db1 mysql
  • You can mount the corresponding data volume to more containers
  • You can also bridge multiple data volumes to a container by using multiple — volumes from identities
  • Data volume containers can be chained, such as: – volumes from db1