Skip to content

Commit 7f95455

Browse files
committed
Implement setting TLD programmatically
1 parent 842abd7 commit 7f95455

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

README.md

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,58 @@ Then the env var in SWAG can be set as `DOCKER_HOST=dockerproxy`. This will allo
4141

4242
If both `DOCKER_HOST` and `docker.sock` volumes are provided this mod will detect containers using both connections. As noted in the [requirements](#requirements), containers detected via `docker.sock` must be in the same user defined network or have `swag_address` label set.
4343

44-
Multiple remote hosts can be used via `DOCKER_HOST` by separating hosts with a comma. Additionally, a friendly host name can be assigned by suffixing the host with `|friendlyName`. If the host is not assigned a friendly name it will have a default generated like `hostN` where N is the position of the host in `DOCKER_HOST`. Example:
44+
Multiple remote hosts can be used via `DOCKER_HOST` by separating hosts with a comma. Additional per-host settings can be assigned by separating with a pipe `|`. The syntax for per-host configuration:
4545

4646
```
47-
DOCKER_HOST=192.168.0.100:2375|serverA,192.168.0.110:2375|serverB,192.168.0.130:2375
47+
host:port|friendly_name|default_tld
4848
```
4949

50-
* Host: `192.168.0.100:2375` -- Friendly Name: `serverA`
51-
* Host: `192.168.0.110:2375` -- Friendly Name: `serverB`
52-
* Host: `192.168.0.130:2375` -- Friendly Name: `host3`
5350

54-
If the detected containers for each host do not have the `swag_address` label set then the host IP will be used.
51+
```
52+
DOCKER_HOST=192.168.0.100:2375|serverA,192.168.0.110:2375|serverB|local.test,192.168.0.130:2375
53+
```
54+
55+
* Host: `192.168.0.100:2375` -- Friendly Name: `serverA` -- TLD: `*`
56+
* Host: `192.168.0.110:2375` -- Friendly Name: `serverB` -- TLD: `local.test`
57+
* Host: `192.168.0.130:2375` -- Friendly Name: `host3` -- TLD: `*`
58+
59+
### Upstream IP and Port
60+
61+
When using a remote docker host from `DOCKER_HOST` auto-proxy assumes the detected containers on not on the same network as SWAG:
62+
63+
* If the detected containers do not have the `swag_address` label set then the Host IP will be used.
64+
* If the detected containers do not have the `swag_port` label set then auto-proxy attempts to find a mapped **host port** on the container and will use it based on **container port** in this order:
65+
* 80
66+
* 8080
67+
* The first mapped port, if any
68+
69+
### Subdomains and TLD
70+
71+
If a detected container does not have `swag_url` label set then the subdomain and TLD can be programmatically generated.
72+
73+
The default TLD used in nginx [`server_name` directive](https://nginx.org/en/docs/http/server_names.html) can be set using `HOST_TLD`. This can also be set per-host using the syntax described in [`DOCKER_HOST` for `default_tld`.](#multiple-hosts)
74+
75+
The subdomain used for a container can optionally be modified to include the Host's `friendly_name` described in the `DOCKER_HOST` syntax by setting the `HOST_INSERT` to either `prefix` or `suffix`
5576
56-
If the detected containers for each host do not have the `swag_url` label set then the default can be configured with friendly host name inserted into the url as either a suffix or prefix using the env `HOST_INSERT`. Example:
77+
Examples using a container named `overseer`:
5778
58-
* container name: `swag`
59-
* `DOCKER_HOST=192.168.0.100:2375|serverA`
60-
* `HOST_INSERT`
61-
* (unset) => nginx `server_name swag.*`
62-
* `prefix` => nginx `server_name serverA-swag.*`
63-
* `suffix` => nginx `server_name swag-serverA.*`
79+
* Using only HOST_INSERT to modify subdomain
80+
* `DOCKER_HOST=192.168.0.100:2375|serverA`
81+
* `HOST_TLD` (not set, defaults to `*`)
82+
* `HOST_INSERT`
83+
* (unset) => nginx `server_name swag.*`
84+
* `prefix` => nginx `server_name serverA-overseer.*`
85+
* `suffix` => nginx `server_name overseer-serverA.*`
86+
* Using HOST_INSERT prefix and HOST_TLD
87+
* `DOCKER_HOST=192.168.0.100:2375|serverA`
88+
* `HOST_TLD=test.home`
89+
* `HOST_INSERT=prefix`
90+
* `server_name serverA-overseer.test.home`
91+
* Using HOST_INSERT prefix and default_tld
92+
* `DOCKER_HOST=192.168.0.100:2375|serverA|myserver.home`
93+
* `HOST_INSERT=prefix`
94+
* `server_name serverA-overseer.myserver.home`
95+
* Using HOST_TLD only
96+
* `DOCKER_HOST=192.168.0.100:2375`
97+
* `HOST_TLD=myserver.home`
98+
* `server_name overseer.myserver.home`

root/app/auto-proxy.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ DUDE
157157
echo "**** Setting proto ${swag_proto} for ${CONTAINER_ID} ****"
158158
if [ -z "${swag_url}" ]; then
159159
if [ "$HOST_INSERT" == "suffix" ]; then
160-
swag_url="${CONTAINER}-${DOCKER_HOST_NAME}.*"
160+
swag_url="${CONTAINER}-${DOCKER_HOST_NAME}.${HOST_TLD}"
161161
elif [ "$HOST_INSERT" == "prefix" ]; then
162-
swag_url="${DOCKER_HOST_NAME}-${CONTAINER}.*"
162+
swag_url="${DOCKER_HOST_NAME}-${CONTAINER}.${HOST_TLD}"
163163
else
164-
swag_url="${CONTAINER}.*"
164+
swag_url="${CONTAINER}.${HOST_TLD}"
165165
fi
166166
fi
167167
sed -i "s|server_name .*|server_name ${swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER_ID}.subdomain.conf"

root/app/docker-wrap.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/with-contenv bash
22

3+
HOST_TLD_ORIG=${HOST_TLD:=*}
34
if [[ -v FIRST_RUN ]];then
45
echo '**** Auto Proxy - first-run ****'
56
fi
@@ -18,12 +19,18 @@ if [[ -v DOCKER_HOST ]];then
1819
DOCKER_HOST_NAME="host${INDEX}"
1920
fi
2021

22+
if [[ -v arrIN[2] ]];then
23+
HOST_TLD=${arrIN[2]}
24+
else
25+
HOST_TLD=$HOST_TLD_ORIG
26+
fi
27+
2128
# get default upstream ip
2229
HOST_PARTS=(${DOCKER_HOST//:/ })
2330
UPSTREAM_HOST="${HOST_PARTS[0]}"
2431

2532
if [[ -v FIRST_RUN ]];then
26-
echo "**** Auto Proxy - Generating proxies for => Host: ${DOCKER_HOST} | Name: ${DOCKER_HOST_NAME:-N/A} | Default Upstream IP: ${UPSTREAM_HOST} ****"
33+
echo "**** Auto Proxy - Generating proxies for => Host: ${DOCKER_HOST} | Name: ${DOCKER_HOST_NAME:-N/A} | Default Upstream IP: ${UPSTREAM_HOST} | Host TLD: ${HOST_TLD} ****"
2734
fi
2835
. /app/auto-proxy.sh
2936

@@ -33,7 +40,7 @@ fi
3340

3441
if [ -S /var/run/docker.sock ]; then
3542
if [[ -v FIRST_RUN ]];then
36-
echo '**** Auto Proxy - Detected docker.sock, generating proxies for => Host: Local | Name: Local | Default Upstream IP: N/A ****'
43+
echo "**** Auto Proxy - Detected docker.sock, generating proxies for => Host: Local | Name: Local | Default Upstream IP: N/A | Host TLD: ${HOST_TLD} ****"
3744
fi
3845
DOCKER_HOST_NAME="local"
3946
unset DOCKER_HOST

0 commit comments

Comments
 (0)