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

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


当前回答

对于任何试图使用MySQL容器并在您的计算机上本地存储卷的人来说。我使用了这个问题的顶级回复中提供的语法。但必须使用MySQL特有的特定路径

docker cp imageIdHere:/var/lib/mysql pathToYourLocalMachineHere

其他回答

太长,读不下去了

$ 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你不必在

如果您只想装载一个文件夹,而不是为容器创建特殊存储,则可以使用bind而不是volume:

使用标签构建图像:码头建造-t<图像>运行映像并绑定app.py存储的当前$(pwd)目录,并将其映射到容器内的/root/example/。docker run--mount type=bind,source=“$(pwd)”,target=/root/example/<image>python app.py

您不需要使用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: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

另一个好的选择是首先构建容器,然后使用-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允许您引入一个命令作为参数,该命令会将作业文件复制到容器路径。

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