docker / docker/docker-py

how to export container to tar file?

Open
#2,274 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

I want to export container to a tar file, and the export API return a generator. I refer to the example code here.

            >>> c = docker.APIClient()
            >>> f = open('./sh_bin.tar', 'wb')
            >>> bits, stat = c.get_archive(container, '/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()

And bellow is my code.

cli = docker.from_env(version="auto", timeout=3600).api 
image = cli.export(container="2991b5d0ead6")

temporary_dir = tempfile.mkdtemp()
with open(temporary_dir + "/" + name + ".tar", "wb") as file:
        for chunk in image:
            file.write(chunk)
tarfile = TarFile(temporary_dir + "/" + name + ".tar")
tarfile.extractall(temporary_dir)

But when I try to extract the tar file in the command line, I got message bellow.

$ tar -xvf 2991b5d0ead6.tar                             
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors

Can someone help me? Thank you!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Compare the APIClient.export call with the get_archive example in the issue, then reproduce the generated file with the provided Python code and command-line tar extraction. Inspect the Docker API client behavior to determine whether the output should be a valid tar archive; done means the cause is documented or the export path reliably produces an extractable archive.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.