def _ensure_net_exists(self): """ Ensures that the network image and docker network exists. """ try: self.get_client().images.get(config.net_container_name()) self.get_client().networks.get(config.net_name()) except ImageNotFound as _: console.info( f'network image [bold]{config.net_container_name()}[/] does not exist, quickly building it' ) _, logs = self.get_client().images.build( path=str(NETWORK_CONTAINER_PATH), pull=True, tag=config.net_container_name(), rm=True, forcerm=True) for log in logs: console.debug(log) console.info( f'network container [bold]{config.net_container_name()}[/] built' ) self._ensure_net_exists() except NotFound as _: console.info( f'docker network [bold]{config.net_name()}[/] does not exist, creating it' ) self.get_client().networks.create(name=config.net_name(), check_duplicate=True) self._ensure_net_exists()
def check(): """ Check plans and Docker environment """ # plans loader = Loader() console.info(f'loaded [bold]{len(loader.valid_plans())}[/] valid plans') # docker try: client = docker.from_env() info = client.info() console.info(f'docker server version: [bold]{info.get("ServerVersion")}[/]') # network container client.images.get(config.net_container_name()) console.info(f'network image [bold]\'{config.net_container_name()}\'[/] exists') # dwn docker network client.networks.get(config.net_name()) console.info(f'docker network [bold]\'{config.net_name()}\'[/] exists') except ImageNotFound as _: console.warn(f'network image [bold]\'{config.net_container_name()}\'[/] does not exist. ' f'build it with the [bold]\'network build-container\'[/] command') except NotFound as _: console.warn(f'docker network [bold]\'{config.net_name()}\'[/] not found.' f'use [bold]\'docker network create {config.net_name()}\'[/] to should solve that.') except DockerException as e: console.error(f'docker client error type [dim]{type(e)}[/]: [bold]{e}[/]') console.info('[green]everything seems to be ok to use dwn![/]')
def run(self) -> models.containers.Container: """ Run the containers for a plan """ self._ensure_net_exists() self._ensure_image_exists() # inline dockerfiles console.debug( f'starting service container [bold]{self.get_container_name()}[/]' f' for plan [bold]{self.plan.name}[/]') opts = self.plan.run_options() opts['name'] = self.get_container_name() container = self.get_client(). \ containers.run(self.plan.image_version(), network=config.net_name(), **opts) if not self.plan.exposed_ports: return container for port_map in self.plan.exposed_ports: inside, outside = port_map[0], port_map[1] self.run_net(outside, inside) return container
def run_net(self, outside: int, inside: int): """ Run a network container for a plan """ self._ensure_net_exists() console.debug( f'starting network proxy [green]{inside}[/]<-{self.get_container_name()}<-' f'[red]{outside}[/] for plan [bold]{self.plan.name}[/]') self.get_client(). \ containers.run(config.net_container_name(), detach=True, environment={ 'REMOTE_HOST': self.get_container_name(), 'REMOTE_PORT': inside, 'LOCAL_PORT': outside, }, stderr=True, stdout=True, remove=True, network=config.net_name(), ports={outside: outside}, name=self.get_net_container_name_with_ports(outside, inside))