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

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


当前回答

从DockerContainer到LocalMachine

$docker cp containerId:/sourceFilePath/someFile.txt C:/localMachineDestinationFolder

从LocalMachine到DockerContainer

$docker cp C:/localMachineSourceFolder/someFile.txt containerId:/containerDestinationFolder

其他回答

太长,读不下去了

$ docker run --rm -iv${PWD}:/host-volume my-image sh -s <<EOF
chown $(id -u):$(id -g) my-artifact.tar.xz
cp -a my-artifact.tar.xz /host-volume
EOF

描述

docker使用主机卷运行,chown工件,cp工件到主机卷:

$ docker build -t my-image - <<EOF
> FROM busybox
> WORKDIR /workdir
> RUN touch foo.txt bar.txt qux.txt
> EOF
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
 ---> 00f017a8c2a6
Step 2/3 : WORKDIR /workdir
 ---> Using cache
 ---> 36151d97f2c9
Step 3/3 : RUN touch foo.txt bar.txt qux.txt
 ---> Running in a657ed4f5cab
 ---> 4dd197569e44
Removing intermediate container a657ed4f5cab
Successfully built 4dd197569e44

$ docker run --rm -iv${PWD}:/host-volume my-image sh -s <<EOF
chown -v $(id -u):$(id -g) *.txt
cp -va *.txt /host-volume
EOF
changed ownership of '/host-volume/bar.txt' to 10335:11111
changed ownership of '/host-volume/qux.txt' to 10335:11111
changed ownership of '/host-volume/foo.txt' to 10335:11111
'bar.txt' -> '/host-volume/bar.txt'
'foo.txt' -> '/host-volume/foo.txt'
'qux.txt' -> '/host-volume/qux.txt'

$ ls -n
total 0
-rw-r--r-- 1 10335 11111 0 May  7 18:22 bar.txt
-rw-r--r-- 1 10335 11111 0 May  7 18:22 foo.txt
-rw-r--r-- 1 10335 11111 0 May  7 18:22 qux.txt

这个技巧之所以有效,是因为heredoc中的chown调用从正在运行的容器外部获取$(id-u):$(id-g)值;即docker主机。

好处是:

您不必在之前运行docker容器--name或docker容器create--name你不必在

如果您只想从映像(而不是正在运行的容器)中提取文件,可以执行以下操作:

docker运行--rm<image>cat<source>><local_dest>

这将打开容器,写入新文件,然后删除容器。然而,一个缺点是文件权限和修改日期不会被保留。

如果您需要一个小文件,可以使用此部分

Docker容器内部

docker运行-it-p 4122:4122<container_ID>

nc -l -p 4122 < Output.txt

主机

nc 127.0.0.1 4122 > Output.txt

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

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

实例

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

我为任何使用Docker for Mac的人发布这个。这就是对我有用的:

 $ mkdir mybackup # local directory on Mac

 $ docker run --rm --volumes-from <containerid> \
    -v `pwd`/mybackup:/backup \  
    busybox \                   
    cp /data/mydata.txt /backup 

注意,当我使用-v装载时,会自动创建备份目录。

我希望有一天这对某人有用