我正在尝试为我们使用的Docker容器构建一个备份和恢复解决方案。

我有一个我创建的Docker基础镜像ubuntu:base,不想每次都用Docker文件重新构建它来添加文件。

我想创建一个从主机运行的脚本,并使用ubuntu:base Docker映像创建一个新容器,然后将文件复制到该容器中。

如何将文件从主机复制到容器?


当前回答

答案很好,但过于具体。我发现docker ps是获取您感兴趣的容器id的好方法

mount | grep <id>

查看卷的安装位置。那是

/var/lib/docker/devicemapper/mnt/<id>/rootfs/

对我来说,但这可能是一条不同的路径,具体取决于操作系统和配置。现在只需将文件复制到该路径。

使用-v并不总是实用的。

其他回答

以下是一种相当丑陋的方法,但它确实有效。

docker run -i ubuntu /bin/bash -c 'cat > file' < file

您可以使用

ifconfig

然后只需输入Docker容器并键入

scp user_name@ip_address:/path_to_the_file destination

在任何情况下,如果您没有安装SSH客户端和服务器,只需使用以下方法进行安装:

sudo apt-get install openssh-server

我会装载并运行带有守护程序的映像,就像这里给出的那样;

docker run -d -v /blah1/blah2:/mnt --name mntcontainer ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

then

docker exec -it mntcontainer bash

创建一个新的dockerfile并使用现有图像作为基础。FROM myName/myImage:最新添加myFile.py bin/myFile.py然后构建容器:码头建造。

最干净的方法是在启动容器时在容器上装载主机目录:

{host} docker run -v /path/to/hostdir:/mnt --name my_container my_image
{host} docker exec -it my_container bash
{container} cp /mnt/sourcefile /path/to/destfile