Skip to content

Instantly share code, notes, and snippets.

@sdbondi
Last active August 29, 2015 13:56
Show Gist options
  • Save sdbondi/8951499 to your computer and use it in GitHub Desktop.
Save sdbondi/8951499 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment