我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
我正在考虑使用Docker在持续集成(CI)服务器上构建依赖关系,这样我就不必在代理本身上安装所有运行时和库。
为了实现这一点,我需要将容器内部构建的构建工件复制回主机。这可能吗?
当前回答
在主机系统(容器外部)上创建一个数据目录,并将其装载到容器内部可见的目录中。这将文件放置在主机系统上的已知位置,并使主机系统中的工具和应用程序可以轻松访问文件
docker run -d -v /path/to/Local_host_dir:/path/to/docker_dir docker_image:tag
其他回答
从DockerContainer到LocalMachine
$docker cp containerId:/sourceFilePath/someFile.txt C:/localMachineDestinationFolder
从LocalMachine到DockerContainer
$docker cp C:/localMachineSourceFolder/someFile.txt containerId:/containerDestinationFolder
如果您只想从映像(而不是正在运行的容器)中提取文件,可以执行以下操作:
docker运行--rm<image>cat<source>><local_dest>
这将打开容器,写入新文件,然后删除容器。然而,一个缺点是文件权限和修改日期不会被保留。
您不需要使用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
在这里,您永远不会启动容器。这看起来对我有利。
这也可以在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 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