def test_parse_int_port(self): PORT = 5000 RESULT = self.make_port_spec('', '', '5000', 'tcp') r = parse_port_spec(PORT) self.assertEqual(r, RESULT)
def create_container(client, image_id, command, environment=None, volumes=None, ports=None, name=None, stdin_open=False, tty=False, detach=True, log=None): """ :return: DockerContainer """ log = log if log else default_logger log_msg = 'create new container: environment: {0} volumes: {1} ' \ 'ports: {2}'.format(environment, volumes, ports) log.debug(log_msg) if volumes is None: volumes = [] if environment is None: environment = {} if ports is None: ports = [] cmd = _docker_run_command(image_id, command, environment, volumes, ports, name, stdin_open, tty, detach) log.debug('debug docker command: ' + cmd) volumes_specs = [parse_volume_spec(v) for v in volumes] binds = convert_volumes_specs_to_binds(volumes_specs) internal_volumes = [v.container for v in volumes_specs] ports_specs = [parse_port_spec(p) for p in ports] port_binds = convert_ports_specs_to_port_binds(ports_specs) internal_ports = [(p.port, p.protocol) for p in ports_specs] kwargs = dict(image=image_id, command=command, environment=environment, volumes=internal_volumes, name=name, ports=internal_ports, stdin_open=stdin_open, tty=tty, detach=detach) try: container = client.create_container(**kwargs) except docker.errors.APIError as e: if e.response.status_code == 404 and e.explanation and \ 'No such image' in str(e.explanation): log_msg = 'Container image not exists. Try pulling ... ' \ '(docker pull {0})'.format(image_id) log.warning(log_msg) output = client.pull(image_id, stream=True) stream_output(output, sys.stderr) container = client.create_container(**kwargs) else: raise id = container['Id'] warnings = container['Warnings'] log.info('new container created: id={0}'.format(id)) if warnings: log.warning('new container warnings: {0}, id={1}'.format(warnings, id)) options = DockerContainerOptions(binds=binds, ports_binds=port_binds) return DockerContainer(client, container['Id'], options)
def test_parse_port_spec_with_empty_port_raise_error(self): PORT = '' with self.assertRaises(ValueError): parse_port_spec(PORT)
def test_parse_port_spec_with_host_ip_and_empty_port(self): PORT = '127.0.0.1::5000' RESULT = self.make_port_spec('127.0.0.1', '', '5000', 'tcp') r = parse_port_spec(PORT) self.assertEqual(r, RESULT)
def test_parse_port_spec_with_host_port_and_ip(self): PORT = '127.0.0.1:4999:5000' RESULT = self.make_port_spec('127.0.0.1', '4999', '5000', 'tcp') r = parse_port_spec(PORT) self.assertEqual(r, RESULT)
def test_parse_port_spec_with_host_port(self): PORT = '4999:5000' RESULT = self.make_port_spec('', '4999', '5000', 'tcp') r = parse_port_spec(PORT) self.assertEqual(r, RESULT)
def create_container(client, image_id, command, environment=None, volumes=None, ports=None, name=None, stdin_open=False, tty=False, detach=True, log=None): """ :return: DockerContainer """ log = log if log else default_logger log_msg = 'create new container: environment: {0} volumes: {1} ' \ 'ports: {2}'.format(environment, volumes, ports) log.debug(log_msg) if volumes is None: volumes = [] if environment is None: environment = {} if ports is None: ports = [] cmd = _docker_run_command(image_id, command, environment, volumes, ports, name, stdin_open, tty, detach) log.debug('debug docker command: ' + cmd) volumes_specs = [parse_volume_spec(v) for v in volumes] binds = convert_volumes_specs_to_binds(volumes_specs) internal_volumes = [v.container for v in volumes_specs] ports_specs = [parse_port_spec(p) for p in ports] port_binds = convert_ports_specs_to_port_binds(ports_specs) internal_ports = [(p.port, p.protocol) for p in ports_specs] kwargs = dict( image=image_id, command=command, environment=environment, volumes=internal_volumes, name=name, ports=internal_ports, stdin_open=stdin_open, tty=tty, detach=detach ) try: container = client.create_container(**kwargs) except docker.errors.APIError as e: if e.response.status_code == 404 and e.explanation and \ 'No such image' in str(e.explanation): log_msg = 'Container image not exists. Try pulling ... ' \ '(docker pull {0})'.format(image_id) log.warning(log_msg) output = client.pull(image_id, stream=True) stream_output(output, sys.stderr) container = client.create_container(**kwargs) else: raise id = container['Id'] warnings = container['Warnings'] log.info('new container created: id={0}'.format(id)) if warnings: log.warning('new container warnings: {0}, id={1}'.format(warnings, id)) options = DockerContainerOptions(binds=binds, ports_binds=port_binds) return DockerContainer(client, container['Id'], options)