Skip to content

Commit 0c4ff4f

Browse files
acdoussanAdam Doussan
andauthored
Fix volumes missing in generated compose file (#41)
* fix volume export based off of #17 (comment) * remove unneeded space * export volumes in addition to networks * fix syntax error * actuall fix syntax errors Co-authored-by: Adam Doussan <[email protected]>
1 parent e19c465 commit 0c4ff4f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

autocompose.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def list_container_names():
1212
def main():
1313
parser = argparse.ArgumentParser(description='Generate docker-compose yaml definition from running container.')
1414
parser.add_argument('-a', '--all', action='store_true', help='Include all active containers')
15-
parser.add_argument('-v', '--version', type=int, default=3, help='Compose file version (1 or 3)')
15+
parser.add_argument('-v', '--version', type=int, default=3, help='Compose file version (1 or 3)')
1616
parser.add_argument('cnames', nargs='*', type=str, help='The name of the container to process.')
1717
args = parser.parse_args()
1818

@@ -22,21 +22,23 @@ def main():
2222

2323
struct = {}
2424
networks = {}
25+
volumes = {}
2526
for cname in container_names:
26-
cfile, c_networks = generate(cname)
27+
cfile, c_networks, c_volumes = generate(cname)
2728

2829
struct.update(cfile)
2930
networks.update(c_networks)
31+
volumes.update(c_volumes)
3032

31-
render(struct, args, networks)
33+
render(struct, args, networks, volumes)
3234

3335

34-
def render(struct, args, networks):
36+
def render(struct, args, networks, volumes):
3537
# Render yaml file
3638
if args.version == 1:
3739
pyaml.p(OrderedDict(struct))
3840
else:
39-
pyaml.p(OrderedDict({'version': '"3"', 'services': struct, 'networks': networks}))
41+
pyaml.p(OrderedDict({'version': '"3"', 'services': struct, 'networks': networks, 'volumes': volumes}))
4042

4143

4244
def is_date_or_time(s: str):
@@ -121,6 +123,11 @@ def generate(cname):
121123
networks[network.attrs['Name']] = {'external': (not network.attrs['Internal']),
122124
'name': network.attrs['Name']}
123125

126+
volumes = {}
127+
for volume in values['volumes']:
128+
volume_name = volume.split(':')[0]
129+
volumes[volume_name] = {'external': True}
130+
124131
# Check for command and add it if present.
125132
if cattrs['Config']['Cmd'] is not None:
126133
values['command'] = cattrs['Config']['Cmd']
@@ -150,7 +157,7 @@ def generate(cname):
150157
if (value != None) and (value != "") and (value != []) and (value != 'null') and (value != {}) and (value != "default") and (value != 0) and (value != ",") and (value != "no"):
151158
ct[key] = value
152159

153-
return cfile, networks
160+
return cfile, networks, volumes
154161

155162

156163
if __name__ == "__main__":

0 commit comments

Comments
 (0)