-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocker-Volume
221 lines (165 loc) · 9.42 KB
/
Docker-Volume
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
docker volume
########################
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
Volumes have several advantages over bind mounts:
* Volumes are easier to back up or migrate than bind mounts.
* You can manage volumes using Docker CLI commands or the Docker API.
* Volumes work on both Linux and Windows containers.
* Volumes can be more safely shared among multiple containers.
* Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
* New volumes can have their content pre-populated by a container.
* Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.
* In addition, volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container.
Typs of mount:
1. Bind Mount
2. Volume*
3. tempfs
If your container generates non-persistent state data, consider using a tmpfs mount to avoid storing the data anywhere permanently, and to increase the container’s performance by avoiding writing into the container’s writable layer.
Choose the -v or --mount flag
--------------------------------
In general, --mount is more explicit and verbose. The biggest difference is that the -v syntax combines all the options together in one field, while the --mount syntax separates them. Here is a comparison of the syntax for each flag.
If you need to specify volume driver options, you must use --mount.
When using volumes with services, only --mount is supported.
-v or --volume:
Consists of three fields, separated by colon characters (:). The fields must be in the correct order, and the meaning of each field is not immediately obvious.
* In the case of named volumes, the first field is the name of the volume, and is unique on a given host machine. For anonymous volumes, the first field is omitted.
* The second field is the path where the file or directory are mounted in the container.
* The third field is optional, and is a comma-separated list of options, such as ro. These options are discussed below.
--mount:
Consists of multiple key-value pairs, separated by commas and each consisting of a <key>=<value> tuple. The --mount syntax is more verbose than -v or --volume, but the order of the keys is not significant, and the value of the flag is easier to understand.
* The type of the mount, which can be bind, volume, or tmpfs. This topic discusses volumes, so the type is always volume.
* The source of the mount. For named volumes, this is the name of the volume. For anonymous volumes, this field is omitted. May be specified as source or src.
* The destination takes as its value the path where the file or directory is mounted in the container. May be specified as destination, dst, or target.
* The readonly option, if present, causes the bind mount to be mounted into the container as read-only. May be specified as readonly or ro.
* The volume-opt option, which can be specified more than once, takes a key-value pair consisting of the option name and its value.
How to create and mount a volume
--------------------------------
docker volume create vol-01
docker volume ls
docker container run -d --name web -v vol-01:/usr/share/nginx/html:ro -p 80:80 nginx:latest
docker run -d --name web --mount source=vol-01,target=/usr/share/nginx/html,ro -p 80:80 nginx:latest
docker volume rm vol-01
docker volume inspect vol-01
Use a volume with Docker Compose
----------------------------------
Here’s an example of a single Docker Compose service with a volume:
services:
frontend:
image: nginx:latest
volumes:
- vol-01:/usr/share/nginx/html:ro
volumes:
vol-01:
Running docker compose up for the first time creates a volume. The same volume is reused when you subsequently run the command.
You can create a volume directly outside of Compose using docker volume create and then reference it inside docker-compose.yml as follows:
services:
frontend:
image: nginx:latest
volumes:
- vol-01:/usr/share/nginx/html:ro
volumes:
vol-01:
external: true
Start a service with volumes
------------------------------
When you start a service and define a volume, each service container uses its own local volume. None of the containers can share this data if you use the local volume driver. However, some volume drivers do support shared storage.
** The docker service create command doesn’t support the -v or --volume flag. When mounting a volume into a service’s containers, you must use the --mount flag.
The following example starts an nginx service with four replicas, each of which uses a local volume called vol-01.
docker service create -d \
--replicas=4 \
--name devtest-service \
--mount source=vol-01,target=/usr/share/nginx/html,ro \
nginx:latest
Create a volume using a volume driver
---------------------------------------
When you create a volume using docker volume create, or when you start a container which uses a not-yet-created volume, you can specify a volume driver. The following examples use the vieux/sshfs volume driver, first when creating a standalone volume, and then when starting a container which creates a new volume.
Initial setup:
The following example assumes that you have two nodes, the first of which is a Docker host and can connect to the second node using SSH.
On the Docker host, install the vieux/sshfs plugin:
docker plugin install --grant-all-permissions vieux/sshfs
This example specifies an SSH password, but if the two hosts have shared keys configured, you can exclude the password. Each volume driver may have zero or more configurable options, each of which is specified using an -o flag.
docker volume create --driver vieux/sshfs \
-o sshcmd=test@node2:/home/test \
-o password=testpassword \
sshvolume
** If the volume driver requires you to pass any options, you must use the --mount flag to mount the volume, and not -v.
Start a container which creates a volume using a volume driver
----------------------------------------------------------------
The following example specifies an SSH password. However, if the two hosts have shared keys configured, you can exclude the password. Each volume driver may have zero or more configurable options.
docker run -d \
--name sshfs-container \
--volume-driver vieux/sshfs \
--mount src=sshvolume,target=/app,volume-opt=sshcmd=test@node2:/home/test,volume-opt=password=testpassword \
nginx:latest
Create a service which creates an NFS volume
-----------------------------------------------
The following example shows how you can create an NFS volume when creating a service. It uses 10.0.0.10 as the NFS server and /var/docker-nfs as the exported directory on the NFS server. Note that the volume driver specified is local.
NFSv3:
docker service create -d \
--name nfs-service \
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,volume-opt=o=addr=10.0.0.10' \
nginx:latest
NFSv4:
docker service create -d \
--name nfs-service \
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,"volume-opt=o=addr=10.0.0.10,rw,nfsvers=4,async"' \
nginx:latest
Create CIFS/Samba volumes
---------------------------
You can mount a Samba share directly in docker without configuring a mount point on your host.
docker volume create \
--driver local \
--opt type=cifs \
--opt device=//uxxxxx.your-server.de/backup \
--opt o=addr=uxxxxx.your-server.de,username=uxxxxxxx,password=*****,file_mode=0777,dir_mode=0777 \
--name cif-volume
* Notice the addr option is required if using a hostname instead of an IP so docker can perform the hostname lookup.
---------------------------------------
Practice
share volume between two container:
---------------------------------------
first container generate current date and move on the volume and another container show current
dates in volumes
1- script for move current date to file:
vi date.sh
#!/bin/bash
while : #while without any condition means while true
do
date > /result/index.html # name of file must be index.html
sleep 3
done
-------
chmod +x date.sh
2-
docker volume create date
3- dockerfile for fist container for generate date and move to file:
---------------------------------------------------------------------
FROM alpine:latest
RUN mkdir -p /app && mkdir /result && apk add --no-cache --upgrade bash
WORKDIR /app
COPY date.sh .
CMD /bin/sh -c /app/date.sh
------
docker image build . -t date:v1
------
docker container run -d --name datepuller -v date:/result date:v1
--------
docker container logs -f date:v1
---------
4- container for show time from volume
------------------------------------------
docker container run -d --name web -v date:/usr/sahre/nginx/html:ro -p 80:80 nginx:latest
using bind mount
----------------
#mount a mountpoint as a volume
mkdir /data #on host
docker container run -d --name web1 -v /data:/usr/share/nginx/html nginx:latest
using tempfs
---------------
data is not safe but performace of read/write better because used from host memory.
docker container run -d --name web2 --mount type=tmpfs,destination=/usr/share/nginx/html/ nginx:latest
or
docker container run -d --name web2 --tmpfs /usr/share/nginx/html/ nginx:latest
how to prune docker volumes:
-----------------------
docker volume prune