31 lines
1 KiB
Bash
Executable file
31 lines
1 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
# Configurable Variables
|
|
DOCKER_SOCK_PATH="/tmp/docker.sock"
|
|
HOST_CONF_PATH="/tmp/hosts"
|
|
NETWORK_NAME="proxy"
|
|
|
|
query_docker () {
|
|
echo $(curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1/json$2)
|
|
}
|
|
|
|
while true
|
|
do
|
|
PROXY_HOST_CONF=""
|
|
CONTAINER_LIST=$(query_docker "containers" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}" | jq -cr '.[].Id')
|
|
|
|
for id in $CONTAINER_LIST
|
|
do
|
|
# Query individual container to access Env variables
|
|
# Filter Env for HOSTNAME, remove list parenthesis and split/only keep values
|
|
HOST_NAMES=$(query_docker "containers/${id}" | jq -cr '.Config.Env[] | select(contains("VIRTUAL_HOST=")) | split("=")[1]')
|
|
|
|
for hostname in $HOST_NAMES
|
|
do
|
|
PROXY_HOST_CONF="$PROXY_HOST_CONF\n127.0.0.1 $hostname # Added by hostman"
|
|
done
|
|
done
|
|
|
|
FILTERED_HOSTS=$(grep -ve "# Added by hostman$" $HOST_CONF_PATH)
|
|
echo -e "$FILTERED_HOSTS$PROXY_HOST_CONF" > $HOST_CONF_PATH
|
|
sleep 10
|
|
done
|