python 套接字(使用 OPC UA)从 Docker 内连接到模拟服务器失败

Failed python socket (using OPC UA) connect to mock server from within Docker

我很确定这是一个标准的网络套接字问题。我目前正在 docker 调整我的应用程序,它使用了 python py-opcua ( OPC UA ) 包。您对如何解决此问题有任何想法,或者我可以尝试任何建议吗?

我已经设置了一个带有节点的模拟服务器,运行在我的 Mac 本地机器上(这里没有 Dockerization!):

        self.server = Server()
        self.server.set_endpoint("opc.tcp://0.0.0.0:50001/")

        # setup our own namespace, not really necessary but should as spec
        uri = "http://some-uri.com"
        self.idx = self.server.register_namespace(uri)

        # get Objects node, this is where we should put our custom stuff
        self.objects = self.server.get_objects_node()
        self.root = self.objects.add_object(self.idx, "Child")

        self._populate_server_with_variables()
        self.server.iserver.history_manager.set_storage(HistorySQLite("mock.db"))

        # Sample new values for all variables...
        self.start()

        self.make_all_variables_writable()

现在我正在尝试使用 py-opcua Client class 连接到上述服务器,连接如下(这部分 运行s 来自 docker container! 同样,来自我的本地机器):

        self.client = Client(
            "opc.tcp://0.0.0.0:50001/",
            timeout=60.
        )

        self.client.connect()  # Where my application fails from within Docker!

        self.root = self.client.get_root_node()

当我运行 docker 容器时,客户端无法连接到模拟服务器。具体来说,当(尝试)建立连接时,docker 容器崩溃。

我尝试了 运行 宁以下 docker run 命令,它们都产生相同的错误:

docker run --env-file env/.local-docker.env -i --rm -p 50001:50001 image_name
docker run --env-file env/.local-docker.env -i --rm --network host image_name
docker run --env-file env/.local-docker.env -i --rm --network=host image_name

我收到的错误始终是来自客户端的套接字连接失败。具体来说,我收到以下错误消息:

Logfile will be save in:  ./logs/log_2020-03-01 18:54:36.769166.log
Connecting to... "opc.tcp://0.0.0.0:50001/"
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/app/deploy/middleware/loop.py", line 96, in <module>
    opc_reader_rt = RealTimeReader(opc_path)
  File "/app/deploy/opc_client/dataset.py", line 40, in __init__
    self.client.connect()
  File "/usr/local/lib/python3.6/site-packages/opcua/client/client.py", line 256, in connect
    self.connect_socket()
  File "/usr/local/lib/python3.6/site-packages/opcua/client/client.py", line 281, in connect_socket
    self.uaclient.connect_socket(self.server_url.hostname, self.server_url.port)
  File "/usr/local/lib/python3.6/site-packages/opcua/client/ua_client.py", line 256, in connect_socket
    return self._uasocket.connect_socket(host, port)
  File "/usr/local/lib/python3.6/site-packages/opcua/client/ua_client.py", line 155, in connect_socket
    sock = socket.create_connection((host, port), timeout=self.timeout)
  File "/usr/local/lib/python3.6/socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/local/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

原来环境变量错误地读取了一个双引号 https://github.com/docker/compose/issues/3702