# Buggy code
def start_container(image_tag_string):
    """Start the test container in detach state"""
    try:
        client.containers.run(image_tag_string, name=container, detach=True)
    except HTTPError:
        # container may already be running
        pass
    try:
        remove_container()
        client.containers.run(image_tag_string, name=container, detach=True)
    except HTTPError:
        # not sure what the error is now
        raise Exception("Cannot remove running container")


# Traceback (FileNotFoundError)
(ternenv) browne@ubuntu18:~/ternenv/tern$ tern -l report -f output.txt -i debian:buster
2019-04-05 00:27:08,017 - DEBUG - __main__ - Starting...
2019-04-05 00:27:08,018 - DEBUG - report - Setting up...
2019-04-05 00:27:08,018 - DEBUG - container - Checking if image "debian:buster" is available on disk...
Traceback (most recent call last):
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 964, in send
self.connect()
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 964, in send
self.connect()
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/browne/ternenv/bin/tern", line 11, in <module>
load_entry_point('tern==0.3.0', 'console_scripts', 'tern')()
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/__main__.py", line 111, in main
do_main(args)
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/__main__.py", line 54, in do_main
report.execute_docker_image(args)
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/report/report.py", line 422, in execute_docker_image
setup(image_tag_string=args.docker_image)
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/report/report.py", line 63, in setup
if not container.check_image(image_tag_string):
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/utils/container.py", line 69, in check_image
client.images.get(image_tag_string)
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/models/images.py", line 316, in get
return self.prepare_model(self.client.api.inspect_image(name))
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/api/image.py", line 245, in inspect_image
self._get(self._url("/images/{0}/json", image)), True
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/utils/decorators.py", line 46, in inner
return f(self, *args, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/api/client.py", line 225, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/sessions.py", line 546, in get
return self.request('GET', url, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
(ternenv) browne@ubuntu18:~/ternenv/tern$

# Fixed code
def start_container(image_tag_string):
    """Start the test container in detach state"""
    try:
        client.containers.run(image_tag_string, name=container, detach=True)
    except requests.exceptions.HTTPError:
        # container may already be running
        pass
    try:
        remove_container()
        client.containers.run(image_tag_string, name=container, detach=True)
    except requests.exceptions.HTTPError:
        # not sure what the error is now
        raise Exception("Cannot remove running container")