无法使用 python 从 Docker 容器连接到 OPC UA 服务器
最初,我可以运行 Python 脚本从我的系统连接到 OPC 服务器(在本地主机中运行)并获取数据。 但是,当我创建相同 python 代码的 Docker 映像并在容器中运行它时,我收到错误消息:File "/usr/local/lib/python3.10/socket.py", line 833, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] 连接被拒绝
这是我的代码片段。
client= Client("opc.tcp://127.0.0.1:4840",timeout=2000)
sleep(2)
try:
KeepAlive(client,2000)
client.connect()
请注意,我已将 IP 更改为 host.docker.internal 而不是 127.0.0.1,并且我收到不同的错误:
urllib3.exceptions.NewConnectionError:
我的 Dockerfile 如下
FROM python:latest
#RUN mkdir -p /test/inf
WORKDIR /data_sync
#COPY requirement.txt .
#RUN pip install -r requirement.txt
RUN pip3 install opcua
RUN pip install opcua-client
EXPOSE 4840
COPY /data_sync .
CMD [ "python", "Main.py"]
所示 当我尝试使用真实IP地址连接到原始OPC服务器。 总是在 Client.connect() 处抛出错误。
我检查了许多增加超时的解决方案,但没有任何效果。
也尝试过 Linux Container 和 Windows。
我不明白为什么同一段代码在计算机上运行而不是在 docker 上运行。我需要在docker中做一些设置吗?
任何建议表示赞赏。提前致谢。
Intially I am able to Run my Python script to connect to OPC Server (Running in my localhost) from my system and get the data.
But when I create an Docker image of my same python code and run it in the container i get the error saying : File "/usr/local/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
This is the snippet of my code.
client= Client("opc.tcp://127.0.0.1:4840",timeout=2000)
sleep(2)
try:
KeepAlive(client,2000)
client.connect()
Please note I have changed the IP to host.docker.internal instead of 127.0.0.1 as well and the i get different error:
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f064d0f0dc0>: Failed to establish a new connection: [Errno 111] Connection refused
My Dockerfile looks as below
FROM python:latest
#RUN mkdir -p /test/inf
WORKDIR /data_sync
#COPY requirement.txt .
#RUN pip install -r requirement.txt
RUN pip3 install opcua
RUN pip install opcua-client
EXPOSE 4840
COPY /data_sync .
CMD [ "python", "Main.py"]
This also occurs when i try to connect to Original OPC server with real IP address.
Alwasy at Client.connect() it throws error.
I have check many solution increasing the timeout and all but nothing works.
Tried Linux Container and Windows as well.
I am not understanding why the same piece of code works in computer and not docker. Do i Have to do some settings in docker?
Any suggestions is appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论