Share the Volume In Between Docker Containers (Method-2)
Dear Readers,
In this article,we will see Share the Volume In Between Docker Containers (Method-2).
What is 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.
Share Container to Container.
Share Host to Container.
Steps to Follow
Create a container “kt-container-4” and volume by using Command Line
- Connect to Docker Linux EC2 Terminal through Putty.
- Switch to root user.
- Create a container “kt-container-4” and volume by using Command Line.
- Verify volume.
- Create a files inside the volume.
Share volume with another container (fourth container)
- Create a New Container and Add to the Volume.
- Verify volume.
- Verify files inside the volume.
- Create a file “ktexperts-file3” inside the volume.
- Go inside the fourth container “kt-container-4” and verify files in the volume.
Share volume with another container (fifth container)
- Create a New Container and Add to the Volume.
- Verify volume.
- Verify files inside the volume.
- Create a directories inside the volume.
- Go inside fourth and fifth containers “kt-container-4” , “kt-container-5” and verify files/directories inside the volume.
Create a container “kt-container-4” 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]# |
3. Create a container “kt-container-4” and volume by using Command Line
To see the list of images
1 2 3 4 |
[root@ip-172-31-4-99 ec2-user]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE kt-image-1 latest 0b95b4c500cc 41 minutes ago 64.2MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB |
To see the list of all containers
1 2 3 4 5 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99303599ae9a ubuntu "//bin/bash" 20 minutes ago Exited (0) 19 minutes ago kt-container-3 44637b7c4948 ubuntu "//bin/bash" 31 minutes ago Exited (127) 9 minutes ago kt-container-2 5d477a9118ad kt-image-1 "//bin/bash" 40 minutes ago Exited (0) 17 minutes ago kt-container-1 |
Run below command to create a container ” kt-container-4″ with volume “ktexperts-volume”
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run -it --name kt-container-4 -v /ktexperts-volume ubuntu //bin/bash root@8a5a5171b5f3:/# |
4. Verify volume
1 2 |
root@8a5a5171b5f3:/# ls bin boot dev etc home ktexperts-volume lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
5. Create a files inside the volume
1 2 3 4 5 |
root@8a5a5171b5f3:/# cd ktexperts-volume/ root@8a5a5171b5f3:/ktexperts-volume# ls root@8a5a5171b5f3:/ktexperts-volume# touch ktexperts-file1 ktexperts-file2 root@8a5a5171b5f3:/ktexperts-volume# ls ktexperts-file1 ktexperts-file2 |
Exit from the container
1 2 |
root@8a5a5171b5f3:/ktexperts-volume# exit exit |
Share volume with another container (fourth container)
1. Create a New Container and Add to the Volume
To see the list of containers
1 2 3 4 5 6 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8a5a5171b5f3 ubuntu "//bin/bash" About a minute ago Exited (0) 34 seconds ago kt-container-4 99303599ae9a ubuntu "//bin/bash" 20 minutes ago Exited (0) 19 minutes ago kt-container-3 44637b7c4948 ubuntu "//bin/bash" 31 minutes ago Exited (127) 9 minutes ago kt-container-2 5d477a9118ad kt-image-1 "//bin/bash" 40 minutes ago Exited (0) 17 minutes ago kt-container-1 |
Run below command to create a container along with volume
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run -it --name kt-container-5 --privileged=true --volumes-from kt-container-4 ubuntu //bin/bash root@c0ee7c5722d6:/# |
2. Verify volume
1 2 |
root@c0ee7c5722d6:/# ls bin boot dev etc home ktexperts-volume lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
3. Verify files inside the volume
1 2 3 |
root@c0ee7c5722d6:/# cd ktexperts-volume/ root@c0ee7c5722d6:/ktexperts-volume# ls ktexperts-file1 ktexperts-file2 |
4. Create a file “ktexperts-file3” inside the volume
1 2 3 4 |
root@c0ee7c5722d6:/ktexperts-volume# cat > ktexperts-file3 ktexperts will help us sharing knowledge as well as gaining knowledge together!! root@c0ee7c5722d6:/ktexperts-volume# cat ktexperts-file3 ktexperts will help us sharing knowledge as well as gaining knowledge together!! |
Exit from the container
1 2 |
root@c0ee7c5722d6:/ktexperts-volume# exit exit |
5. Go inside the fourth container “kt-container-4” 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 5 6 7 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0ee7c5722d6 ubuntu "//bin/bash" 12 hours ago Exited (0) 12 hours ago kt-container-5 8a5a5171b5f3 ubuntu "//bin/bash" 12 hours ago Exited (0) 12 hours ago kt-container-4 99303599ae9a ubuntu "//bin/bash" 12 hours ago Exited (0) 12 hours ago kt-container-3 44637b7c4948 ubuntu "//bin/bash" 12 hours ago Exited (127) 12 hours ago kt-container-2 5d477a9118ad kt-image-1 "//bin/bash" 12 hours ago Exited (0) 12 hours ago kt-container-1 |
Start Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker start kt-container-4 kt-container-4 |
Attach Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker attach kt-container-4 root@8a5a5171b5f3:/# |
Verify files inside volume
1 2 3 4 5 6 7 |
root@8a5a5171b5f3:/# ls bin boot dev etc home ktexperts-volume lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@8a5a5171b5f3:/# cd ktexperts-volume/ root@8a5a5171b5f3:/ktexperts-volume# ls ktexperts-file1 ktexperts-file2 ktexperts-file3 root@8a5a5171b5f3:/ktexperts-volume# cat ktexperts-file3 ktexperts will help us sharing knowledge as well as gaining knowledge together!! |
Exit from the container
1 2 |
root@8a5a5171b5f3:/ktexperts-volume# exit exit |
Share volume with another container (fifth container)
1. Create a New Container and Add to the Volume
1 2 3 4 5 6 7 8 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0ee7c5722d6 ubuntu "//bin/bash" 7 minutes ago Exited (0) 4 minutes ago kt-container-5 8a5a5171b5f3 ubuntu "//bin/bash" 9 minutes ago Exited (0) About a minute ago kt-container-4 99303599ae9a ubuntu "//bin/bash" 29 minutes ago Exited (0) 28 minutes ago kt-container-3 44637b7c4948 ubuntu "//bin/bash" 40 minutes ago Exited (127) 18 minutes ago kt-container-2 5d477a9118ad kt-image-1 "//bin/bash" About an hour ago Exited (0) 26 minutes ago kt-container-1 |
Run below command to create a container along with volume
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run -it --name kt-container-6 --privileged=true --volumes-from kt-container-5 ubuntu //bin/bash root@c9d5c092734f:/# |
2. Verify volume
1 2 |
root@c9d5c092734f:/# ls bin boot dev etc home ktexperts-volume lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
3. Verify files inside the volume
1 2 3 4 5 |
root@c9d5c092734f:/# cd ktexperts-volume/ root@c9d5c092734f:/ktexperts-volume# ls ktexperts-file1 ktexperts-file2 ktexperts-file3 root@c9d5c092734f:/ktexperts-volume# cat ktexperts-file3 ktexperts will help us sharing knowledge as well as gaining knowledge together!! |
4. Create a directories inside the volume
1 2 3 |
root@c9d5c092734f:/ktexperts-volume# mkdir ktexperts1 ktexperts2 ktexperts3 root@c9d5c092734f:/ktexperts-volume# ls ktexperts-file1 ktexperts-file2 ktexperts-file3 ktexperts1 ktexperts2 ktexperts3 |
Exit from the container
1 2 |
root@c9d5c092734f:/ktexperts-volume# exit exit |
5. Go inside fourth and fifth containers “kt-container-4” , “kt-container-5” and verify files/directories inside the volume
Go inside fourth container “kt-container-4” and verify files/directories inside the volume
To see all the containers
1 2 3 4 5 6 7 8 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c9d5c092734f ubuntu "//bin/bash" About a minute ago Exited (0) 45 seconds ago kt-container-6 c0ee7c5722d6 ubuntu "//bin/bash" 7 minutes ago Exited (0) 4 minutes ago kt-container-5 8a5a5171b5f3 ubuntu "//bin/bash" 9 minutes ago Exited (0) About a minute ago kt-container-4 99303599ae9a ubuntu "//bin/bash" 29 minutes ago Exited (0) 28 minutes ago kt-container-3 44637b7c4948 ubuntu "//bin/bash" 40 minutes ago Exited (127) 18 minutes ago kt-container-2 5d477a9118ad kt-image-1 "//bin/bash" About an hour ago Exited (0) 26 minutes ago kt-container-1 |
Start Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker start kt-container-4 dkt-container-4 |
Attach Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker attach kt-container-4 root@8a5a5171b5f3:/# |
verify files/directories inside the volume
1 2 3 4 5 |
root@8a5a5171b5f3:/# ls bin boot dev etc home ktexperts-volume lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@8a5a5171b5f3:/# cd ktexperts-volume/ root@8a5a5171b5f3:/ktexperts-volume# ls ktexperts-file1 ktexperts-file2 ktexperts-file3 ktexperts1 ktexperts2 ktexperts3 |
Exit from the container
1 2 |
root@8a5a5171b5f3:/ktexperts-volume# exit exit |
Go inside fifth container “kt-container-5” and verify files/directories inside the volume
Start Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker start kt-container-5 kt-container-5 |
Attach Container
1 2 3 |
[root@ip-172-31-4-99 ec2-user]# docker attach kt-container-5 root@c0ee7c5722d6:/# |
Verify files/directories inside the volume
1 2 3 4 5 |
root@c0ee7c5722d6:/# ls bin boot dev etc home ktexperts-volume lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@c0ee7c5722d6:/# cd ktexperts-volume/ root@c0ee7c5722d6:/ktexperts-volume# ls ktexperts-file1 ktexperts-file2 ktexperts-file3 ktexperts1 ktexperts2 ktexperts3 |
Exit from the container
1 2 |
root@c0ee7c5722d6:/ktexperts-volume# exit exit |
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