Managing Docker Volumes On Windows

Data stored inside a Docker container is not designed to be persistent. Since Containers need to be portable and even disposable, Docker has created a concept called volumes for storing persistent data outside of a container. In this video, Matt will go over the difference between volumes and bind mounts in Docker. We’ll cover how to create a docker volume, how to list current docker volumes, how to attach a volume to a container, how to attach a volume to multiple containers, how to inspect an existing volume to find it’s physical location on the container host and also how to remove a volume.

Prerequisites include: Docker

In this video I'm going to talk to you about managing, Docker volumes on windows. Docker volumes are the preferred way for handling persistent data created by and used by Docker containers. Let's take a look at how this works. If you want to store persistent data for containers. There are a couple options first. I'll show you how to use a bind Mount. I'm currently in a folder called data on my C drive if I list. The contents of this folder. You can see that I have 5 text files if I want to make this folder available to a container. I can Mount it when starting the container. Let's go ahead and run a container using Docker run. I'm going to run this container in interactive mode, then specify Dash V. Here I'm going to put the path to my data folder followed by a colon then I will specify the path inside the container where I would like this folder to be mounted for this. I'm going to specify the shared data folder on the C drive then I'll specify the Windows Server core image and finally I'll specify that I want to run PowerShell. Once I'm inside the container. Now that I'm inside the new container if I list. The contents of the C Drive. You can see that I have a shared data folder. Let's go into that folder and list the contents. Here are my 5 test files that are located on my container host. I can also create files in this folder, which will be available to other containers or my container host. Let's go ahead and run new item to create a file called container test we can see here that the new file has been created from within the container. Now I'll exit this container, which will shut it down if I run. Docker PS you can see that there are currently no running containers. Now let's list. The contents of the data folder again from my container host and we could see the new file that was created from inside the container called container test bind mounts have some limited functionality, however, so volumes are the preferred way to accomplish what we're trying to do to get started with volumes. We can run the same command to start up a container. But this time with a couple small differences where we specified the volume instead of using the path on the container hosts file system. I'm going to use the word host data as the name of a volume. I want to create an use. From inside the new container if I list the contents of the C Drive. You can see again that I have a folder called shared data. If I list. The contents of that folder. It is currently empty because we created a blank volume. Now let's run control. PQ, which will take us out of the running container but keep it running in the background from the container host. Let's run. Docker volume LS this will list. The current volumes on this container host. I have a volume called host data, which was created when I specified it in the Docker Run Command. If I run docker PS we can see our running container. Let's stop that container using Docker stop. Now we have no running containers, so let's remove the stop containers by running Docker RM. If I list the volumes again, you can see that the host data volume is still available and can be mounted to new containers. Another way to create a volume is to use the Docker Volume Create Command. If you don't specify a name Docker will give it a name, which is a long list of random characters. Otherwise, you can specify a name here. I'm going to call this volume log data. Now we can see it in the list when we list. The volumes again. Now let's go ahead and Mount that to a new container. I'm going to use Docker run again and for the volume. I'm going to specify the volume that I just created and Mount it to see log data. From inside the container, I'm going to go into the log data folder and create a couple files right now. There are no files in this directory, so let's go ahead and create some. Now I have 2 log files in this directory. Let's run control. PQ again to exit this container while it's still running. While that containers running let's start up a new container with the same volume mounted. If we run a listing on the log data folder and the new container. We can see the 2 log files being shared. Now let's exit this container. I currently still have one running container in 2 exited containers. I'm going to go ahead and stop all running containers. Then run docker RM to remove all exited containers. Let's go ahead and list the volumes again, the log data. Volume is still available to be mounted to future containers. If I just run Docker Volume. I'll get some usage help for the command. We already looked at create so let's move on to inspect if I run. Docker volume inspect against the log data volume. It will return the properties for that volume, including the Mount Point, which is the physical path to the volume on the container host. Let's open that folder using invoke item and have a look. Under the log data folder there is a folder called underscore data. If we open that we can see the files that were created from the container earlier. To delete a volume we can run Docker volume. RM followed by the name of the volume you want to delete. Now, if I list the volumes log data is no longer there. Finally we can use prune to remove all unused local volumes. This will delete all volumes that are not mounted to a running or stopped container. You want to be careful with this command, so there is a warning and a prompt to make sure that you are sure that you want to do this, if I type Y, and hit enter it will. Show me which volumes were deleted. And if I list my volumes again, you can see that they have all been deleted thanks for watching.