Provide API for allowing full stack emulation
[osm/vim-emu.git] / src / emuvim / api / util / docker_utils.py
1 import logging
2 from io import BytesIO
3
4 import docker
5
6 from emuvim.api.util.path_utils import get_absolute_path
7
8
9 def build_dockerfile_dir(folder, tag):
10 dcli = docker.from_env().api
11 folder = get_absolute_path(folder)
12 build_stream = dcli.build(folder, tag=tag)
13 logging.info('Docker build result:')
14 for line in build_stream:
15 logging.info(line)
16
17
18 def suffix_tag_name(tag, suffix):
19 if ":" in tag:
20 return "%s_%s" % (tag, suffix)
21 return "%s:latest_%s" % (tag, suffix)
22
23
24 def wrap_debian_like(image):
25 dcli = docker.from_env().api
26 dockerfile = '''
27 FROM %s
28 RUN apt update -y && apt install -y net-tools iputils-ping iproute
29 ''' % image
30 f = BytesIO(dockerfile.encode('utf-8'))
31 wrapper_name = suffix_tag_name(image, 'containernet_compatible')
32 logging.info('wrapping image: %s->%s' % (image, wrapper_name))
33 build_stream = dcli.build(fileobj=f, tag=wrapper_name)
34 build_result = [line for line in build_stream]
35 logging.debug('Docker build result:' + '\n'.join(build_result))
36 return wrapper_name
37
38
39 # 172.17.0.1 is the ip of the docker0 interface on the host
40 DOCKER_HOST_IP = '172.17.0.1'