Docker encapsulates the OPCUA client image of python

Make sure that the virtual machine is connected to the external network, open the HyperTerminal, and download the continuumio/anaconda3 image

docker pull continuumio/anaconda3

Create an empty folder, create two files Dockerfile and main.py under this folder
Insert picture description here

Edit the Dockerfile content as follows:

FROM continuumio/anaconda3:latest
ADD main.py /

RUN pip install paho-mqtt
RUN pip install freeopcua
RUN pip install opcua-client

CMD ["python","-u","./main.py"]

Edit the content of main.py as follows:

from opcua import Client
from opcua import ua
import time
class SubHandler(object):
    def data_change(self, handle, node, val, attr):
        print("Python: New data change event", handle, node, val, attr)
client = Client("opc.tcp://192.168.224.157:4840/")
client.connect()
objects = client.get_objects_node()
root = client.get_root_node()
#myvar = root.get_child(["0:Objects","2:DeviceSet","3:test","3:DataBlocksGlobal","3:db1","3:tags","3:0"])
myvar = client.get_node("ns=2;i=14")
#myvar = client.get_node("ns=5;s=MotionDeviceSystem.ProcessData.System.$CONST_VEL")
#myvar = client.get_node('ns=5;s=MotionDeviceSystem.Controllers.Controller_1.ParameterSet.CurrentProjectDescription')
#myvar = client.get_node('ns=3;s="clocl0.5hz"."tags"[0]')
#var = client.get_node('ns=3;s="Tag_1"')
handler = SubHandler()
sub = client.create_subscription(500, handler)
sub.subscribe_data_change(myvar)
time.sleep(100000)
client.disconnect()

Regarding the OPC UA server configuration and the IP and port filled in by the client, please modify the Node address according to the actual situation. Do not use 127.0.0.1 as the IP address for connecting to the OPC UA server.
In this folder directory, compile a new image image named edgebox with version 0.1. Note that the dot at the end of the instruction should not be lost, which means that you can find the dock configuration file Dockerfile
docker build -t edgebox:0.1 in the current directory .
Wait for the compilation to be completed, use the instruction to view the newly generated image
docker images to
run the opc ua client, anonymously from the opc ua server Subscribe to a variable
docker run -it edgebox:0.1
Refer to common instructions
View all created containers
docker container ls -a
View all docker container processes
docker ps
Delete image image
docker rmi Image name or ID
delete all docker containers
sudo docker rm $(sudo docker ps -a -q)
delete docker container
docker container rm container name or ID

Guess you like

Origin blog.csdn.net/weloveut/article/details/108200912