首页 > 解决方案 > 有没有办法将文件内容从不同的容器复制到主机文件系统?

问题描述

我正在尝试将文本文件从 docker 容器复制到主机文件系统。我们可以使用 docker cp 命令执行此操作,但真正的问题是:可能存在同一映像的不同容器,因为我的虚拟机将在某个时间后重新启动。当我重新启动机器时,同一图像会有不同的容器 ID。我必须在 python 脚本中包含这个命令,所以我不能显式地更改容器 ID。

我已经尝试过docker cp命令,我们必须在其中提及容器 ID。

def function():
    write some data into the file.
    file.close()
    """
    I need to execute commands or bash script here so that file I have 
    created in the container can be copied to host file system given 
    container id will change because of the machine restart. 
    """

标签: pythonshelldockerdocker-containerdocker-image

解决方案


推荐阅读