将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