Skip to content

Instantly share code, notes, and snippets.

@aramalipoor
Created August 17, 2015 08:22
Show Gist options
  • Save aramalipoor/1b283b11e3faf0fd3cd8 to your computer and use it in GitHub Desktop.
Save aramalipoor/1b283b11e3faf0fd3cd8 to your computer and use it in GitHub Desktop.
Bash helper function to get a specific environment variable from a Docker container
#!/bin/bash
# Retrieves an enviroment variable from a docker container
docker_get_env()
{
env_json=$(docker inspect --format '{{ json .Config.Env }}' ${1})
length=$(echo ${env_json} | jq '. | length')
for (( i=0; i<${length}; i++ ))
do
item=$(echo ${env_json} | jq '.['${i}']')
item=$(echo "[" $(echo -n ${item} | sed -n 's/=/", "/p') "]")
key=$(echo ${item} | jq '.[0]')
value=$(echo ${item} | jq '.[1]' --raw-output)
if [[ ${key} == *"$2"* ]]; then
echo ${value}
return 0
fi
done
return 1
}
# Usage
password=$(docker_get_env my-db-container MYSQL_ROOT_PASSWORD)
echo $password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment