ak_1
ak_1

Reputation: 183

docker-py getarchive destination folder

I am following the instructions given in this link for get_archive()link but instead of creating my container i was trying to use already running container in my case "docker-nginx" as an input string and also the destination folder where my content was residing in nginx server as '/usr/share/nginx/html' and got my output for the stat of the folder too i want to know how can i give the destination folder in this function if not where is my extrated tar file ? i was not able to figure out where my file was downloaded

here is my code strm,stat=c.get_archive(container_name,'/usr/share/nginx/html')

my output for print(strm) is <requests.packages.urllib3.response.HTTPResponse object at 0x7fe3581e1250> my output for print(stat) is {u'linkTarget': u'', u'mode': 2147484157, u'mtime': u'2016-10-05T09:37:17.928258508-05:00', u'name': u'html', u'size': 4096}

Upvotes: 2

Views: 3706

Answers (3)

Ali sahin
Ali sahin

Reputation: 21

Answers above are partially true. If you write tar stream directly, it writes extra unkown metadata parameters.

In order to write only content, please use following,

from io import StringIO
strm,status = container.get_archive("container_path/")
for d in strm:
   pw_tar = tarfile.TarFile(fileobj=StringIO(d.decode('utf-8')))
   pw_tar.extractall("extract_to/")

folder or file can be extracted from stream.

Upvotes: 1

Aaron
Aaron

Reputation: 7038

The answer depends on which version of the docker sdk you're using.

For docker sdk versions pre-3.X

get_archive returns a file-like stream. So, use shutil.copyfileobj to stream the data to a file:

import shutil

strm, stat = container.get_archive(container_name, path_in_container)
with open('out.html', 'w') as outfile:
    shutil.copyfileobj(strm, outfile)

For docker sdk 3.X+

get_archive returns an iterator over the file's content, so just iterate and write:

strm, stat = container.get_archive(container_name, path_in_container)
with open('out.html', 'w') as outfile:
    for d in strm:
        outfile.write(d)

Upvotes: 1

ak_1
ak_1

Reputation: 183

I found out how this function works what it does it returns raw response of a requests made to our container. raw response explaination can be found in this link in order to extract raw content we can use this code so that we can simply read our raw data as text format according to the output file in my case html.

raw_data=strm.read()
f= open('out.html', 'w')
f.write(raw_data)

Upvotes: 0

Related Questions