我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
当前回答
这也可以在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()
其他回答
我将PowerShell(Admin)与此命令一起使用。
docker cp {container id}:{container path}/error.html C:\\error.html
实例
docker cp ff3a6608467d:/var/www/app/error.html C:\\error.html
作为一个更通用的解决方案,Jenkins有一个CloudBees插件可以在Docker容器中构建。您可以从Docker注册表中选择要使用的图像,或者定义要构建和使用的Dockerfile。
它将把工作区作为一个卷装载到容器中(使用适当的用户),将其设置为工作目录,执行您请求的任何命令(在容器内)。您还可以使用docker工作流插件(如果您更喜欢代码而不是UI),使用image.inside(){}命令来完成此操作。
基本上,所有这些都要烘焙到CI/CD服务器中,然后再进行一些。
要将文件从容器复制到主机,可以使用以下命令
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允许您引入一个命令作为参数,该命令会将作业文件复制到容器路径。
我希望这个额外的答案可以帮助你