$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1b4ad9311e93 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
mkdir artifactsdocker run -i -v ${PWD}/artifacts:/artifacts ubuntu:14.04 sh << COMMANDS# ... build software here ...cp <artifact> /artifacts# ... copy more artifacts into `/artifacts` ...COMMANDS
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
$ mkdir mybackup # local directory on Mac
$ docker run --rm --volumes-from <containerid> \-v `pwd`/mybackup:/backup \busybox \cp /data/mydata.txt /backup
docker build -t IMAGE_TAG .docker run -d IMAGE_TAGCONTAINER_ID=$(docker ps -alq)# If you do not know the exact file name, you'll need to run "ls"# FILE=$(docker exec CONTAINER_ID sh -c "ls /path/*.zip")docker cp $CONTAINER_ID:/path/to/file .docker stop $CONTAINER_ID
Help on method get_archive in module docker.models.containers:
get_archive(path, chunk_size=2097152) method of docker.models.containers.Container instanceRetrieve a file or folder from the container in the form of a tararchive.
Args:path (str): Path to the file or folder to retrievechunk_size (int): The number of bytes returned by each iterationof the generator. If ``None``, data will be streamed as it isreceived. Default: 2 MB
Returns:(tuple): First element is a raw tar data stream. Second element isa 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 dockerimport osimport tarfile
# Docker clientclient = docker.from_env()#container objectcontainer = client.containers.get("relaxed_pasteur")#setup tar to write bits tof = open(os.path.join(os.getcwd(),"output.tar"),"wb")#get the bitsbits, stat = container.get_archive('/output')#write the bitsfor chunk in bits:f.write(chunk)f.close()#unpacktar = tarfile.open("output.tar")tar.extractall()tar.close()