Browse Source

Initial version

tags/v0.1.1
Chris Smith 7 years ago
commit
bfa5ec7b74
3 changed files with 138 additions and 0 deletions
  1. 25
    0
      LICENCE.md
  2. 24
    0
      README.md
  3. 89
    0
      docker-rerun

+ 25
- 0
LICENCE.md View File

@@ -0,0 +1,25 @@
1
+The MIT License (MIT)
2
+=====================
3
+
4
+Copyright © 2016 Chris Smith
5
+
6
+Permission is hereby granted, free of charge, to any person
7
+obtaining a copy of this software and associated documentation
8
+files (the “Software”), to deal in the Software without
9
+restriction, including without limitation the rights to use,
10
+copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+copies of the Software, and to permit persons to whom the
12
+Software is furnished to do so, subject to the following
13
+conditions:
14
+
15
+The above copyright notice and this permission notice shall be
16
+included in all copies or substantial portions of the Software.
17
+
18
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
19
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
+OTHER DEALINGS IN THE SOFTWARE.

+ 24
- 0
README.md View File

@@ -0,0 +1,24 @@
1
+docker-rerun
2
+===============================================================================
3
+
4
+`docker-rerun` is a small utility script that makes it easy to re-run docker
5
+containers using the same arguments you used previously.
6
+
7
+Want to update to a newer image, or add a missing port publication?
8
+docker-rerun's got you covered.
9
+
10
+## How to use
11
+
12
+In the most basic usage, you pass in a container name and it will be
13
+stopped, deleted and recreated:
14
+
15
+    $ ./docker-rerun apache
16
+
17
+To check what exactly is going to be performed beforehand, use the --dry-run
18
+option:
19
+
20
+    $ ./docker-rerun --dry-run apache
21
+    docker stop apache
22
+    docker rm apache
23
+    docker run --name=apache -p=80:80/tcp --restart=always apache:latest
24
+

+ 89
- 0
docker-rerun View File

@@ -0,0 +1,89 @@
1
+#!/usr/bin/python3
2
+
3
+import argparse
4
+import inspect
5
+import json
6
+import subprocess
7
+import sys
8
+
9
+
10
+def inspect_container(container):
11
+    output = subprocess.check_output(['docker', 'inspect', '--type=container', container])
12
+    return json.loads(output.decode('utf-8'))[0]
13
+
14
+
15
+def handle_binds(container, config):
16
+    if container['HostConfig']['Binds']:
17
+        config['args'].extend(['--volume=%s' % bind for bind in container['HostConfig']['Binds']])
18
+
19
+
20
+def handle_image(container, config):
21
+    config['image'] = container['Config']['Image']
22
+
23
+
24
+def handle_name(container, config):
25
+    # Trim the leading / off the name. They're equivalent from docker's point of view, but having
26
+    # the plain name looks nicer from a human point of view.
27
+    config['args'].append('--name=%s' % container['Name'][1:])
28
+
29
+
30
+def handle_network_mode(container, config):
31
+    network = container['HostConfig']['NetworkMode']
32
+    if network != 'default':
33
+        config['args'].append('--net=%s' % network) 
34
+
35
+
36
+def handle_ports(container, config):
37
+    ports = container['HostConfig']['PortBindings']
38
+    if ports:
39
+        for port, bindings in ports.items():
40
+            for binding in bindings:
41
+                if binding['HostIp']: 
42
+                    config['args'].append('-p=%s:%s:%s' % (binding['HostIp'], binding['HostPort'], port))
43
+                elif binding['HostPort']:
44
+                    config['args'].append('-p=%s:%s' % (binding['HostPort'], port))                
45
+                else:
46
+                    config['args'].append('-p=%s' % port)
47
+
48
+
49
+def handle_restart(container, config):
50
+    policy = container['HostConfig']['RestartPolicy']
51
+    if policy and policy['Name'] != 'no':
52
+        arg = '--restart=%s' % policy['Name']
53
+        if policy['MaximumRetryCount'] > 0:
54
+            arg += ':%s' % policy['MaximumRetryCount']
55
+        config['args'].append(arg)
56
+
57
+
58
+def handle_volumes_from(container, config):
59
+    if container['HostConfig']['VolumesFrom']:
60
+        config['args'].extend(['--volumes-from=%s' % cont for cont in container['HostConfig']['VolumesFrom']])
61
+
62
+
63
+def functions():
64
+    return [m for m in inspect.getmembers(sys.modules[__name__]) if inspect.isfunction(m[1])]
65
+
66
+
67
+def handlers():
68
+    return [func for (name, func) in functions() if name.startswith('handle_')]
69
+    
70
+
71
+def main():
72
+    parser = argparse.ArgumentParser(description='Reruns docker containers with different parameters.')
73
+    parser.add_argument('container', type=str, help='The container to rerun')
74
+    parser.add_argument('-d', '--dry-run', action='store_true', help='Don\'t actually re-run the container, just print what would happen.')
75
+    args = parser.parse_args()
76
+    container = inspect_container(args.container)
77
+
78
+    docker_config = {'args': ['-d'], 'image': ''}
79
+    for handler in handlers():
80
+        handler(container, docker_config)
81
+
82
+    if args.dry_run:
83
+        print('docker stop %s' %  args.container)
84
+        print('docker rm %s' % args.container)
85
+        print('docker run %s %s' % (' '.join(docker_config['args']), docker_config['image']))
86
+
87
+
88
+if __name__ == "__main__":
89
+    main()

Loading…
Cancel
Save