How to Manage Docker Volumes on Windows

Adam Bertram

Adam Bertram

Read more posts by this author.

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 by covering how to manage Docker volumes on Windows.

This blog post has a companion video created by TechSnips contributor, Matt McElreath. Feel free to have a watch or, if you prefer text, read on!

Storing Persistent Data

If you want to store persistent data for containers, there are a couple of options. First, I’ll show you how to use a bind mount. I’m currently in a folder called data on my C:\. If I list the contents of this folder, you can see that I have five text files.

Multiple text files in the C:\data folder
Multiple text files in the C:\data folder

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 -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 C:\shareddata folder. 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.

docker run -it -v c:\Data:c:\shareddata microsoft/windowsservercore powershell

Now that I’m inside the new container, if I list the contents of C:\, you can see that I have a shareddata folder.

Listing directory contents in a Docker container
Listing directory contents in a Docker container

Let’s go into that folder and list the contents. Here are my five test files that are located on my container host.

Files on the host
Files on the 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 containertest.

Creating a file on the container host
Creating a file on the container host

We can see above that the new file has been created from within the container. Now I’ll exit this container which will shut it down by running exit.

If I run docker ps, you can see that there are currently no running containers.

Looking for running containers
Looking for running containers

Enumerating Data in Docker Volumes

Now let’s list the contents of the data folder again from my container host.

Listing files on the container host
Listing files on the container host

We can see the new file that was created from inside the container called containertest. Bind mounts have some limited functionality, however, so volumes are the preferred way to accomplish what we are 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 of 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 hostdata as the name of a volume I want to create and use.

From inside the new container, if I list the contents of C:\, you can see again that I have a folder called shareddata.

Listing files
Listing files

If I list the contents of that folder, it is currently empty because we created a blank volume. Now let’s run Ctrl-P-Q which will take us out of the running container, but keep it running in the background.

From the container host, run docker volume ls. This will list the current volumes on this container host. I have a volume called hostdata, which was created when I specified it in the docker run command.

Listing Docker volumes
Listing Docker volumes

If I run docker ps we can see our running container.

Listing running containers
Listing running containers

Stop that container using docker stop. Now we have no running containers.

Stopping a Docker container
Stopping a Docker container

Remove the stopped containers by running docker rm. If I list the volumes again, you can see that the hostdata volume is still available and can be mounted to new containers.

Listing Docker volumes
Listing Docker volumes

Creating Docker Volumes

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 logdata. Now we can see it is in the list when we list the volumes again.

Creating a new Docker volume
Creating a new Docker volume

You’ll now mount that to a new container. Use docker run again and for the volume specify the volume that just created and mount it to c:\logdata.

> docker run -it -v logdata:c:\logdata microsoft/windowsservercore powershell

From inside the container, go into the logdata folder and create a couple of files. Right now, there are no files in this directory, so go ahead and create some.

PS> New-Item -Name Log1.txt -ItemType File
PS> New-Item -Name Log2.txt -ItemType File

Now I have two log files in this directory.

Two files in C:\logdata
Two files in C:\logdata

Run Ctrl-P-Q again to exit this container while it is still running. While that container’s running, start up a new container with the same volume mounted.

> docker run -it -v logdata:c:\logdata microsoft/windowsservercore powershell

If we run a listing on the logdata folder in the new container we can see the two log files being shared.

Two log files being shared with containers
Two log files being shared with containers

Now, exit this container. You should still have one running container and two exited containers.

Two containers still running
Two containers still running

Now stop all running containers, then run docker rm to remove all exited containers.

Removing Docker containers
Removing Docker containers

List the volumes again. The logdata volume is still available to be mounted to future containers.

Volume still available
Volume still available

If you run run docker volume, you’ll get some usage help for the command.

Docker volume syntax
Docker volume syntax

Inspecting Docker Volumes

We already looked at create, so let’s move on to inspect. If I run docker volume inspect against the logdata 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.

Inspecting Docker volumes
Inspecting Docker volumes

Let’s open that folder using Invoke-Item and have a look. Under the logdata folder, there’s a folder called data. If we open that, we can see the files that were created from the container earlier.

Files created earlier
Files created earlier

Deleting Docker Volumes

To delete a volume, we can run docker volume rm, followed by the name of the volume you want to delete.

> docker volume rm logdata

Now if I list the volumes, logdata is no longer there.

Listing Docker volumes
Listing Docker volumes

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.

Inspecting the prune parameter
Inspecting the prune parameter

You want to be careful with this command, so there’s 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.

No Docker volumes exist
No Docker volumes exist

Summary

In this blog post, you should have gotten a good overview of managing Docker volumes in Windows. Docker is a great container platform. With its support for Windows and your newfound skills at managing Docker volumes, you will be unstoppable!

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!