Skip to content

Instantly share code, notes, and snippets.

@zbyte64
Created June 27, 2016 23:00
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zbyte64/6800eae10ce082bb78f0b7a2cca5cbc2 to your computer and use it in GitHub Desktop.
Save zbyte64/6800eae10ce082bb78f0b7a2cca5cbc2 to your computer and use it in GitHub Desktop.
Docker py put archive example
import tarfile
import time
from io import BytesIO
admin_password = 'xxxxx'
#write password to file
pw_tarstream = BytesIO()
pw_tar = tarfile.TarFile(fileobj=pw_tarstream, mode='w')
file_data = admin_password.encode('utf8')
tarinfo = tarfile.TarInfo(name='pw.txt')
tarinfo.size = len(file_data)
tarinfo.mtime = time.time()
#tarinfo.mode = 0600
pw_tar.addfile(tarinfo, BytesIO(file_data))
pw_tar.close()
container = docker_client.create_container(
**container_options
)
pw_tarstream.seek(0)
pr = docker_client.put_archive(
container=container['Id'],
path='/tmp',
data=pw_tarstream
)
@di
Copy link

di commented Aug 9, 2016

Thanks for this! It's exactly what I was looking for.

Worth mentioning that calling pw_tarstream.seek(0) is pretty important, otherwise you end up putting an empty byte stream. 😃

@vijaykv1
Copy link

Was searching around for this bit of code !! Thanks a lot!! 😄

@aqib-abdullahi
Copy link

Thanks a lot for this. spent a whole day going through this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment