我正在尝试为我们使用的Docker容器构建一个备份和恢复解决方案。
我有一个我创建的Docker基础镜像ubuntu:base,不想每次都用Docker文件重新构建它来添加文件。
我想创建一个从主机运行的脚本,并使用ubuntu:base Docker映像创建一个新容器,然后将文件复制到该容器中。
如何将文件从主机复制到容器?
我正在尝试为我们使用的Docker容器构建一个备份和恢复解决方案。
我有一个我创建的Docker基础镜像ubuntu:base,不想每次都用Docker文件重新构建它来添加文件。
我想创建一个从主机运行的脚本,并使用ubuntu:base Docker映像创建一个新容器,然后将文件复制到该容器中。
如何将文件从主机复制到容器?
当前回答
从容器复制到主机目录
docker cp [container-name/id]:./app/[index.js] index.js
(假设您在dockerfile中创建了一个workdir/app)
从主机复制到容器
docker cp index.js [container-name/id]:./app/index.js
其他回答
使用Docker 1.8,Docker cp能够将文件从主机复制到容器。请参阅Docker博客文章Announcing Docker 1.8:内容信任、工具箱以及注册表和编排更新。
我通常使用以下命令创建python服务器
python-m SimpleHTTPServer
然后使用wget将文件传输到docker中所需的位置。我知道这不是最好的方法,但我发现这要容易得多。
您可以使用以下命令
将文件从主机复制到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
这就是对我有用的
#Run the docker image in detached mode
$docker run -it -d test:latest bash
$ docker ps
CONTAINER ID IMAGE COMMAND
a6c1ff6348f4 test:latest "bash"
#Copy file from host to container
sudo docker cp file.txt a6c1ff6348f4:/tmp
#Copy the file from container to host
docker cp test:/tmp/file.txt /home
解决方案如下所示,
从Docker外壳,
root@123abc:/root# <-- get the container ID
来自主机
cp thefile.txt /var/lib/docker/devicemapper/mnt/123abc<bunch-o-hex>/rootfs/root
文件应直接复制到文件系统中容器所在的位置。