Wednesday, 17 July 2019

Docker Command Part-15 By Raj Gupta

How to bind any folder from local system to any container to access it. This will just create a link to folder in place of copying all the data, So in this away we can avoid copy same data in multiple place and same the memory

[root@ip-172-31-93-105 ~]# mkdir bind
[root@ip-172-31-93-105 ~]# ld
ld: no input files
[root@ip-172-31-93-105 ~]# ls
bind
[root@ip-172-31-93-105 ~]# cd bind/
[root@ip-172-31-93-105 bind]# vi index.html
[root@ip-172-31-93-105 bind]# cat index.html
<html>
<head>
       <title>test</title>
</head>
<body>
      <h1 align="center">Docker BindMount Point</h1>
</body>
</html>

[root@ip-172-31-93-105 bind]# pwd
/root/bind

[root@ip-172-31-93-105 bind]# docker container run -it -v /root/bind:/tmp/test/ ubuntu:14.04 bash
root@212c5cb66eb3:/#
[root@ip-172-31-93-105 bind]# pwd
/root/bind
[root@ip-172-31-93-105 bind]# docker container run -rm -it -v
unknown shorthand flag: 'r' in -rm
See 'docker container run --help'.
[root@ip-172-31-93-105 bind]# docker container run -it -v /root/bind:/tmp/test/ ubuntu:14.04 bash
Unable to find image 'ubuntu:14.04' locally
14.04: Pulling from library/ubuntu
a7344f52cb74: Pull complete
515c9bb51536: Pull complete
e1eabe0537eb: Pull complete
4701f1215c13: Pull complete
Digest: sha256:2f7c79927b346e436cc14c92bd4e5bd778c3bd7037f35bc639ac1589a7acfa90
Status: Downloaded newer image for ubuntu:14.04
root@212c5cb66eb3:/# cd /tmp/
root@212c5cb66eb3:/tmp# ls
test
root@212c5cb66eb3:/tmp# cd test/
root@212c5cb66eb3:/tmp/test# ls
index.html
root@212c5cb66eb3:/tmp/test# cat index.html
<html>
<head>
       <title>test</title>
</head>
<body>
      <h1 align="center">Docker BindMount Point</h1>
</body>
</html>
root@212c5cb66eb3:/tmp/test#

if you change any thing in local system same will reflect in container also

Note:-

In place of this command

docker container run -it -v /root/bind:/tmp/test/ ubuntu:14.04 bash

You can also use

docker container run -it --mount type=bind,source= /root/bind,target=/tmp/test/ ubuntu:14.04 bash

No comments:

Post a Comment