Docker-docker manage data

Docker provides three different ways to mount data from the docker host to the container

  • (1) Volumes, volumes are stored in a part of the host file system managed by Docker (/var/lib/docker/volumes) and are completely managed by docker (recommended)

  • (2) bind mount bind mount, you can mount files or directories on the host to the container.

  • (3) tmpfs is only stored in the memory of the host system and will not be written to the file system of the host

1. Data volume container volumes

  • docker volume

    	ls					列出本地可用数据卷
    	
    	create volume名		创建一个数据卷,若不指定名称,则随机生成一个名称
    
(1) Create a volume
  • docker volume create volume名
(2) Mount the volume
  • Ps: --mount

    • It consists of multiple key-value pairs, separated by commas. For example: type=volume,source=volume1,destination=/volume1,ro=true.
      • type, specify the type, can be specified as bind, volume, tmpfs.
      • source, when the type is volume, specify the volume name, and omit this field when the volume is anonymous. When the type is bind, specify the path. The abbreviation src can be used.
      • destination, the path mounted to the container. You can use the abbreviations dst or target.
      • ro is a configuration item, multiple configuration items are directly separated by a comma, and generally use true or false.

———————————————————————————————————

  • (1

    docker container run \
    	-it \								-it	交互模式
     --name shiyanlou001 \					自命名容器
    	-v volume1:/volume1 \				指定卷volume	将名volume1的卷挂载到,容器的 /volume1中
     --rm ubuntu /bin/bash					运行bash,退出后自动删除容器
    
  • (2

    docker container run \			
    -it --name shiyanlou002 \
    --mount type=volume,src=volume1,target=/volume1 \	type:挂载类型 	src:源数据卷	target:挂载目录	
    --rm ubuntu /bin/bash
    
  • (3
    Different containers can inherit the same volume, using parameters

    run 参数:
    --volumes-from volume名		继承volume卷
    

———————————————————————————————————

2. Bind mount

  • If the directory does not exist, it will be created automatically

  • (1 volume

    docker container run \
     -it \
     -v /home/shiyanlou:/home/shiyanlou \  		-v	主机目录地址:容器目录地址
    --name shiyanlou003 \
    --rm ubuntu /bin/bash
    
  • (2 --mount

    docker container run \
    -it \
    --mount type=bind,src=/home/shiyanlou,target=/home/shiyanlou \		type:挂载类型	src:源目录  target:目标目录
    --name shiyanlou004 \
    --rm ubuntu /bin/bash
    

3. Temporary file system tmpfs

  • Only in the host's memory, when the memory is stopped, the corresponding data will be removed

    docker run \
     -it \
    --mount type=tmpfs,target=/test \
    --name shiyanlou008 \
    --rm ubuntu bash
    

4. Data backup

docker container run \
--volumes-from ShiyanlouVolume \
-v /home/shiyanlou/backup:/backup \				挂载目录到容器中
ubuntu tar cvf /backup/backup.tar /vdata/		打包数据,到挂载目录中

5. Data recovery

docker container run \
--volumes-from ShiyanlouVolume \
-v /home/shiyanlou/backup:/backup \
ubuntu tar xvf /backup/backup.tar -C /

与数据备份道理一致

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/114416987