Created
April 17, 2019 14:59
-
-
Save haniokasai/d5417f0a7fa7399e4f1b337634a6947f to your computer and use it in GitHub Desktop.
Docker-py sample script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import tarfile | |
import docker | |
from docker.models.containers import Container | |
c = docker.APIClient(base_url='tcp://192.168.186.128:5555', timeout=5) | |
print(c.containers()) | |
#cl = docker.client.APIClient(base_url='tcp://192.168.186.128:5555', timeout=5) | |
print(c.version()) | |
print(c.info()) | |
print(docker.version) | |
# | |
import json | |
from json.decoder import WHITESPACE | |
def loads_iter(s): | |
size = len(s) | |
decoder = json.JSONDecoder() | |
end = 0 | |
while True: | |
idx = WHITESPACE.match(s[end:]).end() | |
i = end + idx | |
if i >= size: | |
break | |
ob, end = decoder.raw_decode(s, i) | |
yield ob | |
#forで回す必要あり | |
#for line in c.pull("busybox", stream=True): | |
# print(line) | |
#おかしなリクエスト(設定のおかしなものも含む)は {"message":"page not found"} と返却してしまう | |
print(c.images(name="uphy/ubuntu-desktop-jp")) | |
#c.create_container('busybox', 'ls', ports=[1111, 2222]) | |
#CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |
#629beaaf2755 uphy/ubuntu-desktop-jp:18.04 "/usr/bin/supervisor…" About a minute ago Up About a minute 8080/tcp, 127.0.0.1:6080->80/tcp romantic_sutherland | |
""" | |
stack = c.create_container(image='uphy/ubuntu-desktop-jp:18.04',detach=True,host_config=c.create_host_config(port_bindings={8080: ('0.0.0.0', 8080)},storage_opt={"size":"1g"}),stdin_open=True) | |
print(stack["Id"]) | |
print(stack["Warnings"]) | |
""" | |
#print(cl.start(stack)) | |
#mktar 指定dirをtarballでローカルで固める | |
""" | |
f = open('./sh_bin.tar', 'wb') | |
bits, stat = c.get_archive(stack, '/') | |
print(stat) | |
for chunk in bits: | |
f.write(chunk) | |
f.close() | |
""" | |
# Insert a file or folder in an existing container using a tar archive assource. | |
#c.put_archive("path",tardata) | |
# コンテナの取得 | |
#未テスト | |
#c.export() | |
""" | |
con = c.export("eee126213653") | |
f = open('./busybox-latest.tar', 'wb') | |
for chunk in con: | |
f.write(chunk) | |
f.close() | |
""" | |
#image save Get a tarball of an image. Similar to the ``docker save`` command.https://github.com/docker/docker-py/blob/02e660da12ecb5cf755b17ffd850ce3da21ecec0/docker/models/images.py#L87 | |
""" | |
image = c.get_image("busybox:latest") | |
f = open('./busybox-latestgetimage.tar', 'wb') | |
for chunk in image: | |
f.write(chunk) | |
f.close() | |
""" | |
# Load an image that was previously saved using :py:meth:`~docker.api.image.ImageApiMixin.get_image` (or ``docker save``). Similar to ``docker load``. | |
#tar = tarfile.open("./busybox-latestgetimage.tar", "r|") | |
#print(c.load_image(data=tar)) | |
#busybox latest af2f74c517aa 11 days ago 1.2MB | |
c.load_image(data=open("./busybox-latestgetimage.tar","rb").read()) | |
#import as tarfile like "docker import" | |
#<none> <none> c49ad9dfe280 12 minutes ago 1.42MB | |
print(c.import_image_from_file(filename="./busybox-latestgetimage.tar",repository="title-busy",tag="latest")) | |
#import as data byte inmemory | |
#c.import_image_from_data(data=) | |
#import from url as tarfile | |
#c.import_image_from_url(url=) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment