×
Creating container volumes in windows 2016
Blog Containers

Creating container volumes in windows 2016

Creating container volumes in windows 2016

In this blog, we will share you creating container volumes in windows 2016 using docker commands.

 

OVERVIEW

In addition, to store the data inside the container while building, we can also set it up the container to access the data outside their environment. For example, create a folder on container host and we can access the folder from inside the container.

 

CREATING CONTAINER FOLDER

  • Create a folder in C:\ drive named containerdata using below command.

mkdir containerdata

Creating container volumes in windows 2016

 

  • Create two new files named file1.txt and file2.txt

Creating container volumes in windows 2016

 

LAUNCHING NEW CONTAINER AND MAP THE FOLDER

  • Create a new container along with mapping using below command.

SYNTAX :  docker run -it -v <sourcefolderpath>:<destination folder path where source folder to be mounted> <container OS image< <process to kickstart during creation>

EXAMPLE: docker run -it -v c:\containerdata:c:\mydata azcontainerr/web powershell

docker – Base command for docker command line interface.

-it – To start the docker container in interactive mode.

-v – to map the local folder (or) data volume to a folder inside the container.

c:\containerdata – It’s the source folder from the container host

c:\mydata – Define the path where the source folder to be mounted.

azcontainerr/web – Image to start the container

PowerShell – To kickstart the PowerShell process inside the container.

 

Creating container volumes in windows 2016

 

  • Once the container up and running, List the files and folders to verify mydata folder is available.

Creating container volumes in windows 2016

 

  • If we list the files inside mydata, we can see our files file1.txt and file2.txt.

Creating container volumes in windows 2016

 

  • Create a new file inside the mydata folder using below command.

New-Item -ItemType file -Name file3.txt -Value ‘container text’

Creating container volumes in windows 2016

 

  • List the directory and we can able to find the third file in the list.

Creating container volumes in windows 2016

 

  • We can able to see the file from the container host.

Creating container volumes in windows 2016

 

  • Quit from the container using below command.

exit

Creating container volumes in windows 2016

 

  • Now container is in stopped mode. We can verify it using docker ps –a command.

Creating container volumes in windows 2016

REMOVING CONTAINER

  • Now delete the container using rm command.

docker rm 9d

Creating container volumes in windows 2016

 

  • There are no containers available now.

Creating container volumes in windows 2016

 

  • You can see all three files available in the c:\containerdata folder from the container host

Creating container volumes in windows 2016

 

  • The data are available after we delete the containers.

 

CREATING PERSISTENT DATA VOLUMES

  • We can create persistent data volumes that can be shared access multiple containers. It’s like virtual volume which can be connected to different containers.

 

  • To create persistent data volume, use the below command.

SYNTAX: docker run -it -v <data volume name>:<Folder name where data volume to be mounted> <container image name> <command to kickstart the process>

EXAMPLE: docker run -it -v mydata:c:\demo azcontainerr/web powershell

docker – Base command for docker command line interface.

-it – To start the docker container in interactive mode.

-v – to map the local folder (or) data volume to a folder inside the container.

mydata – create a data volume

c:\demo – where data volume should be mounted inside the container

azcontainerr/web – Image to start the container

powershell – To kickstart the PowerShell process inside the container.

Creating container volumes in windows 2016

 

  • Once the container is up and running, list the folders to verify demo folder is available.

Creating container volumes in windows 2016

 

  • There is no files are available under demo folder.

Creating container volumes in windows 2016

 

  • Create a new file named test.txt using below command.

New-Item -Itemtype file -Name test.txt -Value ‘hello assistanz’

 

Creating container volumes in windows 2016

 

  • Now disconnect this container by pressing CTRL+PQ keys.

Creating container volumes in windows 2016

 

  • Make sure that container is in running state by executing docker ps command.

Creating container volumes in windows 2016

 

  • Launch a new container with same volume mapping using below command.

docker run -it -v mydata:c:\demo azcontainerr/web powershell

Creating container volumes in windows 2016

 

  • Once container up and running, list the folders to view demo folder.

Creating container volumes in windows 2016

 

  • If we list inside the demo folder, we can see the text file which we created from the other container.

Creating container volumes in windows 2016

 

  • Disconnect from this container by pressing CTRL+PQ keys.

Creating container volumes in windows 2016

 

ABOUT DATA VOLUMES

 

  • Actually, we create, manage and delete data volumes using docker volume command.
  • To list the available data volumes in the container host, use the below command.

docker volume ls

Creating container volumes in windows 2016

 

  • Make sure that container is deleted prior to delete volumes. To delete multiple containers, use the below command.

docker rm –f $(docker ps -q)

Creating container volumes in windows 2016

 

  • To delete the docker volume, execute the below command.

SYNTAX: docker volume rm <volume name>

COMMAND : docker volume rm mydata

Creating container volumes in windows 2016

 

  • You can verify the data volumes using docker volume ls command.

Creating container volumes in windows 2016

 

VIDEO

Thanks for reading this blog. We hope it was useful for you to learn how to share the folders and data volumes inside the container(s).

7 thoughts on “Creating container volumes in windows 2016”

  1. Hi,
    very informative post but is there a way we can attach volumes in host D:\drive on to containers ? or can we perform disk partition inside a container?

  2. Thanks for your comment. Yes, you can mount a drive to a container.

    For example, you have disk drive named e: and you like to mount inside the container, use the below format to mount it.

    docker run -it -v :

    Example: docker run -it -v e:\:d: microsoft/windowsservercore powershell

    We haven’t tried the disk partition inside a container :). If you try from your end feel free to share your inputs.

  3. Thank you for the response!!
    I have disk partition inside a container,as there no GUI as such to partition the disk I have tried with diskpart command inside a container which gives me an error like Virtual Disk service error:The service failed to initialize . Any input on the same would be appreciated
    And also does this volume mounting work on Hyper-V containers as well?? I tried something like this
    docker run -it -isolation=hyperv -v -v c:\appdata:c:\demo microsoft/windowsservercore
    but this seems to throwing an error.

  4. We are able to create a container using HyperV isolation without any problem. Make sure that you have hyper-v role installed. Here is the example to create hyper-v isolation containers.

    docker run -it -v c:\install:c:\temp –isolation=hyperv microsoft/windowsservercore powershell

  5. Hi iam able to create the Hyper-v containers but iam not able to mount the host volume on to containers using above command is the syntax of the command i mentioned in previous command correct?

  6. I am running docker on windows server 2016 and i am unable create volume on D drive.

    PS C:\Windows\system32> docker volume create d:\datadrive
    Error response from daemon: create d:\datadrive: invalid volume name

    Thanks you ..

  7. I am running docker on windows server 2016 and i am unable create volume on D drive.

    PS C:\Windows\system32> docker volume create d:\datadrive
    Error response from daemon: create d:\datadrive: invalid volume name

    Thanks you ..

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.