j具名和匿名挂载,
docker run -d -P --name nginx01 -v /etc/nginx nginx
[root@localhost ~]# docker volume ls查看容器卷
DRIVER VOLUME NAME
local 8a0b0f68b5cc36a1ee1f08b9257e87f4bb6afaf27ba50a9590b26f8faf13ff23
local 23638c079fd799dcbe42cc18784039ac5fc5096fbe421f3af892295405531153
local a65de0db28679aec5865dc8c12b4c38706b1973095e30c051a570b5881644748
#这就是匿名挂载,这种就是匿名挂载,-v 只写了容器内的路径,没有写容器外的路径!
[root@localhost ~]# docker run -d -P --name nginx002 -v juming-nginx:/etc/nginx nginx
ac7905937938664debef1ac41d5da5d257c69cde900b1fcca21d6a9e39ccef4b
ERRO[0010] error waiting for container: context canceled
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 8a0b0f68b5cc36a1ee1f08b9257e87f4bb6afaf27ba50a9590b26f8faf13ff23
local 23638c079fd799dcbe42cc18784039ac5fc5096fbe421f3af892295405531153
local a65de0db28679aec5865dc8c12b4c38706b1973095e30c051a570b5881644748
local juming-nginx
#通过 -v 卷名:容器内路径
# 查看一下这个卷
# 所有docker容器内的卷,没有指定目录的情怀下都是在/var/lib/docker/volumes/
[root@localhost ~]# docker volume inspect juming-nginx 查看卷的路径。
[
{
"CreatedAt": "2022-01-04T16:07:45+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
通过具名挂载,可以方便的找到我们的一个卷/var/lib/docker/volumes/xxxxx/_data"
如何确定是匿名挂载,还是匿名挂载,还是指定路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径,#具名挂载
-v /宿主机路径::容器内路径# 指定路径挂载
通过-v r容器内路径:ro rw 改变读写权限
一旦这个设置了容器权限,容器对我们挂载处理的内容就有了限定
docker run -d -P --name nginx002 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx002 -v juming-nginx:/etc/nginx:rw nginx
ro 只能通过宿主机来操作,容器内无法操作。
docker Dockerfile 通过构建docker镜像来构建镜像文件
[root@localhost volumes]# cd /root/
[root@localhost ~]# ls
anaconda-ks.cfg javase8u211-later-archive-downloads.html
docker mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
hava.java --restart=alwys
hellworld.sh shcode
hi.sh
[root@localhost ~]# mkdir docker-test-volume
[root@localhost ~]# ls
anaconda-ks.cfg hellworld.sh --restart=alwys
docker hi.sh shcode
docker-test-volume javase8u211-later-archive-downloads.html
hava.java mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd docker-test-volume/
#通过这个脚本生成镜像,
#创建这个dockerfile 名字可以随机简易dockerfile
#内容的内容指令 (大写) 参数
[root@localhost docker-test-volume]#vim dockerfile1 这是一个匿名挂载
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
[root@localhost docker-test-volume]# docker build -f /root/docker-test-volume/dockerfile1 -t kuangshen/centos .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos #基础镜像
---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"] #挂载
---> Running in 8212d31a8792
Removing intermediate container 8212d31a8792
---> 1d880eac6083
Step 3/4 : CMD echo "----end----" #内部命令
---> Running in 881decd98364
Removing intermediate container 881decd98364
---> 6acfe7404567
Step 4/4 : CMD /bin/bash #生成
---> Running in e3bd5228a158
Removing intermediate container e3bd5228a158
---> 1db844ed918a
Successfully built 1db844ed918a
Successfully tagged kuangshen/centos:latest
[root@localhost docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kuangshen/centos latest 1db844ed918a 2 minutes ago 231MB
tomcat0001 1.0 09ef6b2e826f 25 hours ago 684MB
tomcat0001 latest a22c9a74a6f8 25 hours ago 684MB
tomcat latest 13b1026f5ad2 25 hours ago 684MB
nginx latest 605c77e624dd 5 days ago 141MB
路径
docker run -it 1db844ed918a 后面没有跟/bin/bash 因为那个脚本可以直接进去
注意这里是进入这个镜像所创建的容器,并不知道这个容器id需要在宿主机外面输入
docker inspect 容器id 才可以 找到宿主机创建的同步文件夹,
docker inspect 容器id 才可以 找到宿主机创建的同步文件夹,
"Mounts": [
{
"Type": "volume",
"Name": "16da86bfbad9cdeb8acb94008b8a95e21310070253e9305069f254f8287951c2",
"Source": "/var/lib/docker/volumes/16da86bfbad9cdeb8acb94008b8a95e21310070253e9305069f254f8287951c2/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
volume01 volume02 这个目录是生成镜像时自动挂载的。数据卷目录。
这个卷外部一定有一个同步的目录。
[root@localhost docker-test-volume]# docker run -it 1db844ed918a 运行这个镜像
[root@77b9992258dc /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
[root@77b9992258dc /]# ll
bash: ll: command not found
[root@77b9992258dc /]# ls -l
total 0
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jan 4 09:06 dev
drwxr-xr-x 1 root root 66 Jan 4 09:06 etc
drwxr-xr-x 2 root root 6 Nov 3 2020 home
lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------ 2 root root 6 Sep 15 14:17 lost+found
drwxr-xr-x 2 root root 6 Nov 3 2020 media
drwxr-xr-x 2 root root 6 Nov 3 2020 mnt
drwxr-xr-x 2 root root 6 Nov 3 2020 opt
dr-xr-xr-x 136 root root 0 Jan 4 09:06 proc
dr-xr-x--- 2 root root 162 Sep 15 14:17 root
drwxr-xr-x 11 root root 163 Sep 15 14:17 run
lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Nov 3 2020 srv
dr-xr-xr-x 13 root root 0 Jan 4 09:06 sys
drwxrwxrwt 7 root root 171 Sep 15 14:17 tmp
drwxr-xr-x 12 root root 144 Sep 15 14:17 usr
drwxr-xr-x 20 root root 262 Sep 15 14:17 var
drwxr-xr-x 2 root root 6 Jan 4 09:06 volume01
drwxr-xr-x 2 root root 6 Jan 4 09:06 volume02
[root@77b9992258dc /]# cd /volume01 挂载的的文件 (与宿主机是同步文件)
[root@77b9992258dc volume01]# ls
[root@77b9992258dc volume01]# touch container.txt
[root@77b9992258dc volume01]# docker inspect 1db844ed918a 查看信息,不对需要到宿主机操作
bash: docker: command not found
[root@77b9992258dc volume01]# exit
exit
这里应该查看容器的配置信息。操作错误。
[root@localhost docker-test-volume]# docker inspect 1db844ed918a 这这里查看的是这个镜像的id
[
{
"Id": "sha256:1db844ed918ac3b36bb2acf4d81a057ca8055bb16e2f1f1096fb3c2714785932",
"RepoTags": [
"kuangshen/centos:latest"
],
"RepoDigests": [],
"Parent": "sha256:6acfe7404567644d755094fc630707a7a9f336438484e1917a5f48725e61fdb4",
"Comment": "",
"Created": "2022-01-04T08:46:41.000687602Z",
"Container": "e3bd5228a15817e8875b6dd23fcffcc5f810c388488696be364926caef9cd633",
"ContainerConfig": {
"Hostname": "e3bd5228a158",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/sh\" \"-c\" \"/bin/bash\"]"
],
"Image": "sha256:6acfe7404567644d755094fc630707a7a9f336438484e1917a5f48725e61fdb4",
"Volumes": {
"volume01": {},
"volume02": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "20.10.12",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"/bin/bash"
],
"Image": "sha256:6acfe7404567644d755094fc630707a7a9f336438484e1917a5f48725e61fdb4",
"Volumes": {
"volume01": {},
"volume02": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 231268856,
"VirtualSize": 231268856,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/496c93719225d03e3b5a66fc3e20aeba2978bf70e2991c5e3a25ba685db9de63/merged",
"UpperDir": "/var/lib/docker/overlay2/496c93719225d03e3b5a66fc3e20aeba2978bf70e2991c5e3a25ba685db9de63/diff",
"WorkDir": "/var/lib/docker/overlay2/496c93719225d03e3b5a66fc3e20aeba2978bf70e2991c5e3a25ba685db9de63/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
]
},
"Metadata": {
"LastTagTime": "2022-01-04T16:46:41.743170896+08:00"
}
}
]
#开启另一个端口输入命令
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95eef4e92cf0 1db844ed918a "/bin/sh -c /bin/bash" 44 seconds ago Up 37 seconds vigorous_bose
ac7905937938 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49154->80/tcp, :::49154->80/tcp nginx002
7628b0d1a4ed nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:49153->80/tcp, :::49153->80/tcp nginx001
30662e30fb0a portainer/portainer "/portainer" 26 hours ago Up 2 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp suspicious_tharp
[root@localhost ~]# docker inspect 95eef4e92cf0
[
{
"Id": "95eef4e92cf0a9a1c1602fa6f76047dba3b6986656699a71749a55af9ddffef0",
"Created": "2022-01-04T09:28:06.035631644Z",
"Path": "/bin/sh",
"Args": [
"-c",
"/bin/bash"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3172,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-04T09:28:12.476042091Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:1db844ed918ac3b36bb2acf4d81a057ca8055bb16e2f1f1096fb3c2714785932",
"ResolvConfPath": "/var/lib/docker/containers/95eef4e92cf0a9a1c1602fa6f76047dba3b6986656699a71749a55af9ddffef0/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/95eef4e92cf0a9a1c1602fa6f76047dba3b6986656699a71749a55af9ddffef0/hostname",
"HostsPath": "/var/lib/docker/containers/95eef4e92cf0a9a1c1602fa6f76047dba3b6986656699a71749a55af9ddffef0/hosts",
"LogPath": "/var/lib/docker/containers/95eef4e92cf0a9a1c1602fa6f76047dba3b6986656699a71749a55af9ddffef0/95eef4e92cf0a9a1c1602fa6f76047dba3b6986656699a71749a55af9ddffef0-json.log",
"Name": "/vigorous_bose",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"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/85970d007b92290e5c2188f3d3eea7aff8d30ef97e2f140e2d195f12d0be0357-init/diff:/var/lib/docker/overlay2/496c93719225d03e3b5a66fc3e20aeba2978bf70e2991c5e3a25ba685db9de63/diff",
"MergedDir": "/var/lib/docker/overlay2/85970d007b92290e5c2188f3d3eea7aff8d30ef97e2f140e2d195f12d0be0357/merged",
"UpperDir": "/var/lib/docker/overlay2/85970d007b92290e5c2188f3d3eea7aff8d30ef97e2f140e2d195f12d0be0357/diff",
"WorkDir": "/var/lib/docker/overlay2/85970d007b92290e5c2188f3d3eea7aff8d30ef97e2f140e2d195f12d0be0357/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "16da86bfbad9cdeb8acb94008b8a95e21310070253e9305069f254f8287951c2",
"Source": "/var/lib/docker/volumes/16da86bfbad9cdeb8acb94008b8a95e21310070253e9305069f254f8287951c2/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "36deb8f3bf9108300c31c6970a66cc74dd8f301337d288a0f452beafa260e5bd",
"Source": "/var/lib/docker/volumes/36deb8f3bf9108300c31c6970a66cc74dd8f301337d288a0f452beafa260e5bd/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "95eef4e92cf0",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"/bin/bash"
],
"Image": "1db844ed918a",
"Volumes": {
"volume01": {},
"volume02": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "60b60639a0ba1949d1b683e33c7cad55c7795d214f64c3cd0dc07d8792d0f020",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/60b60639a0ba",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f80f42780617bc58fee12395e0002a08b8ae2aa022bfc2ec37c4078b20e69eda",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:05",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "0e048dc30f7b6215097552c630805f9ac43885e5594b2cd101e768937ec5e32f",
"EndpointID": "f80f42780617bc58fee12395e0002a08b8ae2aa022bfc2ec37c4078b20e69eda",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:05",
"DriverOpts": null
}
}
}
}
]
数据卷容器
两个mysql 同步数据。
子容器,父容器,centos 01挂载到centos 02shang ,就实现了数据同步,父容器上的数据卷容器,;两个或者多个容器直接实现数据共享。
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker images 查看镜像。
REPOSITORY TAG IMAGE ID CREATED SIZE
kuangshen/centos latest 1db844ed918a 46 hours ago 231MB
tomcat0001 1.0 09ef6b2e826f 2 days ago 684MB
tomcat0001 latest a22c9a74a6f8 2 days ago 684MB
tomcat latest 13b1026f5ad2 2 days ago 684MB
nginx latest 605c77e624dd 7 days ago 141MB
tomcat 9.0 b8e65a4d736d 2 weeks ago 680MB
tomcat <none> fb5657adc892 2 weeks ago 680MB
redis latest 7614ae9453d1 2 weeks ago 113MB
mysql 5.7 c20987f18b13 2 weeks ago 448MB
centos latest 5d0da3dc9764 3 months ago 231MB
portainer/portainer latest 580c0e4e98b0 9 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 21 months ago 791MB
[root@localhost ~]# docker run -t --name docker01 kuangshen/centos:1.0
[root@localhost ~]# docker run -it --name docker01 1db844ed918a 启动。
[root@851376de1df7 /]# ls -l
total 0
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jan 6 06:51 dev
drwxr-xr-x 1 root root 66 Jan 6 06:51 etc
drwxr-xr-x 2 root root 6 Nov 3 2020 home
lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------ 2 root root 6 Sep 15 14:17 lost+found
drwxr-xr-x 2 root root 6 Nov 3 2020 media
drwxr-xr-x 2 root root 6 Nov 3 2020 mnt
drwxr-xr-x 2 root root 6 Nov 3 2020 opt
dr-xr-xr-x 122 root root 0 Jan 6 06:51 proc
dr-xr-x--- 2 root root 162 Sep 15 14:17 root
drwxr-xr-x 11 root root 163 Sep 15 14:17 run
lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Nov 3 2020 srv
dr-xr-xr-x 13 root root 0 Jan 6 06:51 sys
drwxrwxrwt 7 root root 171 Sep 15 14:17 tmp
drwxr-xr-x 12 root root 144 Sep 15 14:17 usr
drwxr-xr-x 20 root root 262 Sep 15 14:17 var
drwxr-xr-x 2 root root 6 Jan 6 06:51 volume01
drwxr-xr-x 2 root root 6 Jan 6 06:51 volume02
--volumes-from 通过这个实现容器间的共享
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
851376de1df7 1db844ed918a "/bin/sh -c /bin/bash" 5 minutes ago Up 5 minutes docker01
30662e30fb0a portainer/portainer "/portainer" 3 days ago Up 8 minutes 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp suspicious_tharp
[root@localhost ~]# docker run -it --name docker02 --volumes-from docker01 1db844ed918a
[root@624e9220592a /]# ls -l
total 0
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jan 6 06:58 dev
drwxr-xr-x 1 root root 66 Jan 6 06:58 etc
drwxr-xr-x 2 root root 6 Nov 3 2020 home
lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------ 2 root root 6 Sep 15 14:17 lost+found
drwxr-xr-x 2 root root 6 Nov 3 2020 media
drwxr-xr-x 2 root root 6 Nov 3 2020 mnt
drwxr-xr-x 2 root root 6 Nov 3 2020 opt
dr-xr-xr-x 129 root root 0 Jan 6 06:58 proc
dr-xr-x--- 2 root root 162 Sep 15 14:17 root
drwxr-xr-x 11 root root 163 Sep 15 14:17 run
lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Nov 3 2020 srv
dr-xr-xr-x 13 root root 0 Jan 6 06:51 sys
drwxrwxrwt 7 root root 171 Sep 15 14:17 tmp
drwxr-xr-x 12 root root 144 Sep 15 14:17 usr
drwxr-xr-x 20 root root 262 Sep 15 14:17 var
drwxr-xr-x 2 root root 6 Jan 6 06:51 volume01
drwxr-xr-x 2 root root 6 Jan 6 06:51 volume02
打开另个终端输入命令。
[root@localhost ~]# docker attach 851376de1df7 进入docker01这个容器里
[root@851376de1df7 /]# cd volume01 进入数据同步的文件夹
[root@851376de1df7 volume01]# ls
[root@851376de1df7 volume01]# touch docker01 创建这个文件
[root@851376de1df7 volume01]# ls
docker01
[root@851376de1df7 volume01]# ls 查看生成这个文件了
docker01
[root@851376de1df7 volume01]# cd /volume02
[root@851376de1df7 volume02]# ls
[root@851376de1df7 volume02]# exit
exit
[root@localhost ~]# docker run -it --name docker02 --volumes-from docker01 1db844ed918a
docker: Error response from daemon: Conflict. The container name "/docker02" is already in use by container "624e9220592a2918436117f9a5ba6f6ca1cb175dd84c83d84ce6d631d41395d4". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'. 前面把这个容器关闭了,那就在生成一个查看
[root@localhost ~]# docker run -it --name docker03 --volumes-from docker01 1db844ed918a (这里也可以是kuangshen/centos:版本号)
[root@fb88d210ac79 /]# ls
bin etc lib lost+found mnt proc run srv tmp var volume02
dev home lib64 media opt root sbin sys usr volume01
[root@fb88d210ac79 /]# cd volume01
[root@fb88d210ac79 volume01]# ls
docker01 同步是有这个得
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fb88d210ac79 1db844ed918a "/bin/sh -c /bin/bash" 13 minutes ago Up 13 minutes docker03
851376de1df7 1db844ed918a "/bin/sh -c /bin/bash" 28 minutes ago Exited (0) 13 minutes ago docker01
[root@localhost ~]# docker rm -f 851376de1df7
#可以删除docker01 查看docker03还是可以访问到文件。
这是一个双向拷贝的机制。
多个mysql实现数据共享,
#docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d
-v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456
--name mysql01 mysql:5.7
可以写成下面的。
#docker run -d -p 3310:3306 -v /etc/mysql/conf.d
-v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456
--name mysql01 mysql:5.7
#docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456
--name mysql02 --volumes-form mysql01 mysql:5.7
#这个时候可以实现容器同步。 使用--volumes-form实现的
结论,容器直接配置信息的传递,数据卷的生命周期,一直持续到没有容器使用为止。
但是一旦持久到了本地,本地的数据就不会删除。