Skip to content

Commit cf064c3

Browse files
committed
Parameterize nginx-proxy and docker-gen container labels
1 parent c8efa30 commit cf064c3

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

app/entrypoint.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,16 @@ if [[ "$*" == "/bin/bash /app/start.sh" ]]; then
181181
echo "Error: can't get nginx-proxy container ID !" >&2
182182
echo "Check that you are doing one of the following :" >&2
183183
echo -e "\t- Use the --volumes-from option to mount volumes from the nginx-proxy container." >&2
184-
echo -e "\t- Set the NGINX_PROXY_CONTAINER env var on the letsencrypt-companion container to the name of the nginx-proxy container." >&2
185-
echo -e "\t- Label the nginx-proxy container to use with 'com.github.nginx-proxy.nginx'." >&2
184+
echo -e "\t- Set the NGINX_PROXY_CONTAINER env var on the acme-companion container to the name of the nginx-proxy container." >&2
185+
echo -e "\t- Label the nginx-proxy container to use with '${default_nginx_proxy_container_label}'." >&2
186+
echo -e "\t- Label the nginx-proxy container with a custom label and set NGINX_PROXY_CONTAINER_LABEL env var on the acme-companion container to match the custom label." >&2
186187
exit 1
187188
elif [[ -z "$(get_docker_gen_container)" ]] && ! is_docker_gen_container "$(get_nginx_proxy_container)"; then
188189
echo "Error: can't get docker-gen container id !" >&2
189190
echo "If you are running a three containers setup, check that you are doing one of the following :" >&2
190-
echo -e "\t- Set the NGINX_DOCKER_GEN_CONTAINER env var on the letsencrypt-companion container to the name of the docker-gen container." >&2
191-
echo -e "\t- Label the docker-gen container to use with 'com.github.nginx-proxy.docker-gen'." >&2
191+
echo -e "\t- Set the NGINX_DOCKER_GEN_CONTAINER env var on the acme-companion container to the name of the docker-gen container." >&2
192+
echo -e "\t- Label the docker-gen container to use with '${default_nginx_docker_gen_container_label}'." >&2
193+
echo -e "\t- Label the docker-gen container with a custom label and set NGINX_DOCKER_GEN_CONTAINER_LABEL env var on the acme-companion container to match the custom label." >&2
192194
exit 1
193195
fi
194196
check_writable_directory '/etc/nginx/certs'

app/functions.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ if [[ "$DEBUG" == true ]]; then
1010
DEBUG=1 && export DEBUG
1111
fi
1212

13+
default_nginx_docker_gen_container_label=com.github.nginx-proxy.docker-gen
14+
default_nginx_proxy_container_label=com.github.nginx-proxy.nginx
15+
runtime_nginx_docker_gen_container_label=${NGINX_DOCKER_GEN_CONTAINER_LABEL:-$default_nginx_docker_gen_container_label}
16+
runtime_nginx_proxy_container_label=${NGINX_PROXY_CONTAINER_LABEL:-$default_nginx_proxy_container_label}
17+
1318
function parse_true() {
1419
case "$1" in
1520

@@ -284,7 +289,7 @@ function is_docker_gen_container {
284289
function get_docker_gen_container {
285290
# First try to get the docker-gen container ID from the container label.
286291
local legacy_docker_gen_cid; legacy_docker_gen_cid="$(labeled_cid com.github.jrcs.letsencrypt_nginx_proxy_companion.docker_gen)"
287-
local new_docker_gen_cid; new_docker_gen_cid="$(labeled_cid com.github.nginx-proxy.docker-gen)"
292+
local new_docker_gen_cid; new_docker_gen_cid="$(labeled_cid $runtime_nginx_docker_gen_container_label)"
288293
local docker_gen_cid; docker_gen_cid="${new_docker_gen_cid:-$legacy_docker_gen_cid}"
289294

290295
# If the labeled_cid function dit not return anything and the env var is set, use it.
@@ -300,7 +305,7 @@ function get_nginx_proxy_container {
300305
local volumes_from
301306
# First try to get the nginx container ID from the container label.
302307
local legacy_nginx_cid; legacy_nginx_cid="$(labeled_cid com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy)"
303-
local new_nginx_cid; new_nginx_cid="$(labeled_cid com.github.nginx-proxy.nginx)"
308+
local new_nginx_cid; new_nginx_cid="$(labeled_cid $runtime_nginx_proxy_container_label)"
304309
local nginx_cid; nginx_cid="${new_nginx_cid:-$legacy_nginx_cid}"
305310

306311
# If the labeled_cid function dit not return anything ...

docs/Getting-containers-IDs.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ There are three methods to inform the **acme-companion** container of the **ngin
66

77
* `label` method: add the label `com.github.nginx-proxy.nginx` to the **nginx**/**nginx-proxy** container.
88

9+
* Optional. Using a custom `label`: add a custom label, e.g. :`custom.label.nginx` to the **nginx**/**nginx-proxy** container and set the environment variable `NGINX_PROXY_CONTAINER_LABEL` to match the custom label.
10+
911
* `environment variable` method: assign a fixed name to the **nginx**/**nginx-proxy** container with `container_name:` and set the environment variable `NGINX_PROXY_CONTAINER` to this name on the **acme-companion** container.
1012

1113
* `volumes_from` method. Using this method, the **acme-companion** container will get the **nginx**/**nginx-proxy** container ID from the volumes it got using the `volumes_from` option.
@@ -14,6 +16,8 @@ And two methods to inform the **acme-companion** container of the **docker-gen**
1416

1517
* `label` method: add the label `com.github.nginx-proxy.docker-gen` to the **docker-gen** container.
1618

19+
* Optional. Using a custom `label`: add a custom label, e.g. :`custom.label.docker-gen` to the **docker-gen** container and set the environment variable `NGINX_DOCKER_GEN_CONTAINER_LABEL` to match the custom label.
20+
1721
* `environment variable` method: assign a fixed name to the **docker-gen** container with `container_name:` and set the environment variable `NGINX_DOCKER_GEN_CONTAINER` to this name on the **acme-companion** container.
1822

1923
The methods for each container are sorted by order of precedence, meaning that if you use both the label and the volumes_from method, the ID of the **nginx**/**nginx-proxy** container that will be used will be the one found using the label. **There is no point in using more than one method at a time for either the nginx/nginx-proxy or docker-gen container beside potentially confusing yourself**.
@@ -39,6 +43,25 @@ $ docker run --detach \
3943
nginxproxy/acme-companion
4044
```
4145

46+
`label` method with custom label.
47+
```
48+
$ docker run --detach \
49+
[...]
50+
--label custom.label.nginx \
51+
nginx
52+
53+
$ docker run --detach \
54+
[...]
55+
--label custom.label.docker-gen \
56+
nginxproxy/docker-gen
57+
58+
$ docker run --detach \
59+
[...]
60+
--env NGINX_PROXY_CONTAINER_LABEL=custom.label.nginx
61+
--env NGINX_DOCKER_GEN_CONTAINER_LABEL=custom.label.docker-gen
62+
nginxproxy/acme-companion
63+
```
64+
4265
`environment variable` method
4366
```
4467
$ docker run --detach \

test/tests/docker_api/run.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
nginx_vol='nginx-volumes-from'
66
nginx_env='nginx-env-var'
77
nginx_lbl='nginx-label'
8+
nginx_cstm_lbl='nginx-custom-label'
89
docker_gen='docker-gen-no-label'
910
docker_gen_lbl='docker-gen-label'
11+
docker_gen_cstm_lbl='docker-gen-custom-label'
1012

1113
case $SETUP in
1214

@@ -20,6 +22,7 @@ case $SETUP in
2022
"$nginx_vol" \
2123
"$nginx_env" \
2224
"$nginx_lbl" \
25+
"$nginx_cstm_lbl" \
2326
&> /dev/null
2427
}
2528
trap cleanup EXIT
@@ -78,13 +81,31 @@ case $SETUP in
7881
"$1" \
7982
bash -c "$commands" 2>&1
8083

84+
# Run a nginx-proxy container named nginx-custom-label, with a custom label.
85+
# Store the container id in the custom_labeled_nginx_cid variable.
86+
custom_labeled_nginx_cid="$(docker run --rm -d \
87+
--name "$nginx_cstm_lbl" \
88+
-v /var/run/docker.sock:/tmp/docker.sock:ro \
89+
--label custom.label.nginx \
90+
nginxproxy/nginx-proxy)"
91+
92+
# This should target the nginx-proxy container with the custom label (nginx-custom-label)
93+
docker run --rm \
94+
-v /var/run/docker.sock:/var/run/docker.sock:ro \
95+
--volumes-from "$nginx_vol" \
96+
-e "NGINX_PROXY_CONTAINER_LABEL=custom.label.nginx" \
97+
"$1" \
98+
bash -c "$commands" 2>&1
99+
81100
cat > "${GITHUB_WORKSPACE}/test/tests/docker_api/expected-std-out.txt" <<EOF
82101
Container $nginx_vol received exec_start: sh -c /app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload
83102
$nginx_vol
84103
Container $nginx_env received exec_start: sh -c /app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload
85104
$nginx_env
86105
Container $nginx_lbl received exec_start: sh -c /app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload
87106
$labeled_nginx_cid
107+
Container $nginx_cstm_lbl received exec_start: sh -c /app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload
108+
$custom_labeled_nginx_cid
88109
EOF
89110
;;
90111

@@ -98,8 +119,10 @@ EOF
98119
"$nginx_vol" \
99120
"$nginx_env" \
100121
"$nginx_lbl" \
122+
"$nginx_cstm_lbl" \
101123
"$docker_gen" \
102124
"$docker_gen_lbl" \
125+
"$docker_gen_cstm_lbl" \
103126
&> /dev/null
104127
}
105128
trap cleanup EXIT
@@ -220,6 +243,30 @@ EOF
220243
"$1" \
221244
bash -c "$commands" 2>&1
222245

246+
# Spawn a nginx container named nginx-custom-label, with a custom label.
247+
custom_labeled_nginx2_cid="$(docker run --rm -d \
248+
--name "$nginx_cstm_lbl" \
249+
--label custom.label.nginx \
250+
nginx:alpine)"
251+
252+
# Spawn a "fake docker-gen" container named docker-gen-custom-label, with a custom label.
253+
custom_labeled_docker_gen_cid="$(docker run --rm -d \
254+
--name "$docker_gen_cstm_lbl" \
255+
--label custom.label.docker-gen \
256+
nginx:alpine)"
257+
258+
# This should target the nginx container whose id or name was obtained with
259+
# the custom label (nginx-custom-label)
260+
# and the docker-gen container whose id or name was obtained with
261+
# the custom label (docker-gen-custom-label)
262+
docker run --rm \
263+
-v /var/run/docker.sock:/var/run/docker.sock:ro \
264+
--volumes-from "$nginx_vol" \
265+
-e "NGINX_PROXY_CONTAINER_LABEL=custom.label.nginx" \
266+
-e "NGINX_DOCKER_GEN_CONTAINER_LABEL=custom.label.docker-gen" \
267+
"$1" \
268+
bash -c "$commands" 2>&1
269+
223270
cat > "${GITHUB_WORKSPACE}/test/tests/docker_api/expected-std-out.txt" <<EOF
224271
Container $docker_gen received signal 1
225272
Container $nginx_vol received signal 1
@@ -246,7 +293,12 @@ Container $docker_gen_lbl received signal 1
246293
Container $nginx_lbl received signal 1
247294
$labeled_docker_gen_cid
248295
$labeled_nginx2_cid
296+
Container $docker_gen_cstm_lbl received signal 1
297+
Container $nginx_cstm_lbl received signal 1
298+
$custom_labeled_docker_gen_cid
299+
$custom_labeled_nginx2_cid
249300
EOF
250301
;;
251302

252303
esac
304+
sleep 60

0 commit comments

Comments
 (0)