有时容器需要以host 随机端口的形式起动,可以前置执行这个脚本获取机器的IP和端口
getipport.py
代码语言:javascript复制import socket
import os
import sys
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
ttype = sys.argv[1]
if ttype == "ip":
ip = get_ip_address(os.getenv("eth", "eth1"))
print(ip)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 0))
addr = s.getsockname()
print(addr[1])
s.close()
执行: run.sh
代码语言:javascript复制ip=`python getipport.py ip`
echo $ip
port=`python getipport.py port`
echo $port
export listen_ip=$ip
export listen_port=$port
exec ./myapp 2>>./stderr/stderr.log