Creating A Data Volume In Windows PowerShell

We shall now try creating a data volume which can be used by more than one container images. This will let us make use of a common data disk for more than two container images. This is same as creating a Virtual Machine by making use of the same storage account and creating two different Virtual Machines in Azure. This will let you access data outside the container and it will be the same as mounting a folder somewhere outside the container and taking data from it.

Creation of text files

  • Create a folder named "ContainerImageData" inside the local disk C.
  • Then, create two different Notepad files in that folder and name those as textfile1 and textfile2.

    Windows PowerShell

Hosting the data in the Docker Image

  • We shall now map our local folder to the container image so that the data within will be taken into the container image. Run the below command for that.

    docker run -it -v c:\ContainerImageData:c:\mydata mycontainers/web powershell

    Windows PowerShell

    Windows PowerShell
  • This will now run your container. Now, let us try checking all the files located in our container image. Let us run the following command so that we can see all the files that are located on the C Drive.

    ls

    Windows PowerShell
  • Let’s try opening and seeing the files that are in mydata, For that, run the below command.

    ls .\mydata\

    Windows PowerShell

Creating a new file inside the mydata folder

  • This time, we shall try creating a new file inside the mydata folder. For this, run the following command.

    cd .\mydata\
    New-Item -ItemType file -Name textfile3.txt -Value 'hello people'

    Windows PowerShell
  • Again, check for the files inside mydata folder and you will find your files in there. For that, run the below command and after that exit from the container image by running the exit command.

    ls
    exit

    Windows PowerShell

Stopping and removing all the running containers

  • We shall now check for all the containers that we have started and then we will stop them and remove them from running in the background. Run the below command now.

    docker ps

    Windows PowerShell
  • You might get some more images, all you need to do is to just remove the images that are running. For this, run the command as follows.

    docker rm fa -f

Note

Here, replace "fa" with first two letters of your container image.

Windows PowerShell

  • Now, we have removed all the running container images.

Checking the data in local folder

  • Now, try to check the local folder of your container host machine where we created the folder. Initially, we created only two files when we created that folder but now, there will be three files because we have added one more file in the container image. Run the command.

    Cd C:\ ls c:\ContainerImageData\

    Windows PowerShell
  • You can find a new file in here. This says that we have mounted our folder into the container image and made some changes in there, that is reflected back into our local folder. Here, we can even make use of this folder as a common volume. You can even use the same folder for different images as a data source.

Creating data volume

  • Now, we shall try creating a container volume that actually acts as a volume which can be used as a common container volume for other containers. For this, run the command,

    docker run -it -v mydata:c:\myvolumedemo mycontainers/web powershell

    Windows PowerShell
  • We shall now check inside the C drive of the container image. For this, run the command mentioned below. This will show you the new folder that you have created in your container image.

    ls

    Windows PowerShell
  • Navigate to the myvolumedemo folder, the folder must be empty. Run this command.

    cd myvolumedemo
  • Now, run this command to list the files.

    ls
  • Finally, create a new file in the folder by running the below command.

    New-Item -ItemType file -Name text.txt -value 'hello CWT'

    Windows PowerShell
  • Now, get out of the container image by pressing the following buttons. CTRL+P+Q 

    Windows PowerShell
  • This will take you out of the container image.

Running container again with existing data volume

  • Now, run the below command and run a container image by creating a folder in the C drive of the container host.

    docker run -it -v mydata:c:\myvolumedemo mycontainers/web powershell

    Windows PowerShell
  • Again, run the command ls to get all the available files in there.

    ls

    Windows PowerShell
  • Get into that folder now by using the below command. You will be able to see a new file in there.

    Windows PowerShell

  • Now, we can check for the volume which we have created. For that, run the command.

    docker volume ls

    Windows PowerShell

 Deleting all the running containers at once

  • We shall now run the below command so that we will get all the available running containers IDs.

    docker ps -q

    Windows PowerShell
  • Now, we can kill all these running containers by executing the below command.

    docker rm -f $(docker ps -q)
  • This will actually delete all the images at once.
  • Now, we shall delete the data volume also. For this, run the below-given command.

    docker volume rm mydata
  • The above code will work for you only if you have deleted all your available and running container images.

This is how we create container data volumes and work with them both parallelly on our container machine and on the host machine for that container.


Similar Articles