如何将卷数据从 docker-for-mac 迁移到 colima

发布于 2025-01-09 21:36:27 字数 65 浏览 1 评论 0原文

如何将卷从 docker-for-mac 移动到 colima

How do I move volumes from docker-for-mac into colima?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我鬧 2025-01-16 21:36:27

将从 docker-for-mac 复制所有卷并将其移动到 colima。

注意:您可能不想复制很多卷,因为它们是临时卷,您只需添加 | 即可忽略它们。 grep "YOUR FILTER"for 循环,在 awk 之前或之后。

以下代码做了 2 个假设:

  1. 您有 docker- for-mac 安装并正在运行,
  2. 您已经运行了 colima

这就是您所需要的,现在将其复制并粘贴到您的终端中。不需要碰任何东西。

(
# set -x  # uncomment to debug
set -e

# ssh doesn't like file descriptor piping, we need to write the configuration into someplace real
tmpconfig=$(mktemp);

# Need to have permissions to copy the volumes, and need to remove the ControlPath and add ForwardAgent
(limactl show-ssh --format config colima | grep -v "^  ControlPath\|  ^User"; echo "  ForwardAgent=yes") > $tmpconfig;

# Setup root account
ssh -F $tmpconfig $USER@lima-colima "sudo mkdir -p /root/.ssh/; sudo cp ~/.ssh/authorized_keys /root/.ssh/authorized_keys"

# Loop over each volume inside docker-for-mac
for volume_name in $(DOCKER_CONTEXT=desktop-linux docker volume ls | awk '{print $2}'); do 
    echo $volume_name;

    # Make the volume backup
    DOCKER_CONTEXT=desktop-linux docker run -d --rm --mount source=$volume_name,target=/volume --name copy-instance busybox sleep infinate; 
    DOCKER_CONTEXT=desktop-linux docker exec copy-instance sh -c "tar czf /$volume_name.tar /volume";
    DOCKER_CONTEXT=desktop-linux docker cp copy-instance:/$volume_name.tar /tmp/$volume_name.tar; 
    DOCKER_CONTEXT=desktop-linux docker kill copy-instance;

    # Restore the backup inside colima
    DOCKER_CONTEXT=colima docker volume create $volume_name;
    ssh -F $tmpconfig root@lima-colima "rm -rf /var/lib/docker/volumes/$volume_name; mkdir -p /var/lib/docker/volumes/$volume_name/_data";
    scp -r -F $tmpconfig /tmp/$volume_name.tar root@lima-colima:/tmp/$volume_name.tar;
    ssh -F $tmpconfig root@lima-colima "tar -xf /tmp/$volume_name.tar --strip-components=1 --directory /var/lib/docker/volumes/$volume_name/_data";
    
done
)

Will copy all the volumes from docker-for-mac and move them to colima.

Note: there will be a lot of volumes you may not want to copy over since they're temporary ones, you can ignore them by simply adding a | grep "YOUR FILTER" to the for loop, either before or after the awk.

The following code makes 2 assumptions:

  1. you have docker-for-mac installed and running
  2. you have colima running

That is all you need, now copy-and-paste this into your terminal. No need to touch anything.

(
# set -x  # uncomment to debug
set -e

# ssh doesn't like file descriptor piping, we need to write the configuration into someplace real
tmpconfig=$(mktemp);

# Need to have permissions to copy the volumes, and need to remove the ControlPath and add ForwardAgent
(limactl show-ssh --format config colima | grep -v "^  ControlPath\|  ^User"; echo "  ForwardAgent=yes") > $tmpconfig;

# Setup root account
ssh -F $tmpconfig $USER@lima-colima "sudo mkdir -p /root/.ssh/; sudo cp ~/.ssh/authorized_keys /root/.ssh/authorized_keys"

# Loop over each volume inside docker-for-mac
for volume_name in $(DOCKER_CONTEXT=desktop-linux docker volume ls | awk '{print $2}'); do 
    echo $volume_name;

    # Make the volume backup
    DOCKER_CONTEXT=desktop-linux docker run -d --rm --mount source=$volume_name,target=/volume --name copy-instance busybox sleep infinate; 
    DOCKER_CONTEXT=desktop-linux docker exec copy-instance sh -c "tar czf /$volume_name.tar /volume";
    DOCKER_CONTEXT=desktop-linux docker cp copy-instance:/$volume_name.tar /tmp/$volume_name.tar; 
    DOCKER_CONTEXT=desktop-linux docker kill copy-instance;

    # Restore the backup inside colima
    DOCKER_CONTEXT=colima docker volume create $volume_name;
    ssh -F $tmpconfig root@lima-colima "rm -rf /var/lib/docker/volumes/$volume_name; mkdir -p /var/lib/docker/volumes/$volume_name/_data";
    scp -r -F $tmpconfig /tmp/$volume_name.tar root@lima-colima:/tmp/$volume_name.tar;
    ssh -F $tmpconfig root@lima-colima "tar -xf /tmp/$volume_name.tar --strip-components=1 --directory /var/lib/docker/volumes/$volume_name/_data";
    
done
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文