top of page

Working with Docker Container Volumes

  • Mohan Sekar
  • Nov 14, 2017
  • 2 min read

Docker Volumes

1.Volume can be declared in two different ways.

a) On the command-line, with the -v flag for docker run.

docker run -d -v c:\website\site_name -it microsoft/windowsservercore powershell

b) Within a Dockerfile, with a VOLUME instruction.

VOLUME C:\website\site_name

2.Volumes can be shared across containers.

a) Create a container with volume

docker run –d –v c:\website\site_name –it microsoft/windowsservercore powershell

Then, add some text file with content

PS C:\>Get-Date > c:\website\site_name\date.txt

b) Let’s start another container with the same volume.

docker run –volumes-from alpha microsoft/windowsservercore powershell

PS C:\>Get-Content c:\website\site_name\date.txt

Sunday, November 12, 2017 11:05:57 PM

3.Sharing a directory between the host and a container

a) Create one share folder in host

New-Item -Name Share -ItemType Directory

b) This will mount the bindthis directory into the container at C:\Temp\Share.

docker run -it -v C:\Temp\Docker\Share:C:\Temp\Share microsoft/windowsservercore powershells

Note: It defaults to mounting read-write but we can also mount read-only.

docker run -it -v C:\Temp\Docker\Share:C:\Temp\Share:ro microsoft/windowsservercore powershell

4.Chaining container volumes together.

a) Create a new container.

docker run -it -v C:\Temp\appvolume --name appdata microsoft/windowsservercore powershell

b) Let's go to appvolume folder

cd C:\Temp\appvolume

c) Let's create one file with data.

"Hello" > data.txt

d) Exit the container.

exit

5.Use a data volume from our container.

a) Create a new container.

docker run -it --volumes-from appdata --name appserver1 microsoft/windowsservercore powershell

b) Let's view our data.

cat C:\Temp\appvolume\data.txt

c) Let's make a change to our data.

"Good bye" >> C:\Temp\appvolume\data.txt

d) Exit the container.

exit

6.Chain containers with data volumes.

a) Create a third container.

docker run -it --volumes-from appserver1 --name appserver2 microsoft/windowsservercore powershell

b) Let's view our data.

cat C:\Temp\appvolume\data.txt

c) Exit the container.

exit

d) Tidy up your containers.

docker rm -v appdata appserver1 appserver2


Comments


RECENT POSTS:
SEARCH BY TAGS:

© 2023 by NOMAD ON THE ROAD. Proudly created with Wix.com

  • b-facebook
  • Twitter Round
  • Instagram Black Round
bottom of page