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

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


当前回答

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

Docker容器内部

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

nc -l -p 4122 < Output.txt

主机

nc 127.0.0.1 4122 > Output.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处理。

docker cp[选项]SRC_PATH容器:DEST_PATH

DEST_PATH必须预先存在

这也可以在SDK中完成,例如python。如果你已经构建了一个容器,你可以通过控制台(docker ps-a)查找这个名称,这个名称似乎是科学家和形容词(即“relaxed_pasteur”)的连接。

查看帮助(container.get_archive):

Help on method get_archive in module docker.models.containers:

get_archive(path, chunk_size=2097152) method of docker.models.containers.Container instance
    Retrieve a file or folder from the container in the form of a tar
    archive.

    Args:
        path (str): Path to the file or folder to retrieve
        chunk_size (int): The number of bytes returned by each iteration
            of the generator. If ``None``, data will be streamed as it is
            received. Default: 2 MB

    Returns:
        (tuple): First element is a raw tar data stream. Second element is
        a dict containing ``stat`` information on the specified ``path``.

    Raises:
        :py:class:`docker.errors.APIError`
            If the server returns an error.

    Example:

        >>> f = open('./sh_bin.tar', 'wb')
        >>> bits, stat = container.get_archive('/bin/sh')
        >>> print(stat)
        {'name': 'sh', 'size': 1075464, 'mode': 493,
         'mtime': '2018-10-01T15:37:48-07:00', 'linkTarget': ''}
        >>> for chunk in bits:
        ...    f.write(chunk)
        >>> f.close()

因此,类似这样的东西将从容器中的指定路径(/output)拉出到主机并解压缩tar。

import docker
import os
import tarfile

# Docker client
client = docker.from_env()
#container object
container = client.containers.get("relaxed_pasteur")
#setup tar to write bits to
f = open(os.path.join(os.getcwd(),"output.tar"),"wb")
#get the bits
bits, stat = container.get_archive('/output')
#write the bits
for chunk in bits:
    f.write(chunk)
f.close()
#unpack
tar = tarfile.open("output.tar")
tar.extractall()
tar.close()
docker run -dit --rm IMAGE
docker cp CONTAINER:SRC_PATH DEST_PATH

https://docs.docker.com/engine/reference/commandline/run/https://docs.docker.com/engine/reference/commandline/cp/