docker踩坑之 device or resource busy
问题描述
linux 服务器上运行了一个 docker 容器,在运行 cp 命令时报错
docker cp redis.conf myadmin_redis:/data/redis.conf
将服务器上的文件拷贝到 docker 容器内部时,报以下错误
Error response from daemon: Error processing tar file(exit status 1): unlinkat /data/redis.conf: device or resource busy
说 /data/redis.conf 文件被占用,不能拷贝。
具体原因
网上查了很多,没找到具体原因,后来才想起来,这个文件是容器创建时通过 volume 绑定的,不是一个真正的文件,启动命令如下。
docker run -d --name myadmin_redis -p 6379:6379 -v /root/test_springboot/redis.conf:/data/redis.conf redis redis-server /data/redis.conf
可以看到,容器内的 /data/redis.conf 文件实际上是关联的 /root/test_springboot/redis.conf 文件,所以不能被拷贝修改。这种情况我们直接修改 /root/test_springboot/redis.conf 文件就可以了。
扩展
如果我们在容器运行后,忘了之前的启动命令,有以下两种方式可以帮忙查看。
第一种方式
查看系统操作历史命令
history | grep ''docker run'' | grep redis
适用于 该服务器上平时执行的命令比较少的情况,这样之前的历史命令才不会被冲掉。
第二种方式
查看容器的底层信息,返回一个 json 格式的详细结果,从中就可以看到 volume 绑定等信息。
docker inspect myadmin_redis
结果如下
点击查看详情
[
{
"Id": "d8d84e34168fb4fa59ab4d4a135820d4d8a96c208c971561546fcec6a16a06e5",
"Created": "2022-07-13T12:10:23.532491402Z",
"Path": "docker-entrypoint.sh",
"Args": [
"redis-server",
"/data/redis.conf"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2629113,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-03-16T14:04:01.320295425Z",
"FinishedAt": "2024-03-16T14:01:00.75868032Z"
},
"Image": "sha256:3534610348b5abc4a6f6f7c314b5884a51c251e50f0038cb74d28c08cc7dd2a0",
"ResolvConfPath": "/var/lib/docker/containers/d8d84e34168fb4fa59ab4d4a135820d4d8a96c208c971561546fcec6a16a06e5/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/d8d84e34168fb4fa59ab4d4a135820d4d8a96c208c971561546fcec6a16a06e5/hostname",
"HostsPath": "/var/lib/docker/containers/d8d84e34168fb4fa59ab4d4a135820d4d8a96c208c971561546fcec6a16a06e5/hosts",
"LogPath": "/var/lib/docker/containers/d8d84e34168fb4fa59ab4d4a135820d4d8a96c208c971561546fcec6a16a06e5/d8d84e34168fb4fa59ab4d4a135820d4d8a96c208c971561546fcec6a16a06e5-json.log",
"Name": "/myadmin_redis",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/root/test_springboot/redis.conf:/data/redis.conf"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"6379/tcp": [
{
"HostIp": "",
"HostPort": "6379"
}
]
},
"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/e15371bd5c5d3106f1997d6b340c9498a072e55da938427091d849e2ba674273-init/diff:/var/lib/docker/overlay2/4c29e88a5c7745ebd58ec99dcfd45a505b3737165dd55a71a865f5f3fb8e66b2/diff:/var/lib/docker/overlay2/48d224e8f73e0d3d66ca83d9b7a8cb8ca73515a51a617447c65d8cf7d9378971/diff:/var/lib/docker/overlay2/670ddc27302cdadc0c5db99de262e129d4068763fe9e12cd77aafbdc77cc1fb4/diff:/var/lib/docker/overlay2/9e522ad99ec5f1469fbc78c7cdb742cad47875a937d773db27097e00f04c0773/diff:/var/lib/docker/overlay2/312674f0181ffc96f651757926ee9fd4419498c88d3cfd0181c8528fa25680a4/diff:/var/lib/docker/overlay2/de095bfcbb1e8e76be121f4f5e23f3d46be21fab41ab382bd08de8afbf6296e1/diff",
"MergedDir": "/var/lib/docker/overlay2/e15371bd5c5d3106f1997d6b340c9498a072e55da938427091d849e2ba674273/merged",
"UpperDir": "/var/lib/docker/overlay2/e15371bd5c5d3106f1997d6b340c9498a072e55da938427091d849e2ba674273/diff",
"WorkDir": "/var/lib/docker/overlay2/e15371bd5c5d3106f1997d6b340c9498a072e55da938427091d849e2ba674273/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/root/test_springboot/redis.conf",
"Destination": "/data/redis.conf",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "volume",
"Name": "c3a6e95da330454445e03619faf55cc48f57c79727ad86477504eec365b3f2f6",
"Source": "/var/lib/docker/volumes/c3a6e95da330454445e03619faf55cc48f57c79727ad86477504eec365b3f2f6/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "d8d84e34168f",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"6379/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.14",
"REDIS_VERSION=7.0.3",
"REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-7.0.3.tar.gz",
"REDIS_DOWNLOAD_SHA=2cde7d17214ffe305953da9fff12333e8a72caa57fd4923e4872f6362a208e73"
],
"Cmd": [
"redis-server",
"/data/redis.conf"
],
"Image": "redis",
"Volumes": {
"/data": {}
},
"WorkingDir": "/data",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "fb5b7e1f10a85b9470732b7c9754eaae2e871751438c8f0f84ce62a5406d890e",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"6379/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "6379"
},
{
"HostIp": "::",
"HostPort": "6379"
}
]
},
"SandboxKey": "/var/run/docker/netns/fb5b7e1f10a8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f75c2cfba6bbfb8f456324c66e82318720090019f6e5f6d6b9c59125d374082e",
"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": "7a77e40525a22a4de0a2557bb00ea1b463fb5a1bdfd249463dd313d22f233154",
"EndpointID": "f75c2cfba6bbfb8f456324c66e82318720090019f6e5f6d6b9c59125d374082e",
"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
}
}
}
}
]