Create environment variables for docker containers on the host machine
#!/bin/bash
# Create environment variables for docker containers on the host machine
containers=(
"DB0:postgres"
"ES0:elasticsearch"
"REDIS0:redis"
)
for c in "${containers[@]}"; do
ALIAS="${c%%:*}"
CONTAINER="${c##*:}"
ip=$(docker inspect -format='{{ .NetworkSettings.IPAddress }}' $CONTAINER)
# Only gets first exposed port for now
port=($(docker inspect -format='{{ .NetworkSettings.Ports }}' $CONTAINER | sed "s/^map\[\([0-9]*\)\/\([a-z]\{3,\}\).*\]/\1 \2/"))
export "${ALIAS}_PORT_${port[0]}_${port[1]^^}"=${port[1]}://$ip:${port[0]}
export "${ALIAS}_PORT_${port[0]}_${port[1]^^}_PROTO"=$ip:${port[0]}
export "${ALIAS}_PORT_${port[0]}_${port[1]^^}_ADDR"=$ip
export "${ALIAS}_PORT_${port[0]}_${port[1]^^}_PORT"=${port[0]}
done