CounterFit-IoT / CounterFit

A simulator for IoT sensors and actuators. This creates fake virtual sensors and actuators when the real ones won't fit on your counter 🤪.
MIT License
200 stars 65 forks source link

Running CounterFit in Dev Container #15

Closed sjuratov closed 2 years ago

sjuratov commented 2 years ago

I am trying to use CounterFit in Dev Container as part of IoT-For-Beginners course. So idea is to run all course using virtual hardware but not directly on local PC but rather in docker container.

Here is a "Hello World" code I am trying to run:

from counterfit_connection import CounterFitConnection
CounterFitConnection.init('127.0.0.1', 5000)
print('Hello World!')

Here is output of the run:

vscode ➜ .../1-introduction-to-iot/code/virtual-device/nightlight (main ✗) $ python app.py 
Traceback (most recent call last):
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/util/connection.py", line 95, in create_connection
    raise err
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/local/python/lib/python3.10/http/client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/python/lib/python3.10/http/client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/python/lib/python3.10/http/client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/python/lib/python3.10/http/client.py", line 1037, in _send_output
    self.send(msg)
  File "/usr/local/python/lib/python3.10/http/client.py", line 975, in send
    self.connect()
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f400832dc00>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/python/lib/python3.10/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/usr/local/python/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /connect (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f400832dc00>: Failed to establish a new connection: [Errno 111] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspaces/IoT-For-Beginners/1-getting-started/lessons/1-introduction-to-iot/code/virtual-device/nightlight/app.py", line 3, in <module>
    CounterFitConnection.init('127.0.0.1', 5000)
  File "/usr/local/python/lib/python3.10/site-packages/counterfit_connection.py", line 42, in init
    requests.post(CounterFitConnection.base_url + 'connect')
  File "/usr/local/python/lib/python3.10/site-packages/requests/api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/python/lib/python3.10/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/python/lib/python3.10/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/python/lib/python3.10/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/python/lib/python3.10/site-packages/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /connect (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f400832dc00>: Failed to establish a new connection: [Errno 111] Connection refused'))
vscode ➜ .../1-introduction-to-iot/code/virtual-device/nightlight (main ✗) $ 

It's worth while saying that if I try to run simple Web server inside of the same container, using same 5000 port, it runs just fine.

Any idea what might be a problem?

jimbobbennett commented 2 years ago

Do you mean a docker container, or a VS Code dev container?

jimbobbennett commented 2 years ago

If you want to use a VS Code dev container, then I've created a repo to do this. It's a template repo, so you can create a new repo from it, and open it in either codespaces or VS Code remote containers. CounterFit is pre-installed, along with Python, so you can just develop your virtual app inside it.

https://github.com/CounterFit-IoT/counterfit-dev-container

jimbobbennett commented 2 years ago

If you want to use a Docker container - then you can use this one:

docker pull jimbobbennett/counterfit:latest

When you run this, you need to map port 5000 to a port on your localhost.

More details and the docker file is here: https://github.com/CounterFit-IoT/counterfit-docker-container