Docker data persistence and Bind Mount Volume (rpm)

Transfer from https://www.jianshu.com/p/ef0f24fd0674

Docker data persistence There are two main ways:

  • bind mount
  • volume

Docker data persistent data that is not ended with the end of the container, the data present on the host machine - either a specified directory exists in the host's (using the bind mount), or use docker they manage volume (/ var / lib / docker / volumes lower).

 

bind mount

bind mount since the early docker people began to use for the host machine's directory to mount the container. But bind mount at different host systems are not portable, such as Windows and Linux directory structure is not the same, bind mount points to host directory can not be the same. This is also the reason why the bind mount in Dockerfile not appear, because it would not be Dockerfile transplant.

The host-data directory in the current directory on the host machine to mount the container / container-data directory:

docker run -it -v $(pwd)/host-dava:/container-data alpine sh

There are several points to note:

  • The directory path must host machine (accurate to say needs to be a full path /or ~/a path that starts), otherwise it will be as a docker volume rather than volume processing
  • If the directory does not exist on the host machine, docker will automatically create the directory
  • If the container in the directory does not exist, docker will automatically create the directory
  • If the container in the content directory, but the directory on the host machine is not content docker will use the container's directory covering
  • Whether container in the directory if there is content, so long as the content host directory docker will use the directory on the host to be overwritten

 

volume

container volume is bypassing the file system, data is written directly on the host machine, but the volume is being managed docker, all the volume are specified directory on the host machine under docker / var / lib / docker / volumes.

The my-volume mount / mydata directory in the container:

docker run -it -v my-volume:/mydata alpine sh

You can then look to give my-volume of volume:

docker volume inspect my-volume
[
    {
        "CreatedAt": "2018-03-28T14:52:49Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/my-volume/_data",
        "Name": "my-volume",
        "Options": {},
        "Scope": "local"
    }
]

You see, volume directory of the host machine  / var / lib / Docker / Volumes / My-Volume / the _data  . At this point, if my-volume does not exist, docker will automatically create my-volume, and then mount.

Also volume on the host can not specify:

docker run -it -v /mydata alpine sh

At this point docker volume will automatically create a random name, and mount it to the / mydata directory container in. Anonymous volume directory path on the host machine similar to var / lib / Docker / Volumes / 300c2264cd0acfe862507eedf156eb61c197720f69e7e9a053c87c2182b2e7d8 / the _data .

In addition to helping us make docker automatically create volume, we can create your own:

docker volume create my-volume-2

Then this already my-volume-2 mounted to the container:

docker run -it -v my-volume-2:/mydata alpine sh

 

The Dockerfile VOLUME

In Dockerfile, we can also use VOLUME command to declare contaienr in a directory need to be mapped to a volume:

#Dockerfile
VOLUME /foo
This means that, at run time docker, docker creates an anonymous volume, and this volume is bound to the container / foo directory, if the container is / foo directory under already have content, it will copy the contents of the volume. That is, Dockerfile the  VOLUME / foo  with  docker run -v foo alpine /  same effect.

When the Dockerfile VOLUME of each run a new container, will be automatically created for an anonymous volume, if you need to share data between different container, then we still need  docker run -it -v my-volume: / foo  manner / foo specified data stored in the my-volume.

Therefore, VOLUME / foo sometimes be misleading, if not understand, could result in problems.

Guess you like

Origin www.cnblogs.com/jayhou/p/12337934.html