Skip to content

Instantly share code, notes, and snippets.

@disconnect3d
Last active September 29, 2016 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disconnect3d/549571c3a470645964707c087ebb3bd9 to your computer and use it in GitHub Desktop.
Save disconnect3d/549571c3a470645964707c087ebb3bd9 to your computer and use it in GitHub Desktop.
Simple server listing docker containers with exposed ports as links on all network interfaces. Works with Python 3; `pip install flask` is required.
#!/usr/bin/env python3
# flask is required - install with `pip install flask`
import sys
import subprocess
from flask import Flask
if len(sys.argv) < 2:
print("Usage: %s <public ip>" % sys.argv[0])
sys.exit(0)
public_ip = sys.argv[1]
app = Flask(__name__)
def get_containers_ports():
containers_data = {}
output = subprocess.check_output(['docker', 'ps', '--format', '{{.Image}} {{.Ports}}'])
*lines, _empty = output.split(b'\n')
for line in lines:
print(repr(line))
image_name, ports = line.split(b' ')
# ports example: 0.0.0.0:6666->8080/tcp
# getting exposed port (here 6666)
exposed_port = int(ports[ports.index(b':')+1:ports.index(b'-')])
containers_data[image_name] = exposed_port
return containers_data
# Maps img name to additional url path (after /)
additional_url = {
'intercept_me': 'intercept-me'
}
@app.route('/')
def main():
html = ''
for img_name, port in get_containers_ports().items():
name = img_name.decode('ascii')
additional = additional_url.get(name, '')
addr = 'http://{ip}:{port}/{additional}'.format(ip=public_ip, port=port, additional=additional)
html += '<a href="{addr}">{name} - {addr} </a><br/>'.format(addr=addr, name=name)
return html
app.run('0.0.0.0', 80)#, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment