【发布时间】:2021-02-24 17:48:09
【问题描述】:
下午好。我是 Docker 的新手,我正在努力想出一个可扩展的解决方案来生成网络流量。我想让许多客户端和服务器容器通过 docker 网桥网络相互通信。现在我有一个客户端和一个服务器容器,它们可以相互通信,但速度非常慢。此外,我必须对他们的 IP 地址进行硬编码,以便他们能够找到彼此。必须有更好的方法来扩大规模,但我正在苦苦挣扎,因为我的 client.py 代码需要服务器的 IP 地址和端口号才能找到服务器。
客户端.py
from pyModbusTCP.client import ModbusClient
import time
c = ModbusClient()
c.host("172.20.0.2")
c.port(502)
while True:
if not c.is_open():
if not c.open():
print("Unable to connect to Server at 172.20.0.2:502")
if c.is_open():
regs = c.read_holding_registers(0, 4)
if regs:
print("Register #0: " + str(regs[0]))
print("Register #1: " + str(regs[1]))
print("Register #2: " + str(regs[2]))
print("Register #3: " + str(regs[3]))
time.sleep(5)
客户端 DockerFile
FROM python:3
WORKDIR ~/client-server-docker-test/client
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./client.py"]
服务器.py
from pyModbusTCP.server import ModbusServer, DataBank
from time import sleep
from random import uniform
# set up server
server = ModbusServer('localhost',502, no_block=True)
# initialize register 0 with value of 80
DataBank.set_words(0, [80])
try:
print("Start server...")
server.start()
print("Server is online...")
# change register value every 5 seconds.
while True:
# Set Register @ Address 0 to random int. value
DataBank.set_words(0, [int(uniform(0,100))])
# Tank 2
DataBank.set_words(1, [int(uniform(0,100))])
# Tank 3
DataBank.set_words(2, [int(uniform(0,100))])
# Tank 4
DataBank.set_words(3, [int(uniform(0,100))])
sleep(5)
# when hit ctrl+C in CMD line, shut down server
except:
print("Shutdown server....")
server.stop()
print("Server is offline...")
sleep(0.5)
服务器 DockerFile
FROM python:3
WORKDIR ~/client-server-docker-test/server
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./server.py"]
Docker 编写文件
version: '2.1'
services:
server:
image: server1
container_name: server1_con
ports:
- 502:502
networks:
test_net:
ipv4_address: 172.20.0.2
client:
image: client1
container_name: client1_con
depends_on:
- server
networks:
test_net:
ipv4_address: 172.20.0.3
networks:
test_net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.20.0.0/16
这会运行,但我的客户需要永远将寄存器值打印到命令行。除了速度慢之外,我目前的方法是不可扩展的。我必须使用MODBUS TCP协议进行通信,这就是python代码使用pyModbusTCP的原因。但是必须有一种方法可以让 10 个客户端和服务器容器相互通信,而不必为每个容器指定 IP 地址?
我研究了 docker swarm,但我看不出它如何帮助解决为每个容器分配静态 IP 地址的问题。我还尝试添加一个环境变量,其中 docker 获取服务器的 IP 地址并将其传递到 client.py 代码中,但我无法让它正常工作。我很困惑,任何帮助将不胜感激。非常感谢您的宝贵时间。
编辑:它非常慢意味着当我运行“docker-compose up”时,我会收到“附加到 server1_con,client1_con”的通知,但是我的命令行需要大约 10 分钟才能开始显示打印输出。
【问题讨论】:
-
你读过Networking In Compose吗?您可以只使用服务的名称(例如
server)而不是 IP(并且无需硬编码 IP 地址 - docker 将解析 DNS 查询)。关于“它非常慢”,请提供更多信息(例如,c.read_holding_registers(0, 4)需要 5 秒才能返回)。 -
它非常慢意味着当我运行“docker-compose up”时,我收到一个通知“附加到 server1_con,client1_con”但是我的命令行需要大约 10 分钟才能开始显示打印输出。我遇到的部分麻烦是我的 python 代码需要 IP 地址。我已阅读 Compose 中的网络,但不了解他们的网络应用程序和数据库如何知道相互联系。我尝试将名称“服务器”放入我的 client.py c.host() 行,但它不起作用。
标签: docker docker-compose client-server modbus-tcp