前言:
处理的情况是在于当docker容器已经再启动了, 但是由于之前可能出于种种情况导致有一些文件目录少挂载了, 所以在后续需要进行追加
⚠这里需要注意的是如果是数据相关或者一些比较重要的配置文件的话一定要先从容器中赋值出来一份进行保存, 因为追加卷挂载是需要关闭所有容器, 且关闭docker服务的, 所以如果情况允许的话, 可能对容器的数据和重要文件进行备份之后, 删除容器重新启动一个挂在卷的容器是更好的
这里我们随便使用一个mysql8.0镜像前期不对于data数据进行挂载
如下,这里是没有做卷挂载的
root@DESKTOP-J4DF3UJ:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e6b7beeaad4a mysql:8.0 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp, 0.0.0.0:3312->1106/tcp, :::3312->1106/tcp mysql_test
首先通过命令获取容器id(CONTAINER ID)
root@DESKTOP-J4DF3UJ:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
499d5896d9e4 mysql:8.0 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 33060/tcp, 0.0.0.0:3312->3306/tcp, :::3312->3306/tcp mysql_test
root@DESKTOP-J4DF3UJ:~# docker inspect 499 [ { "Id": "499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639", "Created": "2022-10-12T03:11:27.2356012Z", "Path": "docker-entrypoint.sh", "Args": [ "mysqld" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 9738, "ExitCode": 0, "Error": "", "StartedAt": "2022-10-12T03:11:27.6969975Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:2b6e086aed92765968a96e83064ebabc7c55ec4a7ed7fd60c1f2acf3b09253f1", "ResolvConfPath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/resolv.conf", "HostnamePath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/hostname", "HostsPath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/hosts", "LogPath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639-json.log", "Name": "/mysql_test", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "3306/tcp": [ { "HostIp": "", "HostPort": "3312" } ] }, "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/1bb344cebc5950256efdeef661e5f38cdbae0995148019bfa4d13a8cf481732e-init/diff:/var/lib/docker/overlay2/ce5dc3551d1b554e199c167fd69de713375555c22ed25ec4bdaf25b81f195ca0/diff:/var/lib/docker/overlay2/2670d2b701125af27673f05e5302a8cafdea7da721bf9e257fe3139b22e1b7a1/diff:/var/lib/docker/overlay2/fff6830df6c60d551638c17e2fa3501417674a88292d6d394f97a1cdcb065fee/diff:/var/lib/docker/overlay2/81161f207e545d2c8b3a499392739a542ac95a06cb018bc6c2e4e75c4aa5ddea/diff:/var/lib/docker/overlay2/3dc7ec7a7f37c0f93958796353ec78502745bf7e9940fad0aa30d661cc8278fc/diff:/var/lib/docker/overlay2/0819dd3db0aa94ca6fdf644d5d6533e2016ed95ce1cdab9374fb7c8fa54b7b83/diff:/var/lib/docker/overlay2/499f0a733ece4abb0db5f1ffd2241443db8de7fe7f2f3175041e062a4c3ee031/diff:/var/lib/docker/overlay2/312c38b89e8532b1843d0e509fd269f7f7ee3e5e7e4ef1dfee7de03d6bc25c11/diff:/var/lib/docker/overlay2/3b1321cc8d4a178f654aab62e43f5e5ffb94919751098c667423b9b1d9c804a7/diff:/var/lib/docker/overlay2/dcd2d27d1deae46f3c1695f36b3be9a60773d622cd5bc9a1615697d1bc399b59/diff:/var/lib/docker/overlay2/456057a2ef56e739bf53a3a462ab78dae4a2ff41bacd0432823209cc8cfcef6b/diff", "MergedDir": "/var/lib/docker/overlay2/1bb344cebc5950256efdeef661e5f38cdbae0995148019bfa4d13a8cf481732e/merged", "UpperDir": "/var/lib/docker/overlay2/1bb344cebc5950256efdeef661e5f38cdbae0995148019bfa4d13a8cf481732e/diff", "WorkDir": "/var/lib/docker/overlay2/1bb344cebc5950256efdeef661e5f38cdbae0995148019bfa4d13a8cf481732e/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "aa21df176a5e6b17cbe4ab285d294ebba9b500e1d118eccd3c79d07e7fda9944", "Source": "/var/lib/docker/volumes/aa21df176a5e6b17cbe4ab285d294ebba9b500e1d118eccd3c79d07e7fda9944/_data", "Destination": "/var/lib/mysql", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "499d5896d9e4", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "ExposedPorts": { "3306/tcp": {}, "33060/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "MYSQL_ROOT_PASSWORD=123456", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.14", "MYSQL_MAJOR=8.0", "MYSQL_VERSION=8.0.31-1.el8", "MYSQL_SHELL_VERSION=8.0.31-1.el8" ], "Cmd": [ "mysqld" ], "Image": "mysql:8.0", "Volumes": { "/var/lib/mysql": {} }, "WorkingDir": "", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "1a595be74078e6513df497fbbd31c59989ca5f094ea281c29a9403cf68784033", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "3306/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "3312" }, { "HostIp": "::", "HostPort": "3312" } ], "33060/tcp": null }, "SandboxKey": "/var/run/docker/netns/1a595be74078", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "5341b6682e1b1782594b8c9e93961fcdc67d062a804b815e5a51f7ca363b22b4", "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": "8423e80431da93594db036ed985d54075462bf75db8110ad612a194ee99d71ee", "EndpointID": "5341b6682e1b1782594b8c9e93961fcdc67d062a804b815e5a51f7ca363b22b4", "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 } } } } ]
接下来停止容器并关闭docker服务, 这步是一定要进行的否则无法生效, ⚠必须关闭docker服务不然你就算修改了json文件也是会被覆盖的, 可自看情况选择,这里先停止了所有容器且关闭了docker服务
service docker stop
找到配置文件路径是
/var/lib/docker/containers/<容器ID>/config.v2.json
/var/lib/docker/containers/<容器ID>/hostconfig.json
找到对应的两个文件修改前记得注意备份, 且最好拉出来在ide中修改不然vim真的是锻炼眼神
root@DESKTOP-J4DF3UJ:/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639# ll
total 48
drwx--x--- 4 root root 4096 Oct 12 11:13 ./
drwx--x--- 3 root root 4096 Oct 12 11:11 ../
-rw-r----- 1 root root 7914 Oct 12 11:11 499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639-json.log
drwx------ 2 root root 4096 Oct 12 11:11 checkpoints/
-rw------- 1 root root 3179 Oct 12 11:13 config.v2.json
-rw-r--r-- 1 root root 1516 Oct 12 11:13 hostconfig.json
-rw-r--r-- 1 root root 13 Oct 12 11:11 hostname
-rw-r--r-- 1 root root 174 Oct 12 11:11 hosts
drwx--x--- 2 root root 4096 Oct 12 11:11 mounts/
-rw-r--r-- 1 root root 198 Oct 12 11:11 resolv.conf
-rw-r--r-- 1 root root 71 Oct 12 11:11 resolv.conf.hash
首先对于config.v2.json文件进行修改, 我这里全部复制了具体需要看一下下方的注释修改的是MountPoints
下参数
# 首先是模板
# 新建挂载卷->key是容器内路径名
"/home": {
# 本地资源路径
"Source": "<绝对路径>",
# 容器内资源路径
"Destination": "<绝对路径>",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
# 本地资源路径
"Source": "/home",
# 容器资源路径
"Target": "/home"
},
"SkipMountpointCreation": false
}
接下来是我修改的参数这里是挂在了本地的/home到容器内的/home文件夹
{
"StreamConfig": {},
"State": {
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"RemovalInProgress": false,
"Dead": false,
"Pid": 9738,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-10-12T03:11:27.6969975Z",
"FinishedAt": "0001-01-01T00:00:00Z",
"Health": null
},
"ID": "499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639",
"Created": "2022-10-12T03:11:27.2356012Z",
"Managed": false,
"Path": "docker-entrypoint.sh",
"Args": [
"mysqld"
],
"Config": {
"Hostname": "499d5896d9e4",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.14",
"MYSQL_MAJOR=8.0",
"MYSQL_VERSION=8.0.31-1.el8",
"MYSQL_SHELL_VERSION=8.0.31-1.el8"
],
"Cmd": [
"mysqld"
],
"Image": "mysql:8.0",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"Image": "sha256:2b6e086aed92765968a96e83064ebabc7c55ec4a7ed7fd60c1f2acf3b09253f1",
"NetworkSettings": {
"Bridge": "",
"SandboxID": "1a595be74078e6513df497fbbd31c59989ca5f094ea281c29a9403cf68784033",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8423e80431da93594db036ed985d54075462bf75db8110ad612a194ee99d71ee",
"EndpointID": "5341b6682e1b1782594b8c9e93961fcdc67d062a804b815e5a51f7ca363b22b4",
"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,
"IPAMOperational": false
}
},
"Service": null,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3312"
},
{
"HostIp": "::",
"HostPort": "3312"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/1a595be74078",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"IsAnonymousEndpoint": false,
"HasSwarmEndpoint": false
},
"LogPath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639-json.log",
"Name": "/mysql_test",
"Driver": "overlay2",
"OS": "linux",
"MountLabel": "",
"ProcessLabel": "",
"RestartCount": 0,
"HasBeenStartedBefore": true,
"HasBeenManuallyStopped": true,
"MountPoints": {
"/var/lib/mysql": {
"Source": "",
"Destination": "/var/lib/mysql",
"RW": true,
"Name": "aa21df176a5e6b17cbe4ab285d294ebba9b500e1d118eccd3c79d07e7fda9944",
"Driver": "local",
"Type": "volume",
"ID": "5e7c6720db2925612fda59ef7f905f92a3fe487c85e714711df04dc6e2b75225",
"Spec": {},
"SkipMountpointCreation": false
},
"/home": {
"Source": "/home",
"Destination": "/home",
"RW": true,
"Name": "",
"Driver": "",
"Type": "bind",
"Propagation": "rprivate",
"Spec": {
"Type": "bind",
"Source": "/home",
"Target": "/home"
},
"SkipMountpointCreation": false
}
},
"SecretReferences": null,
"ConfigReferences": null,
"AppArmorProfile": "",
"HostnamePath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/hostname",
"HostsPath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/hosts",
"ShmPath": "",
"ResolvConfPath": "/var/lib/docker/containers/499d5896d9e435a281650e9830086a655cf80668fb7369c2369fc46e73b5c639/resolv.conf",
"SeccompProfile": "",
"NoNewPrivileges": false,
"LocalLogCacheMeta": {
"HaveNotifyEnabled": false
}
}
首先对于hostconfig.json文件进行修改, 我这里全部复制了具体需要看一下下方的注释修改的是Binds
下参数填写挂载的卷信息比如我挂在的就是/home:/home
那么我修改文件就要变成如下, 这里需要注意Bind是一个数组
{
"Binds": [
"/home:/home"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "3312"
}
]
},
"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"
]
}
重启docker服务
service docker start
首先查看我们本机服务器/home文件夹下面有什么文件
root@DESKTOP-J4DF3UJ:/home# ls
1.txt ubuntu
接下来查看容器/home内是否成功挂在
root@DESKTOP-J4DF3UJ:# docker exec -it mysql_test /bin/bash
bash-4.4# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
bash-4.4# cd home
bash-4.4# ls
1.txt ubuntu
Over