Search code examples
python-3.xdockerpy

Problems with docker py, UnicodeDecodeError at tar read on image build


I am trying to make docker image from generated tar file, the problem is thah the tar file wats generates is valid, and the code worked before.

t = tarfile.open('dockerfile.tar',mode='w')
file_name = tarfile.TarInfo("dockerfile")
file_name.size = len(dockerfile)
t.addfile(file_name, BytesIO(dockerfile.encode('utf-8')))
for file in static_files:
   t.add(static_files_path + file,arcname='root/'+file)
for file in files:
    if type(file["data"]) == str:
        file_name = tarfile.TarInfo('root/'+file["file"])
        file_name.size = len(file["data"].encode())
          t.addfile(file_name, BytesIO(file["data"].encode()))
    else:
        file_name = tarfile.TarInfo('root/'+file["file"])
        file_name.size = len(file["data"])
        t.addfile(file_name, BytesIO(file["data"]))
    t.close()
    docker_client = Client(base_url=docker_host)
    c=open('dockerfile.tar','r')
    response = [line for line in docker_client.build(fileobj=c, rm=False, tag=image_name, custom_context=True)]

What I get:

File "/usr/lib/python3.4/codecs.py", line 313, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 3584: invalid start byte
>>> data[3580:3590]
b'\x00\x00\x00\x00\x89PNG\r\n'

The problem is in \x89PNG , but why this is hapening? bug ?


Solution

  • Ok, the problem was in:

    c=open('dockerfile.tar','r')  
    

    should be

    c=open('dockerfile.tar','rb')