我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
当前回答
如果您只想从映像(而不是正在运行的容器)中提取文件,可以执行以下操作:
docker运行--rm<image>cat<source>><local_dest>
这将打开容器,写入新文件,然后删除容器。然而,一个缺点是文件权限和修改日期不会被保留。
其他回答
太长,读不下去了
$ 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 cp <containerId>:/file/path/within/container /host/path/target
下面是一个示例:
$ sudo docker cp goofy_roentgen:/out_read.jpg .
这里,高飞_伦琴是我从以下命令中获得的容器名称:
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b4ad9311e93 bamos/openface "/bin/bash" 33 minutes ago Up 33 minutes 0.0.0.0:8000->8000/tcp, 0.0.0.0:9000->9000/tcp goofy_roentgen
您还可以使用(部分)容器ID。以下命令相当于第一个
$ sudo docker cp 1b4a:/out_read.jpg .
docker cp[选项]SRC_PATH容器:DEST_PATH
DEST_PATH必须预先存在
对于任何试图使用MySQL容器并在您的计算机上本地存储卷的人来说。我使用了这个问题的顶级回复中提供的语法。但必须使用MySQL特有的特定路径
docker cp imageIdHere:/var/lib/mysql pathToYourLocalMachineHere
如果您需要一个小文件,可以使用此部分
Docker容器内部
docker运行-it-p 4122:4122<container_ID>
nc -l -p 4122 < Output.txt
主机
nc 127.0.0.1 4122 > Output.txt