我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
当前回答
docker cp containerId:source_path destination_path
containerId可以从docker ps-a命令获得
源路径应该是绝对的。例如,如果应用程序/服务目录从docker容器中的应用程序开始,则路径为/app/some_directory/file
示例:docker cp d86844abc129:/app/server/output/server-test.png C:/Users/someone/Desktop/output
其他回答
最简单的方法是创建一个容器,获取ID,然后从那里复制
IMAGE_TAG=my-image-tag
container=$(docker create ${IMAGE_TAG})
docker cp ${container}:/src-path ./dst-path/
如果您没有正在运行的容器,只有一个图像,并且假设您只想复制一个文本文件,可以执行以下操作:
docker run the-image cat path/to/container/file.txt > path/to/host/file.txt
太长,读不下去了
$ 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处理。
装载一个“卷”并将工件复制到其中:
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