# Environmental centos7.4, Docker Version 17.12.0-ce
Docker Volume creation, backup, nfs store
#docker volume
The data storage container, i.e. the container destroy all data deleted
data to be retained (persistent data), to be stored in the outer container
docker Volume file or a directory, mount the container used docker
- docker volume
- bind muount # mount any directory or file
- Volumes # fixed directory path, docker management
- tmpfs mounts # stored in the host memory
docker volume documents the official website address https://docs.docker.com/storage/volumes/
#bind muount examples
# Docker mount the file or directory to the container # Create a directory and file
mkdir -p /data/test1
echo 'Docker Volume'> / Data / test1 / test.txt # create the container, -v mount directory (the default read and write permissions)
docker run -dit --name busybox1 -v /data/test1:/data/test1 busybox
# View
Exec busybox1 CAT /data/test1/test.txt Docker # modified files, see whether changes in local
docker exec busybox1 echo my files>>/data/test1/test.txt
docker exec busybox1 cat /data/test1/test.txt
cat /data/test1/test.txt
To ensure Dockerfile portable, can not be used to build the mirror bind muount
#tmpfs mounts
# Use --tmpfs
docker run -dit --name tmpfs-test --tmpfs /app busybox
#Volumes
# Do not specify a mount directory, the default directory / var / lib / Docker / Volumes // _ the Data /
#docker Volumes manageable, officially recommended
# Use Volumes
# Is not specified mount directory, created automatically volumes / <ID number> / _ data /
RUN -dit --name busybox2 -v Docker / the Data / busybox the Test # Create a project file in the container
Exec busybox2 Touch /data/test/test2.txt Docker # view the file path in the host
find /var/lib/docker/volumes -name test2.txt
docker inspect busybox2 |grep Source
#docker volume order management
# Create volume
Volume the Create Volume-test1 Docker # View parameters
docker inspect volume-test1 #使用volume
docker run -dit --name busybox3 -v volume-test1:/volume busybox #查看
docker inspect -f {{.Mounts}} busybox3
# View docker data volumes
Volume LS Docker # delete the data volume is not used (with caution)
docker volume prune
Bind mounts and volumes through -v or --volume mount, tmpfs use --tmpfs
three of these can be used --mount mount, format more clearly, the official recommended
#docker volume data sharing
- Mount same directory
- Use volume container
# Data volume container volume container
# --Volumes-from the name of the vessel, the vessel will be delivered to mount directory (volume parameter passing)
# Create a container busybox4 (do not start)
mkdir -p /data/test2
echo 'busybox4'>/data/test2/test.txt
docker create --name busybox4 -v /data/test2:/data/test2 busybox
# --Volumes-volume containers from other
docker run -dit --name busybox5 --volumes-from busybox4 busybox #查看
docker exec busybox5 cat /data/test2/test.txt
# Mount a plurality of data volumes containers
docker run -dit --name busybox6 --volumes-from busybox4 --volumes-from busybox1 busybox
docker exec busybox6 ls / data / # View
#docker volume backup
- Backup volume mount directory
- --Volumes-from the container using a backup
# Container using data volumes of backup volumes busybox4
docker run --rm --volumes-from busybox4 -v $(pwd):/backup busybox \
takes CVF /backup/backup.tar / data / test2 /
.
Docker volume above examples, all in a single host docker
docker cluster environment, requires the use of shared storage, distributed storage
#docker volume using NFS storage
# NFS server, configuring nfs share
yum install nfs-utils rpcbind -y
mkdir -p /data/nfs/docker
echo "/data/nfs *(rw,no_root_squash,sync)">>/etc/exports
exportfs -r
systemctl start rpcbind nfs-server
systemctl enable rpcbind nfs-server
showmount -e localhost
# Nfs client
yum install -y nfs-utils rpcbind
# Create a volume connection 172.16.50.43:/data/nfs
docker volume create --driver local \
--opt type=nfs \
--opt o=addr=172.16.50.43,rw \
--opt device=:/data/nfs \
volume-nfs
# View
docker volume ls
docker volume inspect volume-nfs
# Container using the volume-nfs
docker run -dit --name busybox7 -v volume-nfs:/nfs busybox #查看
docker inspect -f {{.Mounts}} busybox7
-h df | grep / the Data / nfs # Volume directory / var / lib / docker / volumes / volume-nfs / _data automatically mounted on the nfs service
# Container create test file
docker exec busybox7 touch /nfs/testfiles.txt
# Use volume driver can achieve more backend
# Delete the test container, volume
RM--f -v tmpfs Docker Test busybox {1,2,3,4,5,6,7} # -f force delete, -v delete volume
docker volume prune