有两种方式:
- 数据卷;
- 挂在主机目录。
1 数据卷
1.1 什么是数据卷?
- 数据卷是一个可供一个或多个容器使用的特殊目录;
- 类似于
Linux
下对目录或文件进行mount
,镜像中的被指定为挂载点的目录中的
文件会复制到数据卷中(仅数据卷为空时会复制)。
1.2 数据卷的特性
- 数据卷可以在容器之间共享和重用;
- 数据卷的修改立马生效;
- 数据卷的更新不会影响镜像;
- 数据卷默认一直存在,即使容器被删除。
1.3 创建数据卷
- 命令:
docker volume create xxx
; - 比如:
docker volume create my-tools
- 查看数据卷,使用
docker volume ls
:1.4 查看数据卷
docker volume ls
是查看所有的数据卷;- 而
docker volume inspect xxx
是查看指定数据卷的信息; - 比如:
docker volume inspect my-tools
[
{
"CreatedAt": "2023-05-24T09:30:01+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/my-tools/_data",
"Name": "my-tools",
"Options": null,
"Scope": "local"
}
]
1.5 启动挂在数据卷的容器
- 方法:
在用 docker run 命令的时候,使用 --mount 标记来将 数据卷 挂载到容器里;
可挂挂载多个数据卷。
- 比如:面创建一个名为
tools
的容器,并加载一个 数据卷 到容器的 /usr/share/nginx/tools
目录:
docker run -d -P --name tools --mount source==my-tools,target=/usr/share/nginx/tools nginx:alpinedocker run -d -P --name tools --mount source=my-tools,target=/usr/share/nginx/tools nginx:alpine
- 查看下数据卷的信息:
docker volume inspect tools
。
noamanelson@noamanelson-Virtual-Machine:~$ docker inspect tools
[
{
"Id": "85973675bcfd0dd7ca0bb26257eab558f525009614c13c0eada0dcc71714b262",
"Created": "2023-05-24T01:56:10.651635407Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 39953,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-05-24T01:56:12.455050956Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:8e75cbc5b25c8438fcfe2e7c12c98409d5f161cbb668d6c444e02796691ada70",
"ResolvConfPath": "/var/lib/docker/containers/85973675bcfd0dd7ca0bb26257eab558f525009614c13c0eada0dcc71714b262/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/85973675bcfd0dd7ca0bb26257eab558f525009614c13c0eada0dcc71714b262/hostname",
"HostsPath": "/var/lib/docker/containers/85973675bcfd0dd7ca0bb26257eab558f525009614c13c0eada0dcc71714b262/hosts",
"LogPath": "/var/lib/docker/containers/85973675bcfd0dd7ca0bb26257eab558f525009614c13c0eada0dcc71714b262/85973675bcfd0dd7ca0bb26257eab558f525009614c13c0eada0dcc71714b262-json.log",
"Name": "/tools",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {
}
},
"NetworkMode": "default",
"PortBindings": {
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
48,
173
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": null,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"Mounts": [
{
"Type": "volume",
"Source": "my-tools",
"Target": "/usr/share/nginx/tools"
}
],
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/b852db8a48c8ee2f36af5920c0677d525d90c6403dd02396796dad5927bd2417-init/diff:/var/lib/docker/overlay2/d18166feb9ccbe24169b09f8f4ced2529d0ab7ea3a79cc272c2c3c650903b58c/diff:/var/lib/docker/overlay2/e66e7bdb223fde6e95e5edb32f0f6dab3f2980962e533d1d41844fa915816f9a/diff:/var/lib/docker/overlay2/c3dd0ee7cf2c7a332ebe79d2a6e4443bdb6d2969c41b05cfca59c75ca12998e1/diff:/var/lib/docker/overlay2/08ed0137e18a97effe4374d2ac5fbc8abdfee53044711aae79f732df95e644d8/diff:/var/lib/docker/overlay2/349dbe6ba59c85fa16c3cb587a640b7b67b1e76f3675729ebc36f03303f3c3db/diff:/var/lib/docker/overlay2/fe1cb86615fa0c45b23a9929476b83b10e062e16935454deb56523ed4bf33b82/diff:/var/lib/docker/overlay2/0f3cb2f0fc871c0c25eb31a9f20685598770786dd62865db1f3a41cec9995f82/diff",
"MergedDir": "/var/lib/docker/overlay2/b852db8a48c8ee2f36af5920c0677d525d90c6403dd02396796dad5927bd2417/merged",
"UpperDir": "/var/lib/docker/overlay2/b852db8a48c8ee2f36af5920c0677d525d90c6403dd02396796dad5927bd2417/diff",
"WorkDir": "/var/lib/docker/overlay2/b852db8a48c8ee2f36af5920c0677d525d90c6403dd02396796dad5927bd2417/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "my-tools",
"Source": "/var/lib/docker/volumes/my-tools/_data",
"Destination": "/usr/share/nginx/tools",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "85973675bcfd",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {
}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.23.4",
"PKG_RELEASE=1",
"NJS_VERSION=0.7.11"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx:alpine",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "9b202e7041b396a70bf5256d93119f524e0b2df574ce6da5905a3873746583d0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
},
{
"HostIp": "::",
"HostPort": "32768"
}
]
},
"SandboxKey": "/var/run/docker/netns/9b202e7041b3",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "50abc9764730e724b0ac93a62eeb7a46fb7b8359234d2771417a083f74b5a0ed",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4d733b11c6b91921b907d8e660c4b4686a2b3aa9f6899221d8aafe46a5c5bdad",
"EndpointID": "50abc9764730e724b0ac93a62eeb7a46fb7b8359234d2771417a083f74b5a0ed",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
1.6 删除数据卷
- 命令:
docker volume rm xxx
; - 比如:
docker volume rm my-tools
;
注意:
Docker 不会在容器被删除后自动删除;
在删除容器的时候使用 docker rm -v,同时移除数据卷;
无主的数据卷可能会占据很多空间,要清理请使用以下命令 docker volume prune
2 挂在主机目录
2.1 挂载主机目录作为数据卷
使用 --mount 标记可以指定挂载一个本地主机的目录到容器中去。
docker run -d -P --name tools --mount type=bind,source=/src/webapp,target=/usr/share/nginx/tools nginx:alpine
Docker
挂载主机目录的默认权限是 读写 ,也可以通过增加readonly
指定为 只读:
docker run -d -P --name tools --mount type=bind,source=/src/webapp,target=/usr/share/nginx/tools,readonly nginx:alpine
2.2 查看数据卷信息
docker inspect tools
2.3 挂载本地主机文件作为数据卷
docker run --rm -it --mount type=bind,source=$HOME/.bash_history,target=/root/.bash_history ubuntu:18.04 bash