def get_client(): """Return the client or initialize it.""" if 'client' not in __st__: from docker import client __st__['client'] = client.from_env() return __st__['client']
def __init__( self, check, env, base_package=None, config=None, env_vars=None, metadata=None, agent_build=None, api_key=None, python_version=DEFAULT_PYTHON_VERSION, ): self.docker_client = docker_client.from_env() self.check = check self.env = env self.env_vars = env_vars or {} self.base_package = base_package self.config = config or {} self.metadata = metadata or {} self.agent_build = agent_build self.api_key = api_key or FAKE_API_KEY self.python_version = python_version or DEFAULT_PYTHON_VERSION self._agent_version = self.metadata.get('agent_version') self.container_name = 'dd_{}_{}'.format(self.check, self.env) self.config_dir = locate_config_dir(check, env) self.config_file = locate_config_file(check, env) self.config_file_name = config_file_name(self.check) if self.agent_build and 'py' not in self.agent_build: self.agent_build = '{}-py{}'.format(self.agent_build, self.python_version) if self.agent_build and self.metadata.get('use_jmx', False): self.agent_build = '{}-jmx'.format(self.agent_build)
def get_client(): """Return the client or initialize it.""" if 'client' not in __st__: from docker import client __st__['client'] = client.from_env() return __st__['client']
def setup(self): docker_cfg = self.container.config.get('DOCKER') if docker_cfg: self.client = client.Client(**docker_cfg) else: self.client = client.from_env() self.client.info() self.event_handlers = []
def start_proxy(self, outpost: Outpost) -> Container: """Start proxy container based on outpost created""" client: DockerClient = from_env() container = client.containers.run( image=self.get_container_image("ghcr.io/goauthentik/dev-proxy"), detach=True, network_mode="host", environment={ "AUTHENTIK_HOST": self.live_server_url, "AUTHENTIK_TOKEN": outpost.token.key, }, ) return container
def __init__(self, container_id, pretty_print=True): from docker import client, errors from docker_replay.parser import ConfigParser self.pretty_print = pretty_print c = client.from_env(version='auto') try: inspect = c.api.inspect_container(container_id) self.parser = ConfigParser(inspect) except errors.NotFound: print('no such container: %s' % container_id) sys.exit(1)
def __init__(self, container_id, pretty_print=True): from docker import client, errors from docker_replay.parser import ConfigParser self.pretty_print = pretty_print c = client.from_env(version='auto') try: inspect = c.api.inspect_container(container_id) self.parser = ConfigParser(inspect) except errors.NotFound: print('no such container: %s' % container_id) sys.exit(1)
def start_proxy(self, outpost: Outpost) -> Container: """Start proxy container based on outpost created""" client: DockerClient = from_env() container = client.containers.run( image=f"beryju/authentik-proxy:{__version__}", detach=True, network_mode="host", auto_remove=True, environment={ "AUTHENTIK_HOST": self.live_server_url, "AUTHENTIK_TOKEN": outpost.token.key, }, ) return container
def runContainer(): mydocker = client.from_env() container = mydocker.containers.run('registry.cn-hangzhou.aliyuncs.com/edge_node/eureka:latest', detach=True,name="eureka",ports={"10000/tcp" : [10000]} ,network_mode="zhuanyong") container_list = mydocker.containers.list(); container = mydocker.containers.get("eureka") if container in container_list: if container.status == "running": print("create success") print(container.name+" "+container.id+" "+container.short_id+" "+container.status) else: print("create failure")
def run_serialized_container_command(image, command, volumes): try: from docker.client import from_env except ImportError: warnings.warn( "Cannot load docker environment without the python package docker. Ensure that dagster[docker] or the python package docker is installed." ) raise client = from_env() client.containers.run(image, command=command, detach=False, volumes=volumes, auto_remove=True)
class JobExecutor: docker_client = dockerClient.from_env() producer = KafkaProducer( **kafka_params, value_serializer=lambda v: json.dumps(v).encode('utf-8')) def startContainer(self, feedName): container = self.docker_client.containers.get(feedName) container.start() def publishUrl(self, feedName, url): item = {"url": url, "type": feedName} self.producer.send(topic="worker-queue".format(name=feedName), value=item, key=bytes(url, 'utf-8'))
def run_serialized_container_command(image, command, volumes): try: from docker.client import from_env client = from_env() byte_stream = client.containers.run( image, command=command, volumes=volumes, auto_remove=False, ) # Decode to string, split by newlines, and filter for any empty strings lines = list(filter(None, byte_stream.decode('utf-8').split("\n"))) return lines except ImportError: warnings.warn( "Cannot load docker environment without the python package docker. Ensure that dagster[docker] or the python package docker is installed." ) raise
def run_detached_container_command(image, command, volumes, output_file): try: from docker.client import from_env client = from_env() # This is currently blocking. Has to be updated to run in detached mode. The problem # is knowing when the file is ready to be read from. client.containers.run(image, command=command, volumes=volumes, auto_remove=False) for message in ipc_read_event_stream(output_file): yield message except ImportError: warnings.warn( "Cannot load docker environment without the python package docker. Ensure that dagster[docker] or the python package docker is installed." ) raise
def setUpClass(): """Clean containers and services directory""" from docker import client from docker.errors import NotFound cts = client.from_env().containers.list(all=True) for ct in cts: try: ct.stop() ct.remove(v=True, force=True) except NotFound: pass cli = CliTest() for service in ['db', 'databases', 'emails', 'php']: rmtree(BASE_DIR + '/static/services/' + service, ignore_errors=True) exec_cmd(cli.cmd_base + ['services-add', 'php']) exec_cmd(cli.cmd_base + ['services-add', 'emails'])
def run_in_container(container_name, command, check=True, interactive=False): """Runs a command in the given container. This is useful for WaitFor conditions in `docker_run` :param container_name: The name of the container :param command: command line to run in the container :param check: Whether or not to raise an exception on non-zero exit codes. Default: ``True`` :param interactive: Attach to stdin. Default: ``False`` """ client = docker_client.from_env() container = client.containers.get(container_name) if not container: raise Exception("Could not find container {}".format(container_name)) try: command_line = ' '.join(command) if isinstance(command, list) else command except TypeError as e: raise e result = container.exec_run(command_line, stdin=interactive) if check and result.exit_code != 0: raise Exception(result.output) return result
class DockerClient(object): _client = client.from_env() def stop_all_running_containers(self): for container in self._client.containers.list(): container.stop() def list_all_images(self): images = list() for image in self._client.images.list(all=True): images.extend(image.tags) return images def get_image(self, name): try: self._client.images.get(name) except errors.ImageNotFound: pass except errors.APIError: pass def remove_image(self, image_id): self._client.images.remove(image_id, force=True)
def __init__(self, settings): self.__settings = settings self.__sc = SystemCalls(self._name(), self.__settings) self.__dc = from_env(environment=self.__sc.get_docker_env()).api
import io import os import sys import unittest import docker_clean from contextlib import redirect_stdout from click.testing import CliRunner from docker.errors import NotFound from docker import client DOCKER_CLIENT = client.from_env() BASE_DIR = os.path.abspath(os.path.dirname(__file__)) sys.path.insert(0, BASE_DIR + '/../') class DockerCleanTest(unittest.TestCase): def test_remove_containers_empty(self): clean_cts() fake_file = io.StringIO() with redirect_stdout(fake_file): docker_clean.remove_containers(True) res = fake_file.getvalue() self.assertEqual('No exited container to remove', res.strip()) def test_remove_containers_pretend(self): clean_cts() clean_images() DOCKER_CLIENT.containers.run('hello-world', remove=False) num_containers = len(DOCKER_CLIENT.containers.list(all=True)) fake_file = io.StringIO()
#!/usr/bin/env python # coding: utf-8 """ Docker Clean command. Clean unused containers, images, volumes and networks. That saves a lot of space ... """ import sys import click import humanfriendly from docker import client DOCKER_CLIENT = client.from_env(timeout=360) @click.command(help="""Clean Docker containers, images, volumes and networks that are not in use""", name="docker-clean") @click.version_option('1.0.2') @click.option('--force', '-f', help="Do it", is_flag=True) @click.option('--containers/--no-containers', '-c/', help="Remove containers", is_flag=True, default=True) @click.option('--images/--no-images', '-i/', help="Remove images", is_flag=True, default=True) @click.option('--volumes/--no-volumes', '-V/', help="Remove volumes", is_flag=True, default=True) @click.option('--networks/--no-networks', '-n/', help="Remove networks", is_flag=True, default=True) def clean( force: bool = False, containers: bool = True, images: bool = True, volumes: bool = True, networks: bool = True): """See command help."""
def get_container_ip(container_id_or_name): """Get a Docker container's IP address from its id or name.""" client = docker_client.from_env() inspect = client.api.inspect_container(container_id_or_name) return inspect["NetworkSettings"]["Networks"]["compose_default"][ "IPAddress"]
def init_docker_client() -> client: return client.from_env()
from docker import client import os import sys mydocker = client.from_env() net = mydocker.networks.get("host") print(net.containers)
def __init__(self): self.docker_client = client.from_env() self._init_screen() self._init_widgets()