Share the Volume In Between Docker Containers (Method-1)
Dear Readers,
In this article,we will see Share the Volume In Between Docker Containers.
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 Dockerfile and Add Instructions to create container,volume
- Connect to Docker Linux EC2 Terminal through Putty.
- Switch to root user.
- Create a Dockerfile and Add Instruction to create volume.
- Create image from above Dockerfile.
- Verify images.
- Create container “kt-container-1” from image “kt-image-1”.
- Verify the volume inside the container.
- Create files inside the volume “ktexperts”.
Share volume with another New Container (second container)
- Create a New Container and Add to the Volume.
- Verify volume
- Verify file inside the volume.
- Create a new file inside the volume.
- Go inside the container-1 and verify files in the volume.
Share volume with another New Container (third container)
- Create a New Container and Add to the Volume.
- Verify volume.
- Verify files inside the volume.
- Create a new directory inside the volume.
- Go inside first and second containers “kt-container-1” , “kt-container-2” and verify files/directories inside the volume.
Create a Dockerfile and Add Instructions to create container,volume
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 Dockerfile and Add Instruction to create volume
1 2 3 4 |
[ec2-user@ip-172-31-4-99 ~]$ sudo su [root@ip-172-31-4-99 ec2-user]# vi Dockerfile FROM ubuntu VOLUME ["/ktexperts"] |
Note
:wq! — to quit
4. Create image from above Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-4-99 ec2-user]# docker build -t kt-image-1 . Sending build context to Docker daemon 20.48kB Step 1/2 : FROM ubuntu 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 ---> 549b9b86cb8d Step 2/2 : VOLUME ["/ktexperts"] ---> Running in 7035a5672b93 Removing intermediate container 7035a5672b93 ---> 0b95b4c500cc Successfully built 0b95b4c500cc Successfully tagged kt-image-1:latest |
5. Verify 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 19 seconds ago 64.2MB ubuntu latest 549b9b86cb8d 2 weeks ago 64.2MB |
6. Create container “kt-container-1” from image “kt-image-1”
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run --name kt-container-1 -it kt-image-1 //bin/bash root@5d477a9118ad:/# |
7. Verify the volume inside the container
1 2 |
root@5d477a9118ad:/# ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
8. Create files inside the volume “ktexperts”
1 2 3 4 5 |
root@5d477a9118ad:/# cd ktexperts/ root@5d477a9118ad:/ktexperts# ls root@5d477a9118ad:/ktexperts# touch kt-file1 root@5d477a9118ad:/ktexperts# ls kt-file1 |
Exit from the container
1 2 |
root@5d477a9118ad:/ktexperts# exit exit |
Share volume with another New Container (second container)
1. Create a New Container and Add to the Volume
We can share volume while creating another container.
To see all the containers
1 2 3 |
[root@ip-172-31-4-99 ec2-user]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5d477a9118ad kt-image-1 "//bin/bash" 3 minutes ago Exited (0) About a minute ago kt-container-1 |
Using below command to create a container and Add to the Volume
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run -it --name kt-container-2 --privileged=true --volumes-from kt-container-1 ubuntu //bin/bash root@44637b7c4948:/# |
2. Verify Volume “ktexperts”
1 2 |
root@44637b7c4948:/# ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var |
3. Verify file “kt-file1” inside the volume
1 2 3 |
root@44637b7c4948:/# cd ktexperts/ root@44637b7c4948:/ktexperts# ls kt-file1 |
4. Create a new file inside the volume
1 2 3 4 5 6 |
root@44637b7c4948:/ktexperts# cat > kt-file2 ktexperts is a knowledge sharing platform root@44637b7c4948:/ktexperts# ls kt-file1 kt-file2 root@44637b7c4948:/ktexperts# cat kt-file2 ktexperts is a knowledge sharing platform |
Exit from the container
1 2 |
root@44637b7c4948:/ktexperts# exit exit |
5. Go inside the container-1 and verify files in the volume
We need to start and attach for going inside the container
Go inside the container
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 44637b7c4948 ubuntu "//bin/bash" 8 minutes ago Exited (0) About a minute ago kt-container-2 5d477a9118ad kt-image-1 "//bin/bash" 17 minutes ago Exited (0) 14 minutes ago kt-container-1 |
Start Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker start kt-container-1 kt-container-1 |
Attach Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker attach kt-container-1 root@5d477a9118ad:/# |
Verify files in the volume
1 2 3 4 5 6 7 |
root@5d477a9118ad:/# ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@5d477a9118ad:/# cd ktexperts/ root@5d477a9118ad:/ktexperts# ls kt-file1 kt-file2 root@5d477a9118ad:/ktexperts# cat kt-file2 ktexperts is a knowledge sharing platform |
Exit from the container
1 2 |
root@5d477a9118ad:/ktexperts# exit exit |
Share volume with another New Container (third container)
1. Create a New Container and Add to the Volume
We can share volume while creating another container.
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker run -it --name kt-container-3 --privileged=true --volumes-from kt-container-2 ubuntu //bin/bash root@99303599ae9a:/# |
2. Verify volume
1 2 |
root@99303599ae9a:/# ls bin boot dev etc home ktexperts 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@99303599ae9a:/# cd ktexperts/ root@99303599ae9a:/ktexperts# ls kt-file1 kt-file2 root@99303599ae9a:/ktexperts# cat kt-file2 ktexperts is a knowledge sharing platform |
4. Create a new directory inside the volume
1 2 3 |
root@99303599ae9a:/ktexperts# mkdir kt1 kt2 root@99303599ae9a:/ktexperts# ls kt-file1 kt-file2 kt1 kt2 |
Exit from the container
1 2 |
root@99303599ae9a:/ktexperts# exit exit |
5. Go inside first and second containers “kt-container-1” , “kt-container-2” and verify files/directories inside the volume
Go inside first container “kt-container-1” and verify files/directories inside the volume
To see all the 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" 9 minutes ago Exited (0) 8 minutes ago kt-container-3 44637b7c4948 ubuntu "//bin/bash" 20 minutes ago Exited (0) 13 minutes ago kt-container-2 5d477a9118ad kt-image-1 "//bin/bash" 29 minutes ago Exited (0) 6 minutes ago kt-container-1 |
Start Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker start kt-container-1 kt-container-1 |
Attach Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker attach kt-container-1 root@5d477a9118ad:/# |
Verify files/directories inside the container
1 2 3 4 5 |
root@5d477a9118ad:/# ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@5d477a9118ad:/# cd ktexperts/ root@5d477a9118ad:/ktexperts# ls kt-file1 kt-file2 kt1 kt2 |
Exit from the container
1 2 |
root@5d477a9118ad:/ktexperts# exit exit |
Go inside second container “kt-container-2” and verify files/directories inside the volume
Start Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker start kt-container-2 kt-container-2 |
Attach Container
1 2 |
[root@ip-172-31-4-99 ec2-user]# docker attach kt-container-2 root@44637b7c4948:/# |
Verify files/directories inside the volume
1 2 3 4 5 |
root@44637b7c4948:/#ls bin boot dev etc home ktexperts lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@44637b7c4948:/# cd ktexperts/ root@44637b7c4948:/ktexperts# ls kt-file1 kt-file2 kt1 kt2 |
Exit from the container
1 2 |
root@44637b7c4948:/ktexperts# 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