我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
当前回答
您不需要使用docker run。
你可以用docker create来完成。
从文档中:
docker create命令在指定的映像上创建一个可写容器层,并为运行指定的命令做好准备。然后将容器ID打印到STDOUT。这与docker run-d类似,只是容器从未启动。
因此,您可以做到:
docker create --name dummy IMAGE_NAME
docker cp dummy:/path/to/file /dest/to/file
docker rm -f dummy
在这里,您永远不会启动容器。这看起来对我有利。
其他回答
要将文件从容器复制到主机,可以使用以下命令
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 .
装载一个“卷”并将工件复制到其中:
mkdir artifacts
docker run -i -v ${PWD}/artifacts:/artifacts ubuntu:14.04 sh << COMMANDS
# ... build software here ...
cp <artifact> /artifacts
# ... copy more artifacts into `/artifacts` ...
COMMANDS
然后,当构建完成并且容器不再运行时,它已经将构建中的工件复制到主机上的工件目录中。
Edit
注意:当您这样做时,可能会遇到docker用户的用户id与当前运行用户的用户id匹配的问题。也就是说,/fartifacts中的文件将显示为用户所有,用户的UID在docker容器中使用。解决此问题的一种方法可能是使用主叫用户的UID:
docker run -i -v ${PWD}:/working_dir -w /working_dir -u $(id -u) \
ubuntu:14.04 sh << COMMANDS
# Since $(id -u) owns /working_dir, you should be okay running commands here
# and having them work. Then copy stuff into /working_dir/artifacts .
COMMANDS
另一个好的选择是首先构建容器,然后使用-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 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你不必在
装载卷、复制工件、调整所有者id和组id:
mkdir artifacts
docker run -i --rm -v ${PWD}/artifacts:/mnt/artifacts centos:6 /bin/bash << COMMANDS
ls -la > /mnt/artifacts/ls.txt
echo Changing owner from \$(id -u):\$(id -g) to $(id -u):$(id -g)
chown -R $(id -u):$(id -g) /mnt/artifacts
COMMANDS
编辑:请注意,某些命令(如$(id-u))是反斜杠的,因此将在容器中处理,而未反斜杠的命令将在命令发送到容器之前由主机中运行的shell处理。