def test_container_running(self): """Make sure, even in another directory, the venv base dir is correct.""" stop_remove_container('pytest') docker_actions.get_client().containers.run( 'edyan/adminer:latest', detach=True, name='pytest') self.assertTrue(docker_actions.container_running('pytest'))
def test_create_network(self): """ Create a network then a container, attache one to the other And verify everything is OK """ stop_remove_container('pytest') docker_actions.get_client().containers.run( 'edyan/adminer:latest', detach=True, name='pytest') self.assertTrue(docker_actions.container_running('pytest')) self.assertFalse(docker_actions._container_in_network('pytest', 'pytest')) self.assertFalse(docker_actions.network_exists('nw_pytest')) network_created = docker_actions.create_network('nw_pytest') self.assertNotEqual(False, network_created) self.assertIs(str, type(network_created)) self.assertFalse(docker_actions.create_network('nw_pytest')) self.assertTrue(docker_actions.add_container_to_network('pytest', 'nw_pytest')) self.assertFalse(docker_actions.add_container_to_network('pytest', 'nw_pytest')) self.assertTrue(docker_actions._container_in_network('pytest', 'nw_pytest')) stop_remove_container('pytest') remove_network('nw_pytest')
def start(project_name: str): """For Windows we need to do a few manipulations to be able to access our dev environment from the host ... Found here: https://github.com/docker/for-win/issues/221#issuecomment-270576243""" if os.name not in ['nt']: return docker_actions.get_client().containers.run( 'justincormack/nsenter1', remove=True, tty=True, privileged=True, network_mode='none', pid_mode='host', command='bin/sh -c "iptables -A FORWARD -j ACCEPT"') subnet = docker_actions.get_subnet(project_name) switch_ip = docker_actions.get_switch_ip() msg = 'We need to create a route for the network {} via {} to work...' secho(msg.format(subnet, switch_ip), fg='yellow', nl=False) subprocess.call( ['route', 'add', subnet, 'MASK', '255.255.255.0', switch_ip]) echo()
def test_guess_shell_bash(self): stop_remove_container('pytest') docker_actions.get_client().containers.run( 'edyan/php:7.2', detach=True, name='pytest') shell = docker_actions.guess_shell('pytest') self.assertEqual('/bin/bash', shell) stop_remove_container('pytest')
def stop_remove_container(ct_name: str): try: ct = docker_actions.get_client().containers.get(ct_name) ct.stop() ct.remove() except NotFound: pass
def __init__(self, http_port: int = 80, https_port: int = 443, ct_name: str = 'proxy_stakkr'): """Set the right values to start the proxy.""" self.ports = {'http': http_port, 'https': https_port} self.ct_name = ct_name self.docker_client = docker.get_client()
def run_dns(project_name: str): """Either add the DNS to the network if started or start it""" network = project_name + '_stakkr' # Started ? Only attach it to the current network if docker_actions.container_running(CT_NAME) is True: docker_actions.add_container_to_network(CT_NAME, network) return # Not started ? Create it and add it to the network try: volumes = ['/var/run/docker.sock:/tmp/docker.sock', '/etc/resolv.conf:/tmp/resolv.conf'] docker_actions.get_client().containers.run( 'mgood/resolvable', remove=True, detach=True, network=network, hostname='docker-dns', name=CT_NAME, volumes=volumes) except DockerException as error: click.echo(click.style('[ERROR]', fg='red') + " Can't start the DNS ...") click.echo('Error was -> {}'.format(error)) sys.exit(1)
def manage_dns(project_name: str, action: str): """Control the start or stop of DNS forwarder""" dns_started = docker_actions.container_running(CT_NAME) if dns_started is True and action == 'stop': dns = docker_actions.get_client().containers.get('docker_dns') return dns.stop() elif action == 'start': run_dns(project_name) return click.echo(click.style('[ERROR]', fg='red') + " DNS already stopped") sys.exit(1)
def remove_network(network_name: str): try: network = docker_actions.get_client().networks.get(network_name) network.remove() except NotFound: pass
def __init__(self, port: int = 80, proxy_name: str = 'proxy_stakkr'): """Set the right values to start the proxy.""" self.port = port self.proxy_name = proxy_name self.docker_client = docker.get_client()