首页 > 代码库 > Docker-3:Data Volume
Docker-3:Data Volume
Add a volume
You can also use the VOLUME
instruction in a Dockerfile
to add one or more new volumes to any container created from that image.
docker run -d -P --name web -v /webapp training/webapp python app.py #add a volume /webapp for container web
docker inspect web
it will shows something like
"Mounts": [ { "Name": "9773d6771225964ee795b675c3c05019fae21dfa7251264eb4a8e42fbc1e5232", "Source": "/var/lib/docker/volumes/9773d6771225964ee795b675c3c05019fae21dfa7251264eb4a8e42fbc1e5232/_data", "Destination": "/webapp", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],
"Source" is the dir in the local host while "Destination" is the dir in the container.
Mount a host dir as a data volume
In addition to creating a volume using the -v
flag you can also mount a directory from your Docker engine’s host into a container.
docker run -d -P --name web -v /src/webapp:/webapp:ro training/webapp python app.py
/src/webapp is the newly created dir in the host while /webapp is the dir in the container. "ro‘ means read only.
The host directory is, by its nature, host-dependent. For this reason, you can’t mount a host directory from Dockerfile
because built images should be portable. A host directory wouldn’t be available on all potential hosts.
Mount a shared-storage volume as a data volume
In addition to mounting a host directory in your container, some Docker volume plugins allow you to provision and mount shared storage, such as iSCSI, NFS, or FC.
Docker-3:Data Volume