-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdocker-volumes1
More file actions
170 lines (85 loc) · 3.27 KB
/
docker-volumes1
File metadata and controls
170 lines (85 loc) · 3.27 KB
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
in simple terms volume can be referred as directory
inside the container
we can access the volume after stopping the container as well
volume will be created in one container and can be accessed
by other containers
you can define the volume to a container while you're
creating the container
a volume can be shared across any number of containers
when you create a image out of a running container, and when container is created out of it, the volume is avialble in newly created container
Benefits of volumes
---------------------
Decoupling container from storage
Share volumes among different containers
Attach volumes to containers
on deleting container volume doesnt get deleted
FROM ubuntu
VOLUME ["/myvolume"]
docker build -t imagename .
create container out of the image
docker run -it --name container2 --privileged=true --volumes-from container1 ubuntu /bin/bash
now after creating container2(new)
myvolume1 is visible whatever you
can do in volume
touch /myvolume1/samplefile
docker start container1
docker attach container1
ls /myvolume1
-------------------------------
while creating a container itself if you
want to attach the volume
docker run -it --name cont-name
-v /volume ubuntu /bin/bash
we are telling in the root directory
create a directory as volume2
docker run -it --name cont4
--priveledged=true --volumes-from cont-name
ubuntu /bin/bash
Docker volumes
------------------------
anything data that is put in volume, can be shared to other container
and by sharing the volume
The data is no longer available when the the container
dies, using docker volumes we can persist data even after contianer
dies
with the volumes we can persist the data
even after the container dies
when the container dies, the volumes are untouched
* Normal Volumes
* Bind Volumes
* Anonymous volumes
all data created in temp layer is deleted
why docker volumes are so important
-------------------------------------
docker run -d --name mysql -e "MYSQL_ROOT_PASSWORD=12345678"
mysql:8
docker ps
docker exec -it container-id bash
mysql -u root -p12345668
show databases;
create database prod_db;
show databases;
save/ persist data
docker volumes ---->
------------------------------------------------------------------------------
--> a running container conent can be modified.
-->as storage of docker is ephermal by default, its lost
after restart
--> Any modifications are done to a running container are
written to read/writable filesystem layer by default that is
removed when the container is stopped.
R/W FILE system LAYER should not be used for persistent storage, as it doest perform well
-->Docker keeps ephermal storage for sometime after container is stopped
making it easier to troubleshoot based on information logs
persistent storage can be offered by using a host directory
the container will bind mount to this host directory to write and access
persistent data
----------------------------------
mkdir -p /var/local/mysql
chown -R 27:27 /var/local/mysql
docker pull mariadb
docker run --name mariadb:latest -d -v /var/local/mysql:/var/lib/mysql
-e MYSQL_USER=user -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=addresses -e MYSQL_ROOT_PASSWORD=password mariadb
ls -l /var/local/mysql
docker exec -it mariadb /bin/bash
ls -l /var/lib/mysql