我正在尝试为我们使用的Docker容器构建一个备份和恢复解决方案。
我有一个我创建的Docker基础镜像ubuntu:base,不想每次都用Docker文件重新构建它来添加文件。
我想创建一个从主机运行的脚本,并使用ubuntu:base Docker映像创建一个新容器,然后将文件复制到该容器中。
如何将文件从主机复制到容器?
我正在尝试为我们使用的Docker容器构建一个备份和恢复解决方案。
我有一个我创建的Docker基础镜像ubuntu:base,不想每次都用Docker文件重新构建它来添加文件。
我想创建一个从主机运行的脚本,并使用ubuntu:base Docker映像创建一个新容器,然后将文件复制到该容器中。
如何将文件从主机复制到容器?
当前回答
真正的交易是:
docker volume create xdata
docker volume inspect xdata
所以你可以看到它的装载点。把你的东西复制到那里然后
docker run --name=example1 --mount source=xdata,destination=/xdata -it yessa bash
其中yessa是图像名称
其他回答
通常有三种类型:
从容器到主机docker cp container_id:/bar/foo.txt。
docker cp命令也可以双向工作。
从主机到容器docker exec-i container_id sh-c“cat>/bar/foo.txt“</foo.txt文件从主机复制到容器的第二种方法:docker cp foo.txt mycontainer:/foo.txt
从一个容器到一个容器混合1和2docker cp container_id1:/bar/foo.txt。docker exec-i container_id2 sh-c“cat>/bar/foo.txt“</foo.txt文件
将点文件放入shell会话(使用base64转义)
有时,对shell启动执行单独的交互式命令会很麻烦。当我想将一个点文件复制到我的容器中,这样我的shell就会包含我的自定义项
docker exec -u root -it mycontainername bash -c "echo "$(cat ~/.bashrc | base64 -w 0)" | base64 --decode > /tmp/.bashrc_inside_container; bash --rcfile /tmp/.bashrc_inside_container"
将文件从主机复制到正在运行的容器
docker exec -i $CONTAINER /bin/bash -c "cat > $CONTAINER_PATH" < $HOST_PATH
根据Erik的回答以及Mikl和z0r的评论。
这就是对我有用的
#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
如果像我这样的人不清楚@h3nrik答案中的mycontainer是什么意思,它实际上是容器id。为了将/app/example_scenes/1440p60中的WarpSquare.mp4文件从退出的docker容器复制到当前文件夹,我使用了这个。
docker cp `docker ps -q -l`:/app/example_scenes/1440p60/WarpSquare.mp4 .
其中docker ps-q-l提取最后退出实例的容器id。如果它不是已退出的容器,您可以通过docker容器ls或docker ps获取它