Docker容器数据恢复
- 1 创建mongo数据库时未挂载数据到宿主机
- 2 查找数据卷位置
- 3 将容器在宿主机上的数据复制到指定目录下
- 4 修改docker-compose并挂载数据(注意端口)
- 5 重新运行新容器
以mongodb8.0.3为例。
1 创建mongo数据库时未挂载数据到宿主机
version: '3.1'
services:
mongo_test:
container_name: mongo_test
restart: always
image: mongo:8.0.3
ports:
- 17017:27017
volumes:
- /etc/localtime:/etc/localtime:ro # 将外边时间直接挂载到容器内部,权限只读
#- /opt/soft/mongo/mongo_test/data:/data/db
#- /opt/soft/mongo/mongo_test/conf:/data/configdb
- /opt/soft/mongo/keyfile.key:/data/configdb/keyfile.key
environment:
TZ: Asia/Shanghai # 设置时区为上海时区
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 123456
logging:
driver: json-file
options:
max-size: 10m
max-file: 5
networks:
- mongo
networks:
mongo:
driver: bridge
2 查找数据卷位置
数据卷通常存储在/var/lib/docker/volumes/
目录下。每个数据卷都有一个对应的文件夹。可以使用docker volume ls
列出所有容器使用的数据卷,然后使用docker inspect <volume_name>
查看特定数据卷的详细信息,找到其在宿主机上的存储路径。
[root@node01 mongo]# docker volume ls
DRIVER VOLUME NAME
local 93bb1c65b0329a2050c29f2ef35eee4761a86029f1222e4f9df7d332a2bb6381
local f789fdd5ef3c229c7c1cb19a1478a7aba7416490191a563572eaf010f12317a2
[root@node01 mongo]# ls /var/lib/docker/volumes/
93bb1c65b0329a2050c29f2ef35eee4761a86029f1222e4f9df7d332a2bb6381
backingFsBlockDev
f789fdd5ef3c229c7c1cb19a1478a7aba7416490191a563572eaf010f12317a2
metadata.db
可以发现宿主机上的两个目录正是docker容器使用的卷。
容器存在时,可通过查看容器内数据默认的挂载找到对应关系:
docker inspect mongo_test
"Mounts": [
{
"Type": "bind",
"Source": "/opt/soft/mongo/keyfile.key",
"Destination": "/data/configdb/keyfile.key",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "volume",
"Name": "f789fdd5ef3c229c7c1cb19a1478a7aba7416490191a563572eaf010f12317a2",
"Source": "/var/lib/docker/volumes/f789fdd5ef3c229c7c1cb19a1478a7aba7416490191a563572eaf010f12317a2/_data",
"Destination": "/data/configdb",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "93bb1c65b0329a2050c29f2ef35eee4761a86029f1222e4f9df7d332a2bb6381",
"Source": "/var/lib/docker/volumes/93bb1c65b0329a2050c29f2ef35eee4761a86029f1222e4f9df7d332a2bb6381/_data",
"Destination": "/data/db",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "bind",
"Source": "/etc/localtime",
"Destination": "/etc/localtime",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
}
]
如果容器不存在,可创建一个不挂载文件的新容器,从而分析已删除的原容器数据卷和宿主机上的卷积对应关系。
综上可以了解,上述两个数据卷是容器mongo_test使用的,且有如下对应关系:
/var/lib/docker/volumes/f789fdd5ef3c229c7c1cb19a1478a7aba7416490191a563572eaf010f12317a2:/data/configdb
/var/lib/docker/volumes/93bb1c65b0329a2050c29f2ef35eee4761a86029f1222e4f9df7d332a2bb6381:/data/db
3 将容器在宿主机上的数据复制到指定目录下
mkdir -p /opt/soft/mongo/mongo_test/{conf,data}
chmod 777 /opt/soft/mongo/mongo_test/data
chmod 777 /opt/soft/mongo/mongo_test/conf
cp -R /var/lib/docker/volumes/f789fdd5ef3c229c7c1cb19a1478a7aba7416490191a563572eaf010f12317a2/_data/* /opt/soft/mongo/mongo_test/conf
cp -R /var/lib/docker/volumes/93bb1c65b0329a2050c29f2ef35eee4761a86029f1222e4f9df7d332a2bb6381/_data/* /opt/soft/mongo/mongo_test/data
4 修改docker-compose并挂载数据(注意端口)
version: '3.1'
services:
mongo_test:
container_name: mongo_test
restart: always
image: mongo:8.0.3
ports:
- 17017:27017
volumes:
- /etc/localtime:/etc/localtime:ro # 将外边时间直接挂载到容器内部,权限只读
- /opt/soft/mongo/mongo_test/data:/data/db
- /opt/soft/mongo/mongo_test/conf:/data/configdb
- /opt/soft/mongo/keyfile.key:/data/configdb/keyfile.key
environment:
TZ: Asia/Shanghai # 设置时区为上海时区
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 123456
logging:
driver: json-file
options:
max-size: 10m
max-file: 5
networks:
- mongo
networks:
mongo:
driver: bridge
5 重新运行新容器
docker-compose up -d
至此,Docker容器数据恢复就完成了。