简体   繁体   中英

Difference between data and host volume in Docker containers?

As far as I know, there are 2 options for using volume in Docker:

1. Mount a host directory as data volume:

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 
-v <host directory>/data:/var/opt/mssql/data 
-d mcr.microsoft.com/mssql/server:2019-latest

2. Use data volume containers:

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 
-v sqlvolume:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2019-latest

My questions:

1) In the 1st option, I think data in the /var/opt/mssql/data is completely kept on <host directory> . But in the 2nd option, where is it kept in the Docker data files?

2) Let's say I have a currently used container where /var/opt/mssql/data is stored and then decide to mount it to a <host directory> . In this scene, can I move this data directly to the host directory using the docker run command in the 2nd option? Or should I backup and restore this data?

3) When mounting data in a host directory and deleting all the containers and uninstalling Docker, we still have the data. So, after reinstalling the Docker and mounting the same database to the same host, will we get the same data before uninstalling Docker?

  1. Your bind mounted data is kept in the bind-mounted folder on your host. Nothing really mysterious about that. For docker volumes, in most typical situations, the data is kept on your docker host. The exact place and folder architecture depends on your configuration, more specifically on the storage driver in use . Meanwhile, inspecting /var/lib/docker/volumes is probably a good start to look under the hood. Note that some drivers support storing data over the network (eg vieux/sshfs ...), but from your question I doubt you use any of those at the moment.

  2. I'm not totally sure what you are asking here. Meanwhile, one feature available with docker volumes (and not with bind mounts, ie mounting a host folder to the container) is the ability to copy the content from the path in the image to a freshly created volume when the container starts.

  3. Data is data, wherever you keep it. If you have any sort of (thoroughly tested,) backup. you will always be able to restore it and feed it to your new container. The situation is usually quite easy to visualize regarding bind-mounts for docker beginners (ie, backup the folder on the host. restore it and bind-mount it back to a new container). If you decide to work with volumes (which is probably a good suggestion as explained in the volume documentation introduction ), see Backup, restore, or migrate data volumes to get a first idea of how to proceed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM