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

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

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

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


当前回答

另一个解决方法是使用好的旧scp。这在需要复制目录的情况下非常有用。

从主机运行:

scp FILE_PATH_ON_YOUR_HOST IP_CONTAINER:DESTINATION_PATH
scp foo.txt 172.17.0.2:foo.txt

如果需要复制目录:

scp -r DIR_PATH_ON_YOUR_HOST IP_CONTAINER:DESTINATION_PATH
scp -r directory 172.17.0.2:directory

一定要将ssh安装到容器中。

apt-get install openssh-server

其他回答

从容器复制到主机目录

docker cp [container-name/id]:./app/[index.js] index.js

(假设您在dockerfile中创建了一个workdir/app)

从主机复制到容器

docker cp index.js [container-name/id]:./app/index.js

您可以使用以下命令

将文件从主机复制到docker容器docker cp/主机文件container_id:/to_the_place_you_want_the_file_to_be将文件从docker容器复制到主机docker cp container_id:src_path to_the_place_you_want_the_file_to_be

试试docker cp。

用法:

docker cp CONTAINER:PATH HOSTPATH

它将文件/文件夹从PATH复制到HOSTPATH。

真正的交易是:

docker volume create xdata
docker volume inspect xdata

所以你可以看到它的装载点。把你的东西复制到那里然后

docker run  --name=example1 --mount source=xdata,destination=/xdata -it yessa bash

其中yessa是图像名称

解决方案如下所示,

从Docker外壳,

root@123abc:/root#  <-- get the container ID

来自主机

cp thefile.txt /var/lib/docker/devicemapper/mnt/123abc<bunch-o-hex>/rootfs/root

文件应直接复制到文件系统中容器所在的位置。