Share Volume From Host to Container
Dear Readers,
In this article,we will see Share Volume From Host to Container.
What is a Volume?
Volume is a directory inside your container.
First declare directory as a volume then share volume.
Even If we stop container,still we can access the volume.
Volume will be created in one container.
You can declare a directory as volume only while creating container.
We can’t create volume from existing container.
we can share volume across number of containers.
Volume won’t be included when you update an image.
We can map volumes in 2 ways.
1.Share Container to Container.
2.Share Host to Container.
Steps to Follow
Create a container “ktexperts-container” and volume by using Command Line
- Connect to Docker Linux EC2 Terminal through Putty.
- Switch to root user.
- Verify files/directories in current directory (/home/ec2-user).
- Create a container “ktexperts-container” and volume by using Command Line.
- Verify volume.
- Verify files inside the volume.
- Verify files in host volume (/home/ec2-user).
Share volume with another container (second container)
- Create a New Container and Add to the Volume.
- Verify volume.
- Verify files/directories inside the volume.
- Create a directories inside the volume.
- Verify files/directories inside host volume (/home/ec2-user).
- Go inside the first container “ktexperts-container” and verify files in the volume.
Create a container “ktexperts-container” and volume by using Command Line
1. Connect to Docker Linux EC2 Terminal through Putty
Open Linux EC2 Instance Terminal
1 2 3 4 5 6 7 8 9 10 11 |
Using username "ec2-user". Authenticating with public key "imported-openssh-key" __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ 5 package(s) needed for security, out of 13 available Run "sudo yum update" to apply all updates. [ec2-user@ip-172-31-4-99 ~]$ |
2. Switch to root user
1 2 |
[ec2-user@ip-172-31-4-99 ~]$ sudo su [root@ip-172-31-4-99 ec2-user]# |
To see the list of all images
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE |
To see the list of all containers
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
3. Verify files/directories in current directory (/home/ec2-user)
1 2 |
[root@ip-172-31-4-99 ec2-user]# ls Dockerfile ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
Create a container “kt-container-4” and volume by using Command Line
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-4-99 ec2-user]# docker run -it --name ktexperts-container -v /home/ec2-user:/ktexperts --privileged=true ubuntu //bin/bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 2746a4a261c9: Pull complete 4c1d20cdee96: Pull complete 0d3160e1d0de: Pull complete c8e37668deea: Pull complete Digest: sha256:250cc6f3f3ffc5cdaa9d8f4946ac79821aafb4d3afc93928f0de9336eba21aa4 Status: Downloaded newer image for ubuntu:latest root@7ade0d5c2b3a:/# |
4. Verify volume
1 2 |
root@7ade0d5c2b3a:/# ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
5. Verify files inside the volume
we can see the files inside the volume which are there in host (/home/ec2-user).
1 2 3 |
root@7ade0d5c2b3a:/# cd ktexperts/ root@7ade0d5c2b3a:/ktexperts# ls Dockerfile ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
6. Create files inside the volume
1 2 3 4 |
root@7ade0d5c2b3a:/ktexperts# touch kt-1 kt-2 kt-3 root@7ade0d5c2b3a:/ktexperts# ls Dockerfile kt-1 kt-2 kt-3 ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
Exit from the container
1 2 |
root@7ade0d5c2b3a:/ktexperts# exit exit |
7. Verify files in host volume (/home/ec2-user)
1 2 |
[root@ip-172-31-4-99 ec2-user]# ls Dockerfile kt-1 kt-2 kt-3 ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
Share volume with another container (second container)
1. Create a New Container and Add to the Volume
To see the list all containers
1 2 3 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7ade0d5c2b3a ubuntu "//bin/bash" 2 minutes ago Exited (0) About a minute ago ktexperts-container |
Run below command to create a container ” ktexperts-container-1″ with volume “ktexperts”
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run -it --name ktexperts-container-1 --privileged=true --volumes-from ktexperts-container ubuntu //bin/bash root@f5ac011c6b54:/# |
2. Verify volume
1 2 |
root@f5ac011c6b54:/# ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
3. Verify files/directories inside the volume
1 2 3 |
root@f5ac011c6b54:/# cd ktexperts/ root@f5ac011c6b54:/ktexperts# ls Dockerfile kt-1 kt-2 kt-3 ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
4. Create a directories inside the volume
1 2 3 |
root@f5ac011c6b54:/ktexperts# mkdir ktdir-1 ktdir-2 ktdir-3 root@f5ac011c6b54:/ktexperts# ls Dockerfile kt-1 kt-2 kt-3 ktdir-1 ktdir-2 ktdir-3 ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
Exit from the container
1 2 |
root@f5ac011c6b54:/ktexperts# exit exit |
5. Verify files/directories inside host volume (/home/ec2-user)
1 2 |
[root@ip-172-31-4-99 ec2-user]# ls Dockerfile kt-1 kt-2 kt-3 ktdir-1 ktdir-2 ktdir-3 ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
6. Go inside the first container “ktexperts-container” and verify files in the volume
We need to start and attach for going inside the container
Go inside the contianer
To see all the containers
1 2 3 4 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f5ac011c6b54 ubuntu "//bin/bash" 2 minutes ago Exited (0) About a minute ago ktexperts-container-1 7ade0d5c2b3a ubuntu "//bin/bash" 7 minutes ago Up 11 seconds ktexperts-container |
Start Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker start ktexperts-container ktexperts-container |
Attach Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker attach ktexperts-container root@7ade0d5c2b3a:/# |
Verify Volume
1 2 |
root@7ade0d5c2b3a:/# ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
Verify files/directories inside the volume
1 2 3 |
root@7ade0d5c2b3a:/# cd ktexperts/ root@7ade0d5c2b3a:/ktexperts# ls Dockerfile kt-1 kt-2 kt-3 ktdir-1 ktdir-2 ktdir-3 ktexpertsdir1 ktexpertsdir2 ktexpertsfile1 ktexpertsfile2 |
Exit from the container
1 2 |
root@7ade0d5c2b3a:/ktexperts# exit exit |
See the list of all details of the containers
To see the full details of the first container “ktexperts-container”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
[root@ip-172-31-4-99 ec2-user]# docker inspect ktexperts-container [ { "Id": "7ade0d5c2b3a70a1fc75850dffa4b006b02ffeacbcc955bc892266b3bb97f9fd", "Created": "2020-01-10T08:38:54.473172011Z", "Path": "//bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2020-01-10T08:45:59.415993803Z", "FinishedAt": "2020-01-10T08:48:05.881447776Z" }, "Image": "sha256:549b9b86cb8d75a2b668c21c50ee092716d070f129fd1493f95ab7e43767eab8", "ResolvConfPath": "/var/lib/docker/containers/7ade0d5c2b3a70a1fc75850dffa4b006b02ffeacbcc955bc892266b3bb97f9fd/resolv.conf", "HostnamePath": "/var/lib/docker/containers/7ade0d5c2b3a70a1fc75850dffa4b006b02ffeacbcc955bc892266b3bb97f9fd/hostname", "HostsPath": "/var/lib/docker/containers/7ade0d5c2b3a70a1fc75850dffa4b006b02ffeacbcc955bc892266b3bb97f9fd/hosts", "LogPath": "/var/lib/docker/containers/7ade0d5c2b3a70a1fc75850dffa4b006b02ffeacbcc955bc892266b3bb97f9fd/7ade0d5c2b3a70a1fc75850dffa4b006b02ffeacbcc955bc892266b3bb97f9fd-json.log", "Name": "/ktexperts-container", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": [ "/home/ec2-user:/ktexperts" ], "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": true, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": [ "label=disable" ], "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": [ { "Name": "nofile", "Hard": 4096, "Soft": 1024 } ], "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": null, "ReadonlyPaths": null }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/c503b1763f2132e4efce2e48ff5d45b2a8caa296a0fb6925bf3afdba97d595ff-init/diff:/var/lib/docker/overlay2/930830420f8825b517835a9ee4d0c1d668757615c866ca6884ba4f71212b71f8/diff:/var/lib/docker/overlay2/8a0c80fcdefe0f4710c4a2b4d8ef08a64a2fde951efddb1562ea590bf5f8e5a7/diff:/var/lib/docker/overlay2/a49aa2775cbe67c8c927d243a70ba4020123c1a0028cb6bd0a65cafe31e763a7/diff:/var/lib/docker/overlay2/5376ec1a5c6f0ef73b8b4edea814b464a2aa47a6f00dca01796e69bbb00ead24/diff", "MergedDir": "/var/lib/docker/overlay2/c503b1763f2132e4efce2e48ff5d45b2a8caa296a0fb6925bf3afdba97d595ff/merged", "UpperDir": "/var/lib/docker/overlay2/c503b1763f2132e4efce2e48ff5d45b2a8caa296a0fb6925bf3afdba97d595ff/diff", "WorkDir": "/var/lib/docker/overlay2/c503b1763f2132e4efce2e48ff5d45b2a8caa296a0fb6925bf3afdba97d595ff/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "bind", "Source": "/home/ec2-user", "Destination": "/ktexperts", "Mode": "", "RW": true, "Propagation": "rprivate" } ], "Config": { "Hostname": "7ade0d5c2b3a", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "//bin/bash" ], "Image": "ubuntu", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "793bdb0ac7f7fa0472916b0ed39323eb765d2aade26e14e9fb421b5fb7fc62c0", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/793bdb0ac7f7", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "bc8b7152d227bcde136f341602ebc3213439515d334929847784ce25ac19254d", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ] |
To see the full details of second container “ktexperts-container-1”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
[root@ip-172-31-4-99 ec2-user]# docker inspect ktexperts-container-1 [ { "Id": "f5ac011c6b540b0cd4c988b40a63647c25ac41e76b8d09f01d2dd18d87ac58b3", "Created": "2020-01-10T08:43:22.582183935Z", "Path": "//bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2020-01-10T08:43:22.873778419Z", "FinishedAt": "2020-01-10T08:45:04.438898954Z" }, "Image": "sha256:549b9b86cb8d75a2b668c21c50ee092716d070f129fd1493f95ab7e43767eab8", "ResolvConfPath": "/var/lib/docker/containers/f5ac011c6b540b0cd4c988b40a63647c25ac41e76b8d09f01d2dd18d87ac58b3/resolv.conf", "HostnamePath": "/var/lib/docker/containers/f5ac011c6b540b0cd4c988b40a63647c25ac41e76b8d09f01d2dd18d87ac58b3/hostname", "HostsPath": "/var/lib/docker/containers/f5ac011c6b540b0cd4c988b40a63647c25ac41e76b8d09f01d2dd18d87ac58b3/hosts", "LogPath": "/var/lib/docker/containers/f5ac011c6b540b0cd4c988b40a63647c25ac41e76b8d09f01d2dd18d87ac58b3/f5ac011c6b540b0cd4c988b40a63647c25ac41e76b8d09f01d2dd18d87ac58b3-json.log", "Name": "/ktexperts-container-1", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": [ "ktexperts-container" ], "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": true, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": [ "label=disable" ], "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": [ { "Name": "nofile", "Hard": 4096, "Soft": 1024 } ], "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": null, "ReadonlyPaths": null }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/c1874aeedf092eead194c75f697bc3bb8d8105d779dcb334f129fa34e1d1974f-init/diff:/var/lib/docker/overlay2/930830420f8825b517835a9ee4d0c1d668757615c866ca6884ba4f71212b71f8/diff:/var/lib/docker/overlay2/8a0c80fcdefe0f4710c4a2b4d8ef08a64a2fde951efddb1562ea590bf5f8e5a7/diff:/var/lib/docker/overlay2/a49aa2775cbe67c8c927d243a70ba4020123c1a0028cb6bd0a65cafe31e763a7/diff:/var/lib/docker/overlay2/5376ec1a5c6f0ef73b8b4edea814b464a2aa47a6f00dca01796e69bbb00ead24/diff", "MergedDir": "/var/lib/docker/overlay2/c1874aeedf092eead194c75f697bc3bb8d8105d779dcb334f129fa34e1d1974f/merged", "UpperDir": "/var/lib/docker/overlay2/c1874aeedf092eead194c75f697bc3bb8d8105d779dcb334f129fa34e1d1974f/diff", "WorkDir": "/var/lib/docker/overlay2/c1874aeedf092eead194c75f697bc3bb8d8105d779dcb334f129fa34e1d1974f/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "bind", "Source": "/home/ec2-user", "Destination": "/ktexperts", "Mode": "", "RW": true, "Propagation": "rprivate" } ], "Config": { "Hostname": "f5ac011c6b54", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "//bin/bash" ], "Image": "ubuntu", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "9309d6085951a99105857326b80b1d9ca5624165618082067550667d7dfd5d3a", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/9309d6085951", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "bc8b7152d227bcde136f341602ebc3213439515d334929847784ce25ac19254d", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ] |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform