Issue
I'm currently turning my code into docker images to control the lights based on a sensor. I'm quite a novice at this. It runs locally but when I turn it into an image with this dockerfile using docker build .:
#set base image (host OS)
FROM python:3.8-slim
# set the working directory in the container
WORKDIR /code
# copy the dependencies file to the working
directory
COPY requirements.txt .
# install dependencies
RUN pip install -r requirements.txt
# copy the content of the local src directory to the working directory
COPY src/ .
# command to run on container start
CMD [ "python", "./main.py"]
I get the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-
packages/asyncio_mqtt/client.py", line 79, in
connect
await loop.run_in_executor(None,
self._client.connect, self._hostname, self._port,
60)File
"/usr/local/lib
/python3.8/concurrent/futures/thread.p y", line
57, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.8/site-
packages/paho/mqtt/client.py", line 941, in
connect
return self.reconnect()
File "/usr/local/lib/python3.8/site-
packages/paho/mqtt/client.py", line 1075, in
reconnect
sock = self._create_socket_connection()
File "/usr/local/lib/python3.8/site-
packages/paho/mqtt/client.py", line 3546, in
_create_socket_connection
return socket.create_connection(addr,
source_address=source, timeout=self._keepalive)
File "/usr/local/lib/python3.8/socket.py", line
787, in create_connection
for res in getaddrinfo(host, port, 0,
SOCK_STREAM):
File "/usr/local/lib/python3.8/socket.py", line
918, in getaddrinfo
for res in _socket.getaddrinfo(host, port,
family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not
known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./main.py", line 89, in <module>
asyncio.run(main())
File
"/usr/local/lib/python3.8/asyncio/runners.py",
line 44, in run
return loop.run_until_complete(main)
File
"/usr/local/lib/
python3.8/asyncio/base_events.py", line 616, in
run_until_complete
return future.result()
File "./main.py", line 86, in main
await asyncio.gather(*tasks)
File "./main.py", line 26, in sub_and_listen_task
async with Client(self.broker) as client:
File "/usr/local/lib/python3.8/site-
packages/asyncio_mqtt/client.py", line 324, in
__aenter__
await self.connect()
File "/usr/local/lib/python3.8/site-
packages/asyncio_mqtt/client.py", line 88, in
connect
raise MqttError(str(error))
asyncio_mqtt.error.MqttError: [Errno -2] Name or
service not known
Basically service unknown at, from what I'm understanding, the connection. The issue is that I did this before and it worked but now when i run that exact same dockerfile and I look in portainer its running python 3.8.10 instead of 3.8.9 (the working container ran 3.8.9 while the not working one is running 3.8.10) which is the only difference I can detect. I tried removing the old python image but I have no clue how to specifically get 3.8.9 (if that even is the issue thats causing this).
Other than that my code is based on asyncio_mqtt. In the requirements file i specified also:
orjson==3.5.1
asyncio_mqtt==0.8.1
The connection, publishing, etc. code below:
class mqtt_task():
def __init__(self, broker, subscription):
self.broker = broker
self.subscription = subscription
async def sub_and_listen_task(self):
async with Client(self.broker) as client:
self.client = client
async with client.filtered_messages(self.subscription) as
messages:
await client.subscribe(self.subscription)
async for message in messages:
if "ON" in message.payload.decode("utf-8"):
space_name = config[0]
spaces[space_name].moved()
async def publish_msg(self, space_name, state):
msg = {'state': state}
try:
await self.client.publish(space_name + '/desired/light' ,
orjson.dumps(msg))
except AttributeError as ex:
print('Unable to publish to ' + space_name +
'/desired/light')
#print(ex)
mqtt = mqtt_task(broker_local, subscription_local)
class SpaceCode:
def __init__(self, name):
self.name = name
self.event = asyncio.Event()
self.task = asyncio.create_task(self.motion_task())
def moved(self):
print('Movement detected...')
self.event.set()
async def movement_detected(self):
await self.event.wait()
self.event.clear()
async def motion_task(self):
while True:
await mqtt.publish_msg(self.name, 'on')
print('TURNING ON.... ' + self.name)
try:
await asyncio.wait_for(self.movement_detected(), timeout=time_untill_dim)
continue
except asyncio.TimeoutError:
print('TIMEOUT...DIMMING ' + self.name)
await mqtt.publish_msg(self.name, 'dim')
try:
await asyncio.wait_for(self.movement_detected(), timeout=time_untill_shutoff)
continue
except asyncio.TimeoutError:
print('TIMEOUT...TURNING OFF ' + self.name)
await mqtt.publish_msg(self.name, 'off')
await self.movement_detected()
spaces = dict()
async def main():
tasks = []
for name in config:
spaces[name] = SpaceCode(name)
tasks.append(mqtt.sub_and_listen_task())
for t in spaces.values():
tasks.append(t.task)
print(tasks)
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
Any feedback would be appreciated!
Solution
@hardillb was right the error message had to do with an connection issue. The broker was never able to connect due to the fact that within docker it wasn't in the correct network within docker.
Answered By - ajay1738
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.