Migrate Docker volume to local mount point

Original link: https://blog.frytea.com/archives/760/

75

To migrate a Docker volume to a local mount point, you need to perform the following steps:

  1. First, make sure that any containers that are using the volume are stopped. You can stop a container with the following command (replace <container_name> with your container name): `
    docker stop <container_name>
 1. 使用docker cp 命令将volume 的内容复制到本地文件系统。首先,创建一个临时容器,将要迁移的volume 挂载到该容器: ``` docker run -d --name temp_container -v <volume_name>:/volume_data busybox tail -f /dev/null

Replace <volume_name> with the name of the volume to migrate.

  1. Use the docker cp command to copy the volume data from the temporary container to a local directory: `
    docker cp temp_container:/volume_data <local_mount_point>
将<local_mount_point> 替换为要将数据复制到的本地目录。 1. 删除临时容器: ``` docker rm -f temp_container
  1. Update your docker run command or docker-compose.yml file to change the original volume to a local mount point. For example, if your previous docker run command was as follows: `
    docker run -d –name my_container -v <volume_name>:/data my_image
将其更改为:

docker run -d –name my_container -v <local_mount_point>:/data my_image

将<local_mount_point> 替换为您在步骤3 中使用的本地目录。 1. 启动更新后的容器,并验证数据是否已成功迁移到本地挂载点。请注意,根据volume 的大小和复杂性,迁移过程可能会花费一定时间。在迁移期间,请确保不要对volume 进行修改以避免数据不一致。

This article is transferred from: https://blog.frytea.com/archives/760/
This site is only for collection, and the copyright belongs to the original author.