我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。

为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?


当前回答

我将PowerShell(Admin)与此命令一起使用。

docker cp {container id}:{container path}/error.html  C:\\error.html

实例

docker cp ff3a6608467d:/var/www/app/error.html  C:\\error.html

其他回答

另一个好的选择是首先构建容器,然后使用-c标志和shell解释器运行它,以执行一些命令

docker run --rm -i -v <host_path>:<container_path> <mydockerimage> /bin/sh -c "cp -r /tmp/homework/* <container_path>"

上述命令执行以下操作:

-i=以交互模式运行容器

--rm=执行后删除容器。

-v=将文件夹作为卷从主机路径共享到容器路径。

最后,/bin/sh-c允许您引入一个命令作为参数,该命令会将作业文件复制到容器路径。

我希望这个额外的答案可以帮助你

大多数答案并没有表明容器必须在docker cp工作之前运行:

docker build -t IMAGE_TAG .
docker run -d IMAGE_TAG
CONTAINER_ID=$(docker ps -alq)
# If you do not know the exact file name, you'll need to run "ls"
# FILE=$(docker exec CONTAINER_ID sh -c "ls /path/*.zip")
docker cp $CONTAINER_ID:/path/to/file .
docker stop $CONTAINER_ID
sudo docker cp <running_container_id>:<full_file_path_in_container> <path_on_local_machine>

例子:

sudo docker cp d8a17dfc455f:/tests/reports /home/acbcb/Documents/abc

如果您没有正在运行的容器,只有一个图像,并且假设您只想复制一个文本文件,可以执行以下操作:

docker run the-image cat path/to/container/file.txt > path/to/host/file.txt

最简单的方法是创建一个容器,获取ID,然后从那里复制

IMAGE_TAG=my-image-tag
container=$(docker create ${IMAGE_TAG})
docker cp ${container}:/src-path ./dst-path/