Create a gist now

Instantly share code, notes, and snippets.

@vieux /flatten.py
Last active Feb 3, 2017

to flatten dhrp/sshd : sudo python flatten.py 2bbfe079a94259b229ae66962d9d06b97fcdce7a5449775ef738bb619ff8ce73 it'll produce a 42422bbfe079a94259b229ae66962d9d06b97fcdce7a5449775ef738bb619ff8ce73 image
import os
from StringIO import StringIO
import json
import sys
import subprocess
import shutil
def getParent(rootid, id):
print "--> ", id
with open("/var/lib/docker/graph/"+id+"/json") as data_file:
data = json.load(data_file)
if "parent" in data:
parent = data["parent"]
getParent(rootid, parent)
print "[cp -r /var/lib/docker/graph/"+id+"/* /var/lib/docker/graph/4242"+rootid+"/]"
os.system("cp -r /var/lib/docker/graph/"+id+"/* /var/lib/docker/graph/4242"+rootid+"/")
rootid = sys.argv[-1]
shutil.rmtree(path="/var/lib/docker/graph/4242"+rootid,ignore_errors=True)
print "[mkdir /var/lib/docker/graph/4242"+rootid+"]"
os.mkdir("/var/lib/docker/graph/4242"+rootid)
getParent(rootid, rootid)
data = []
with open("/var/lib/docker/graph/4242"+rootid+"/json", "r") as data_file:
data = json.load(data_file)
data["id"] = "4242" + data["id"]
del data["parent"]
del data["Size"]
with open("/var/lib/docker/graph/4242"+rootid+"/json", "w") as data_file:
json.dump(data, data_file)
shutil.rmtree(path="/var/lib/docker/graph/4242"+rootid+"/layersize",ignore_errors=True)
print "==> 4242"+rootid
@brycereynolds

@vieux I'm trying this script out but I'm not entirely sure what to do once the new graph directory is created. It's executing properly and I'm getting a new imageId back (4242fe27c813ed320e812a98edeec1e3727d0bacb3ac70b48c529b5a98fd376e1395).

I tried to run this image but it doesn't seem to find it locally via the imageId. I'm pretty new to working at this level so any help is much appreciated!

@BertrandBordage

Same issue as @brycereynolds here.

@bryyyon
bryyyon commented Aug 13, 2014

@brycereynolds @BertrandBordage: The Python script appears to flatten the image into a new ID matching the old, but with the prefix 4242. So if you flatten the long ID "ABCXXXX" (/var/lib/docker/graph/ABCXXXX/) this script creates: /var/lib/docker/graph/4242ABCXXXX/

While using this I had to remove the 4242 prefix on the JSON "id" field within the /var/lib/docker/graph/4242ABCXXXX/json file, then rename /var/lib/docker/graph/4242ABCXXXX/ back to /var/lib/docker/graph/ABCXXXX/. I was then able to use the flattened image.

Hope this helps. I'm not at all certain if this is a valid way of handling this with respect to Docker, but it's working fine for me so far.

@ChrisGClark

thanks for sharing, but doesn't work on mac as images are not stored in /var/lib/docker/graph

C02SC2J3FVH8:dockerFlatten cclark$ sudo python flatten.py 270795070413

[mkdir /var/lib/docker/graph/4242270795070413]
Traceback (most recent call last):
File "flatten.py", line 23, in
os.mkdir("/var/lib/docker/graph/4242"+rootid)
OSError: [Errno 2] No such file or directory: '/var/lib/docker/graph/4242270795070413'

I tried creating directory /var/lib/docker/graph, but then fails with:

sudo python flatten.py 270795070413
[mkdir /var/lib/docker/graph/4242270795070413]
--> 270795070413
Traceback (most recent call last):
File "flatten.py", line 24, in
getParent(rootid, rootid)
File "flatten.py", line 11, in getParent
with open("/var/lib/docker/graph/"+id+"/json") as data_file:
IOError: [Errno 2] No such file or directory: '/var/lib/docker/graph/270795070413/json'

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