def run_container(client, image, algorithmia_api_key, algorithmia_api_address, mode, local_aws=False, networking=False): raw_args = {} if isinstance(image, Image): image = image.id raw_args['image'] = image raw_args['version'] = client.containers.client.api._version home_dir = str(Path.home()) aws_dir = os.path.join(home_dir, ".aws") if networking and local_aws: raw_args['ports'] = {80: 80} container_args = _create_container_args(raw_args) container_args['host_config'] = client.api.create_host_config( port_bindings={80: ("0.0.0.0", 80)}, binds={aws_dir: { 'bind': "/root/.aws", "mode": "ro" }}) container_args['volumes'] = ["/root/.aws"] elif networking: raw_args['ports'] = {80: 80} container_args = _create_container_args(raw_args) container_args['host_config'] = client.api.create_host_config( port_bindings={80: ("0.0.0.0", 80)}) elif local_aws: container_args = _create_container_args(raw_args) container_args['host_config'] = client.api.create_host_config( binds={aws_dir: { 'bind': "/root/.aws", "mode": "ro" }}) container_args['volumes'] = ["/root/.aws"] else: container_args = _create_container_args(raw_args) container_args['detach'] = True container_args['environment'] = {} container_args['environment']['ALGORITHMIA_API_KEY'] = algorithmia_api_key container_args['environment'][ 'ALGORITHMIA_API_ADDRESS'] = algorithmia_api_address container_args['environment']['MODE'] = mode resp = client.api.create_container(**container_args) client.api.start(resp['Id']) return resp['Id']
def run_algorithm_container(client, image, nvidia_support, algorithmia_api_key): raw_args = {} if nvidia_support: device_request = { 'Driver': 'nvidia', 'Capabilities': [['gpu'], ['nvidia'], ['compute'], ['compat32'], ['graphics'], ['utility'], ['video'], ['display']], # not sure which capabilities are really needed 'Count': -1, # enable all gpus 'NVIDIA_VISIBLE_DEVICES': '-1', } else: device_request = None if isinstance(image, Image): image = image.id raw_args['image'] = image # kwargs['command'] = command raw_args['version'] = client.containers.client.api._version raw_args['ports'] = {9999: 9999} container_args = _create_container_args(raw_args) # modification to the original create function container_args['host_config'] = client.api.create_host_config( port_bindings={LOCAL_PORT: ("127.0.0.1", LOCAL_PORT)}) if device_request is not None: container_args['host_config']['DeviceRequests'] = [device_request] # end modification container_args['detach'] = True if algorithmia_api_key: container_args['environment'] = {} container_args['environment'][ 'ALGORITHMIA_API_KEY'] = algorithmia_api_key resp = client.api.create_container(**container_args) client.api.start(resp['Id']) return resp['Id']
def _create_with_nvidia_container_toolkit(client, image, command, gpus, kwargs): """ This function adds the gpu option to the normal client.containers.create(...) function and adds a device request. This function does not modify the environment variable. :param client: The docker client to use for this create :type client: docker.DockerClient :param image: The image for this docker container :type image: str :param command: The command for this docker container :type command: str or list[str] :param gpus: One of the following options: - The string 'all' to use all available gpus - An int representing the number of gpus to use - a list of device ids or uuids :type gpus: str or int or List[str or int] :param kwargs: The kwargs of the docker.DockerClient.containers.create() function """ # start addition device_request = _get_gpu_device_request(gpus) # end addition if isinstance(image, docker.models.images.Image): image = image.id kwargs['image'] = image kwargs['command'] = command # noinspection PyProtectedMember kwargs['version'] = client.api._version create_kwargs = _create_container_args(kwargs) # addition to the original create function create_kwargs['host_config']['DeviceRequests'] = [device_request] # end addition resp = client.api.create_container(**create_kwargs) return client.containers.get(resp['Id'])
def test_create_container_args(self): create_kwargs = _create_container_args( dict(image='alpine', command='echo hello world', blkio_weight_device=[{ 'Path': 'foo', 'Weight': 3 }], blkio_weight=2, cap_add=['foo'], cap_drop=['bar'], cgroup_parent='foobar', cpu_period=1, cpu_quota=2, cpu_shares=5, cpuset_cpus='0-3', detach=False, device_read_bps=[{ 'Path': 'foo', 'Rate': 3 }], device_read_iops=[{ 'Path': 'foo', 'Rate': 3 }], device_write_bps=[{ 'Path': 'foo', 'Rate': 3 }], device_write_iops=[{ 'Path': 'foo', 'Rate': 3 }], devices=['/dev/sda:/dev/xvda:rwm'], dns=['8.8.8.8'], domainname='example.com', dns_opt=['foo'], dns_search=['example.com'], entrypoint='/bin/sh', environment={'FOO': 'BAR'}, extra_hosts={'foo': '1.2.3.4'}, group_add=['blah'], ipc_mode='foo', kernel_memory=123, labels={'key': 'value'}, links={'foo': 'bar'}, log_config={ 'Type': 'json-file', 'Config': {} }, lxc_conf={'foo': 'bar'}, healthcheck={'test': 'true'}, hostname='somehost', mac_address='abc123', mem_limit=123, mem_reservation=123, mem_swappiness=2, memswap_limit=456, name='somename', network_disabled=False, network='foo', oom_kill_disable=True, oom_score_adj=5, pid_mode='host', pids_limit=500, ports={ 1111: 4567, 2222: None }, privileged=True, publish_all_ports=True, read_only=True, restart_policy={'Name': 'always'}, security_opt=['blah'], shm_size=123, stdin_open=True, stop_signal=9, sysctls={'foo': 'bar'}, tmpfs={'/blah': ''}, tty=True, ulimits=[{ "Name": "nofile", "Soft": 1024, "Hard": 2048 }], user='******', userns_mode='host', uts_mode='host', version='1.23', volume_driver='some_driver', volumes=[ '/home/user1/:/mnt/vol2', '/var/www:/mnt/vol1:ro', 'volumename:/mnt/vol3', '/volumewithnohostpath', '/anothervolumewithnohostpath:ro', 'C:\\windows\\path:D:\\hello\\world:rw' ], volumes_from=['container'], working_dir='/code')) expected = dict(image='alpine', command='echo hello world', domainname='example.com', detach=False, entrypoint='/bin/sh', environment={'FOO': 'BAR'}, host_config={ 'Binds': [ '/home/user1/:/mnt/vol2', '/var/www:/mnt/vol1:ro', 'volumename:/mnt/vol3', '/volumewithnohostpath', '/anothervolumewithnohostpath:ro', 'C:\\windows\\path:D:\\hello\\world:rw' ], 'BlkioDeviceReadBps': [{ 'Path': 'foo', 'Rate': 3 }], 'BlkioDeviceReadIOps': [{ 'Path': 'foo', 'Rate': 3 }], 'BlkioDeviceWriteBps': [{ 'Path': 'foo', 'Rate': 3 }], 'BlkioDeviceWriteIOps': [{ 'Path': 'foo', 'Rate': 3 }], 'BlkioWeightDevice': [{ 'Path': 'foo', 'Weight': 3 }], 'BlkioWeight': 2, 'CapAdd': ['foo'], 'CapDrop': ['bar'], 'CgroupParent': 'foobar', 'CpuPeriod': 1, 'CpuQuota': 2, 'CpuShares': 5, 'CpusetCpus': '0-3', 'Devices': [{ 'PathOnHost': '/dev/sda', 'CgroupPermissions': 'rwm', 'PathInContainer': '/dev/xvda' }], 'Dns': ['8.8.8.8'], 'DnsOptions': ['foo'], 'DnsSearch': ['example.com'], 'ExtraHosts': ['foo:1.2.3.4'], 'GroupAdd': ['blah'], 'IpcMode': 'foo', 'KernelMemory': 123, 'Links': ['foo:bar'], 'LogConfig': { 'Type': 'json-file', 'Config': {} }, 'LxcConf': [{ 'Key': 'foo', 'Value': 'bar' }], 'Memory': 123, 'MemoryReservation': 123, 'MemorySwap': 456, 'MemorySwappiness': 2, 'NetworkMode': 'foo', 'OomKillDisable': True, 'OomScoreAdj': 5, 'PidMode': 'host', 'PidsLimit': 500, 'PortBindings': { '1111/tcp': [{ 'HostIp': '', 'HostPort': '4567' }], '2222/tcp': [{ 'HostIp': '', 'HostPort': '' }] }, 'Privileged': True, 'PublishAllPorts': True, 'ReadonlyRootfs': True, 'RestartPolicy': { 'Name': 'always' }, 'SecurityOpt': ['blah'], 'ShmSize': 123, 'Sysctls': { 'foo': 'bar' }, 'Tmpfs': { '/blah': '' }, 'Ulimits': [{ "Name": "nofile", "Soft": 1024, "Hard": 2048 }], 'UsernsMode': 'host', 'UTSMode': 'host', 'VolumeDriver': 'some_driver', 'VolumesFrom': ['container'], }, healthcheck={'test': 'true'}, hostname='somehost', labels={'key': 'value'}, mac_address='abc123', name='somename', network_disabled=False, networking_config={'foo': None}, ports=[('1111', 'tcp'), ('2222', 'tcp')], stdin_open=True, stop_signal=9, tty=True, user='******', volumes=[ '/mnt/vol2', '/mnt/vol1', '/mnt/vol3', '/volumewithnohostpath', '/anothervolumewithnohostpath', 'D:\\hello\\world' ], working_dir='/code') assert create_kwargs == expected
def test_create_container_args(self): create_kwargs = _create_container_args(dict( image='alpine', command='echo hello world', blkio_weight_device=[{'Path': 'foo', 'Weight': 3}], blkio_weight=2, cap_add=['foo'], cap_drop=['bar'], cgroup_parent='foobar', cpu_period=1, cpu_quota=2, cpu_shares=5, cpuset_cpus='0-3', detach=False, device_read_bps=[{'Path': 'foo', 'Rate': 3}], device_read_iops=[{'Path': 'foo', 'Rate': 3}], device_write_bps=[{'Path': 'foo', 'Rate': 3}], device_write_iops=[{'Path': 'foo', 'Rate': 3}], devices=['/dev/sda:/dev/xvda:rwm'], dns=['8.8.8.8'], domainname='example.com', dns_opt=['foo'], dns_search=['example.com'], entrypoint='/bin/sh', environment={'FOO': 'BAR'}, extra_hosts={'foo': '1.2.3.4'}, group_add=['blah'], ipc_mode='foo', kernel_memory=123, labels={'key': 'value'}, links={'foo': 'bar'}, log_config={'Type': 'json-file', 'Config': {}}, lxc_conf={'foo': 'bar'}, healthcheck={'test': 'true'}, hostname='somehost', mac_address='abc123', mem_limit=123, mem_reservation=123, mem_swappiness=2, memswap_limit=456, name='somename', network_disabled=False, network='foo', oom_kill_disable=True, oom_score_adj=5, pid_mode='host', pids_limit=500, ports={ 1111: 4567, 2222: None }, privileged=True, publish_all_ports=True, read_only=True, restart_policy={'Name': 'always'}, security_opt=['blah'], shm_size=123, stdin_open=True, stop_signal=9, sysctls={'foo': 'bar'}, tmpfs={'/blah': ''}, tty=True, ulimits=[{"Name": "nofile", "Soft": 1024, "Hard": 2048}], user='******', userns_mode='host', version='1.23', volume_driver='some_driver', volumes=[ '/home/user1/:/mnt/vol2', '/var/www:/mnt/vol1:ro', 'volumename:/mnt/vol3', '/volumewithnohostpath', '/anothervolumewithnohostpath:ro', ], volumes_from=['container'], working_dir='/code' )) expected = dict( image='alpine', command='echo hello world', domainname='example.com', detach=False, entrypoint='/bin/sh', environment={'FOO': 'BAR'}, host_config={ 'Binds': [ '/home/user1/:/mnt/vol2', '/var/www:/mnt/vol1:ro', 'volumename:/mnt/vol3', '/volumewithnohostpath', '/anothervolumewithnohostpath:ro' ], 'BlkioDeviceReadBps': [{'Path': 'foo', 'Rate': 3}], 'BlkioDeviceReadIOps': [{'Path': 'foo', 'Rate': 3}], 'BlkioDeviceWriteBps': [{'Path': 'foo', 'Rate': 3}], 'BlkioDeviceWriteIOps': [{'Path': 'foo', 'Rate': 3}], 'BlkioWeightDevice': [{'Path': 'foo', 'Weight': 3}], 'BlkioWeight': 2, 'CapAdd': ['foo'], 'CapDrop': ['bar'], 'CgroupParent': 'foobar', 'CpuPeriod': 1, 'CpuQuota': 2, 'CpuShares': 5, 'CpusetCpus': '0-3', 'Devices': [{'PathOnHost': '/dev/sda', 'CgroupPermissions': 'rwm', 'PathInContainer': '/dev/xvda'}], 'Dns': ['8.8.8.8'], 'DnsOptions': ['foo'], 'DnsSearch': ['example.com'], 'ExtraHosts': ['foo:1.2.3.4'], 'GroupAdd': ['blah'], 'IpcMode': 'foo', 'KernelMemory': 123, 'Links': ['foo:bar'], 'LogConfig': {'Type': 'json-file', 'Config': {}}, 'LxcConf': [{'Key': 'foo', 'Value': 'bar'}], 'Memory': 123, 'MemoryReservation': 123, 'MemorySwap': 456, 'MemorySwappiness': 2, 'NetworkMode': 'foo', 'OomKillDisable': True, 'OomScoreAdj': 5, 'PidMode': 'host', 'PidsLimit': 500, 'PortBindings': { '1111/tcp': [{'HostIp': '', 'HostPort': '4567'}], '2222/tcp': [{'HostIp': '', 'HostPort': ''}] }, 'Privileged': True, 'PublishAllPorts': True, 'ReadonlyRootfs': True, 'RestartPolicy': {'Name': 'always'}, 'SecurityOpt': ['blah'], 'ShmSize': 123, 'Sysctls': {'foo': 'bar'}, 'Tmpfs': {'/blah': ''}, 'Ulimits': [{"Name": "nofile", "Soft": 1024, "Hard": 2048}], 'UsernsMode': 'host', 'VolumesFrom': ['container'], }, healthcheck={'test': 'true'}, hostname='somehost', labels={'key': 'value'}, mac_address='abc123', name='somename', network_disabled=False, networking_config={'foo': None}, ports=[('1111', 'tcp'), ('2222', 'tcp')], stdin_open=True, stop_signal=9, tty=True, user='******', volume_driver='some_driver', volumes=[ '/mnt/vol2', '/mnt/vol1', '/mnt/vol3', '/volumewithnohostpath', '/anothervolumewithnohostpath' ], working_dir='/code' ) assert create_kwargs == expected