Skip to content

Commit 27d433c

Browse files
Helder CorreiaJrCs
authored andcommitted
Complete support for dynamic container names (#231)
* Allow setting NGINX_DOCKER_GEN_CONTAINER from a label * Find labeled cid in runtime instead of startup time
1 parent 1f678ed commit 27d433c

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ $ docker run -d -p 80:80 -p 443:443 \
3030
-v /etc/nginx/vhost.d \
3131
-v /usr/share/nginx/html \
3232
-v /var/run/docker.sock:/tmp/docker.sock:ro \
33-
--label com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true \
33+
--label com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy \
3434
jwilder/nginx-proxy
3535
```
36-
The "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true" label is needed so that the letsencrypt container knows which nginx proxy container to use.
36+
The "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy" label is needed so that the letsencrypt container knows which nginx proxy container to use.
3737

3838
* Second start this container:
3939
```bash
@@ -63,7 +63,7 @@ To run nginx proxy as a separate container you'll need:
6363
curl https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl > /path/to/nginx.tmpl
6464
```
6565

66-
2) Set the `NGINX_DOCKER_GEN_CONTAINER` environment variable to the name or id of the docker-gen container.
66+
2) Use the `com.github.jrcs.letsencrypt_nginx_proxy_companion.docker_gen` label on the docker-gen container, or explicitly set the `NGINX_DOCKER_GEN_CONTAINER` environment variable to the name or id of that container.
6767

6868
Examples:
6969

@@ -75,7 +75,7 @@ $ docker run -d -p 80:80 -p 443:443 \
7575
-v /etc/nginx/vhost.d \
7676
-v /usr/share/nginx/html \
7777
-v /path/to/certs:/etc/nginx/certs:ro \
78-
--label com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true \
78+
--label com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy \
7979
nginx
8080
```
8181

@@ -86,23 +86,25 @@ $ docker run -d \
8686
--volumes-from nginx \
8787
-v /path/to/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro \
8888
-v /var/run/docker.sock:/tmp/docker.sock:ro \
89+
--label com.github.jrcs.letsencrypt_nginx_proxy_companion.docker_gen \
8990
jwilder/docker-gen \
9091
-notify-sighup nginx -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
9192
```
9293

93-
* Then start this container (NGINX_DOCKER_GEN_CONTAINER variable must contain the docker-gen container name or id):
94+
* Then start this container:
9495
```bash
9596
$ docker run -d \
9697
--name nginx-letsencrypt \
97-
-e "NGINX_DOCKER_GEN_CONTAINER=nginx-gen" \
9898
--volumes-from nginx \
9999
-v /path/to/certs:/etc/nginx/certs:rw \
100100
-v /var/run/docker.sock:/var/run/docker.sock:ro \
101101
jrcs/letsencrypt-nginx-proxy-companion
102102
```
103-
Then start any containers to be proxied as described previously.
104103

105-
* If for some reason you can't use the docker --volumes-from option, you can specify the name or id of the nginx container with `NGINX_PROXY_CONTAINER` variable.
104+
* Then start any containers to be proxied as described previously.
105+
106+
Note: If the docker-gen container name is static and you want to explicitly set it, use `-e NGINX_DOCKER_GEN_CONTAINER=nginx-gen`. The same thing is true with the nginx container (`-e NGINX_PROXY_CONTAINER=nginx`).
107+
106108

107109
#### Let's Encrypt
108110

@@ -161,7 +163,9 @@ $ docker run -d \
161163

162164
* `REUSE_KEY` - Set it to `true` to make simp_le reuse previously generated private key instead of creating a new one on certificate renewal. Recommended if you intend to use HPKP.
163165

164-
* The "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true" label - set this label on the nginx-proxy container to tell the docker-letsencrypt-nginx-proxy-companion container to use it as the proxy.
166+
* The "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy" label - set this label on the nginx-proxy container to tell the docker-letsencrypt-nginx-proxy-companion container to use it as the proxy.
167+
168+
* The "com.github.jrcs.letsencrypt_nginx_proxy_companion.docker_gen" label - set this label on the docker-gen container to tell the docker-letsencrypt-nginx-proxy-companion container to use it as the docker-gen when it's split from nginx (separate containers).
165169

166170
* `ACME_TOS_HASH` - Let´s you pass an alternative TOS hash to simp_le, to support other CA´s ACME implentation.
167171

@@ -172,5 +176,5 @@ If you want other examples how to use this container, look at:
172176
* [Evert Ramos's Examples](https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion) - using docker-compose version '3'
173177
* [Karl Fathi's Examples](https://github.com/fatk/docker-letsencrypt-nginx-proxy-companion-examples)
174178
* [More examples from Karl](https://github.com/pixelfordinner/pixelcloud-docker-apps/tree/master/nginx-proxy)
175-
* [George Ilyes' Examples](https://github.com/gilyes/docker-nginx-letsencrypt-sample)
179+
* [George Ilyes' Examples](https://github.com/gilyes/docker-nginx-letsencrypt-sample)
176180
* [Dmitry's simple docker-compose example](https://github.com/dmitrym0/simple-lets-encrypt-docker-compose-sample)

app/entrypoint.sh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ function get_nginx_proxy_cid {
3333
break
3434
fi
3535
done
36-
# Check if any container has been labelled as the nginx proxy container.
37-
local labeled_cid=$(docker_api "/containers/json" | jq -r '.[] | select( .Labels["com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy"] == "true")|.Id')
38-
if [[ ! -z "${labeled_cid:-}" ]]; then
39-
export NGINX_PROXY_CONTAINER=$labeled_cid
40-
fi
41-
if [[ -z "${NGINX_PROXY_CONTAINER:-}" ]]; then
36+
if [[ -z "$(nginx_proxy_container)" ]]; then
4237
echo "Error: can't get nginx-proxy container id !" >&2
4338
echo "Check that you use the --volumes-from option to mount volumes from the nginx-proxy or label the nginx proxy container to use with 'com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true'." >&2
4439
exit 1
@@ -79,7 +74,7 @@ source /app/functions.sh
7974

8075
if [[ "$*" == "/bin/bash /app/start.sh" ]]; then
8176
check_docker_socket
82-
if [[ -z "${NGINX_DOCKER_GEN_CONTAINER:-}" ]]; then
77+
if [[ -z "$(docker_gen_container)" ]]; then
8378
[[ -z "${NGINX_PROXY_CONTAINER:-}" ]] && get_nginx_proxy_cid
8479
fi
8580
check_writable_directory '/etc/nginx/certs'

app/functions.sh

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,37 @@ function docker_kill {
6767
docker_api "/containers/$id/kill?signal=$signal" "POST"
6868
}
6969

70+
function labeled_cid {
71+
docker_api "/containers/json" | jq -r '.[] | select(.Labels["'$1'"])|.Id'
72+
}
73+
74+
function docker_gen_container {
75+
echo ${NGINX_DOCKER_GEN_CONTAINER:-$(labeled_cid com.github.jrcs.letsencrypt_nginx_proxy_companion.docker_gen)}
76+
}
77+
78+
function nginx_proxy_container {
79+
echo ${NGINX_PROXY_CONTAINER:-$(labeled_cid com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy)}
80+
}
81+
7082
## Nginx
7183
reload_nginx() {
72-
if [[ -n "${NGINX_DOCKER_GEN_CONTAINER:-}" ]]; then
84+
local _docker_gen_container=$(docker_gen_container)
85+
local _nginx_proxy_container=$(nginx_proxy_container)
86+
87+
if [[ -n "${_docker_gen_container:-}" ]]; then
7388
# Using docker-gen and nginx in separate container
74-
echo "Reloading nginx docker-gen (using separate container ${NGINX_DOCKER_GEN_CONTAINER})..."
75-
docker_kill "$NGINX_DOCKER_GEN_CONTAINER" SIGHUP
76-
if [[ -n "${NGINX_PROXY_CONTAINER:-}" ]]; then
89+
echo "Reloading nginx docker-gen (using separate container ${_docker_gen_container})..."
90+
docker_kill "${_docker_gen_container}" SIGHUP
91+
92+
if [[ -n "${_nginx_proxy_container:-}" ]]; then
7793
# Reloading nginx in case only certificates had been renewed
78-
echo "Reloading nginx (using separate container ${NGINX_PROXY_CONTAINER})..."
79-
docker_kill "$NGINX_PROXY_CONTAINER" SIGHUP
94+
echo "Reloading nginx (using separate container ${_nginx_proxy_container})..."
95+
docker_kill "${_nginx_proxy_container}" SIGHUP
8096
fi
8197
else
82-
if [[ -n "${NGINX_PROXY_CONTAINER:-}" ]]; then
83-
echo "Reloading nginx proxy..."
84-
docker_exec "$NGINX_PROXY_CONTAINER" \
98+
if [[ -n "${_nginx_proxy_container:-}" ]]; then
99+
echo "Reloading nginx proxy (${_nginx_proxy_container})..."
100+
docker_exec "${_nginx_proxy_container}" \
85101
'[ "sh", "-c", "/usr/local/bin/docker-gen -only-exposed /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload" ]'
86102
fi
87103
fi

0 commit comments

Comments
 (0)