Last active
August 29, 2015 14:13
-
-
Save martinrusev/ba81e5f668b2991ed286 to your computer and use it in GitHub Desktop.
Docker builder
This file contains hidden or 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
| from datetime import datetime | |
| from docker import Client | |
| from djangoapp.instance.models import Instance | |
| class AmonInstance(object): | |
| def __init__(self, account=None): | |
| self.client = Client(base_url='unix://var/run/docker.sock', version='1.15') | |
| self.account_id = 1 | |
| self.instance = Instance.objects.get(account_id=self.account_id) | |
| def reload_container(self): | |
| try: | |
| self.stop_container() | |
| except: | |
| pass | |
| self.start_container() | |
| def start_container(self): | |
| container = self.client.create_container('martinrusev/djangoapp', | |
| name='djangoapp', | |
| ports=[8000], | |
| volumes=['/djangoapp/'] | |
| ) | |
| self.client.start(container, | |
| port_bindings={8000: 8000}, | |
| binds={ | |
| '/home/djangoapp': | |
| { | |
| 'bind': '/djangoapp', | |
| 'ro': True | |
| }, | |
| '/var/log/djangoapp': { | |
| 'bind': '/var/log', | |
| } | |
| }) | |
| container_id = container['Id'] | |
| Instance.objects.filter(account_id=self.account_id).update(container_id=container_id, | |
| modified_date=datetime.now()) | |
| def stop_container(self): | |
| self.client.stop(self.instance.container_id) | |
| self.client.remove_container(self.instance.container_id) | |
| Instance.objects.filter(account=self.account).update(container_id=None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment