Last active
August 29, 2015 14:24
-
-
Save pdxjohnny/d1cbbe39fca6ad71fe0a to your computer and use it in GitHub Desktop.
Make docker listen on lan
This file contains 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
""" | |
Makes docker listen on lan | |
tcp://192.168.1.32:2375 | |
http://192.168.1.32:2375 | |
""" | |
import json | |
import copy | |
import fcntl | |
import socket | |
import string | |
import random | |
import struct | |
import argparse | |
import StringIO | |
import collections | |
import ConfigParser | |
__description__ = """Modifies docker config to listen on another address \ | |
or port""" | |
def interface_ip(ifname): | |
try: | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
return socket.inet_ntoa(fcntl.ioctl( | |
s.fileno(), | |
0x8915, # SIOCGIFADDR | |
struct.pack('256s', ifname[:15]) | |
)[20:24]) | |
except: | |
return False | |
DEFUALT_FILE = "/etc/default/docker" | |
DEFUALT_PORT = 2375 | |
DEFUALT_ADDR = interface_ip("eth0") or \ | |
interface_ip("wlan0") or \ | |
socket.gethostbyname(socket.gethostname()) | |
def arg_setup(): | |
arg_parser = argparse.ArgumentParser(description=__description__) | |
arg_parser.add_argument("--address", "-a", type=str, \ | |
help="Address to host on", default=DEFUALT_ADDR) | |
arg_parser.add_argument("--port", "-p", type=int, \ | |
help="Port to host on", default=DEFUALT_PORT) | |
arg_parser.add_argument("--modfile", "-f", type=str, \ | |
help="File to change", default=DEFUALT_FILE) | |
initial = vars(arg_parser.parse_args()) | |
args = {} | |
for arg in initial: | |
if initial[arg] is not None: | |
args[arg] = initial[arg] | |
return args | |
def run(port, address, modfile): | |
listen_args = "-H tcp://{address}:{port}".format(port=port, address=address) | |
settings_file = open(modfile, "ab") | |
option = "DOCKER_OPTS=\"{listen_args} -H unix:///var/run/docker.sock\"\n".format(listen_args=listen_args) | |
settings_file.write(option) | |
settings_file.close() | |
settings_file = open(modfile, "rb") | |
for line in settings_file: | |
print line[:-1] | |
settings_file.close() | |
return | |
def main(): | |
args = arg_setup() | |
run(**args) | |
return 0 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment