【Docker】容器获取宿主机IP和MAC
【代码】容器获取宿主机IP和MAC。
·
将IP和MAC写入环境变量
# 宿主机.bashrc文件
export ip_address=$(ifconfig | grep 'inet .*br' | sed -E 's/.*inet (.*) netmask.*/\1/')
export mac_address=$(ifconfig eth0 | grep ether | grep -oE "[a-fA-F0-9:]{17}")
docker compose读取环境变量并传入容器
version: "3.8"
services:
unicom_etl:
image: unicom_etl:1.5
build: .
container_name: unicom_etl_1.5
environment:
- mac_address=${mac_address}
volumes:
- log:/vol
- ./:/app
restart: always
容器内获取环境变量
import os
os.environ.get('mac_address')
参考:
https://www.04007.cn/article/1147.html
https://github.com/docker/compose/issues/4081#issuecomment-416165383
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)