Docker containers are an excellent way to test your application in a virtualized environment. However, these container runtimes can also be managed using Docker volumes which allow you to share data between two or more of them without the need for copying files back and forth. This tutorial will show how to create and manage Docker Volumes on Windows machines.
The “docker-compose volumes” is a tool that allows users to create and manage Docker volumes on Windows. The process for creating a volume is quite simple and straightforward, but the process for managing volumes can be difficult.
Docker volumes are the primary method of storing and using persistent data generated by Docker containers. Let’s look at how this works on Windows by looking at how to construct Docker volumes. You’ll also learn how to deal with them.
Matt McElreath, a TechSnips contributor, developed a video to accompany this blog article. Feel free to look at your watch or continue reading if you prefer text.
Persistent Data Storage
There are a variety of solutions for storing permanent data for containers. To begin, I’ll demonstrate how to utilize a bind mount. I’m now in the data folder on my C: drive. I have five text files in this folder, as you can see from the contents list.
C:data contains a number of text files.
If I want a container to have access to this folder, I can mount it when the container starts.
Let’s try running a container using docker run. I’m going to run this container interactively, then use the -V option. I’ll enter the path of my data folder here, followed by a colon, and then the location within the container where I’d want this folder to be mounted.
I’m going to use the C:shareddata folder for this. Then I’ll provide the Windows server core image and, once inside the container, I’ll declare that I want to execute PowerShell.
run docker -it -v c: microsoft/windowsservercore powershell data:c:shareddata
If I display the contents of C: now that I’m within the new container, I can see that I have a shareddata folder.
In a Docker container, listing directory contents
Let’s take a look inside that folder and see what’s within. Here are the links to my five test files on my container host.
Files on the server
In this folder, I may also create files that will be accessible to other containers or my container host. Let’s create a file named containertest by running new item.
On the container host, create a file
The new file was generated from inside the container, as seen above. Now I’ll run exit from this container, which will shut it off.
You can check that there are presently no running containers by executing docker ps.
Containers that are currently in use
Data Counting in Docker Volumes
Let’s go through the contents of the data folder from my container host once again.
Files on the container host are listed.
We can view the new containertest file that was produced from inside the container. Bind mounts, on the other hand, have restricted functionality, hence volumes are the ideal method of achieving our goal. We can start using volumes using the same command we used to start a container, but with a few minor variations. Instead of using the path on the container hosts’ file system to provide the volume, I’m going to use the term hostdata as the name of the volume I want to build and utilize.
If I list the contents of C: from inside the new container, I can see that I have a folder named shareddata.
file listing
Because we established a blank disk, the contents of that folder are presently empty. Now press Ctrl-P-Q to exit the currently running container while keeping it running in the background.
Run docker volume ls from the container host. The current volumes on this container host will be listed. I have a volume named hostdata that was created when I used the docker run command to specify it.
Docker volumes are listed.
We can view our operating container if I run docker ps.
List of active containers
Docker stop will stop that container. We don’t have any containers operating right now.
Docker container shutdown
Run docker rm to remove the halted containers. You can see that the hostdata volume is still accessible and may be mounted to new containers if I list the volumes again.
Docker volumes are listed.
Docker Volumes Creation
The docker volume create command is another option for creating a volume. If you don’t give it a name, Docker will choose one for you from a huge list of random characters. If not, you may enter a name here. This volume will be known as logdata. When we list the volumes again, we can see that is now in the list.
New Docker volume creation
That will now be mounted to a new container. Docker run once again, this time specifying the volume you just built and mounting it to c:logdata.
> docker run -it -v logdata:c:logdata microsoft/windowsservercore powershell
Create a handful of files in the logdata folder from inside the container. There are currently no files in this directory, so create some immediately.
PS> New-Item -Name Log1.txt -ItemType File PS> New-Item -Name Log2.txt -ItemType File
In this directory, I now have two log files.
C:logdata contains two files.
To quit this container while it is still running, press Ctrl-P-Q again. Start a new container with the same volume mounted while that one is running.
> docker run -it -v logdata:c:logdata microsoft/windowsservercore powershell
The two log files are shared when we perform a listing on the logdata folder in the new container.
Containers have access to two log files.
Exit this container now. One operating container and two departed containers should still be present.
Two containers are still operational.
Stop all operating containers, then docker rm to delete all exiting ones.
Docker containers being removed
List the volumes once again. The logdata disk is still accessible for future containers to mount.
The quantity is still available.
You’ll see some use assistance with the command if you run docker volume.
Volume syntax in Docker
Docker Volumes Inspection
Let’s move on to inspect now that we’ve looked at create. When I run docker volume inspect on the logdata volume, it returns the volume’s characteristics, including the mount point, which is the container host’s physical path to the volume.
Docker Volumes Inspection
Let’s have a peek at that folder using Invoke-Item. There’s a data folder under the logdata folder. We may view the files that were produced from the container previously if we open that.
Earlier generated files
Docker Volumes Delete
To remove a volume, type docker volume rm followed by the volume name you wish to remove.
> docker volume rm logdata
Logdata is no longer there when I list the volumes.
Docker volumes are listed.
Finally, prune may be used to get rid of any unwanted local volumes. All volumes that are not mounted to a running or stopped container will be deleted.
Examining the prune option
Because you should exercise caution while using this command, there is a warning and a prompt to ensure that you are certain you want to proceed. It will show me which volumes were removed if I write Y and click enter.
You can see that all of my volumes have been removed if I list them again.
There are no Docker volumes.
Summary
You should have a decent understanding of how to manage Docker volumes on Windows after reading this blog article. Docker is an excellent container management system. You’ll be unstoppable with its Windows support and your newly acquired Docker volume management abilities!
The “docker volume create” is a command-line tool that allows users to create and manage Docker volumes on Windows. With this tool, you can also mount new volumes or attach existing ones to your containers. The “docker volume create” is installed by default with the Docker installation package.
Related Tags
- docker-compose volumes example
- docker volume windows
- docker run -v
- docker create volume in specific directory
- dockerfile volume example