Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,16 @@ if [[ "$*" == "/bin/bash /app/start.sh" ]]; then
echo "Error: can't get nginx-proxy container ID !" >&2
echo "Check that you are doing one of the following :" >&2
echo -e "\t- Use the --volumes-from option to mount volumes from the nginx-proxy container." >&2
echo -e "\t- Set the NGINX_PROXY_CONTAINER env var on the letsencrypt-companion container to the name of the nginx-proxy container." >&2
echo -e "\t- Label the nginx-proxy container to use with 'com.github.nginx-proxy.nginx'." >&2
echo -e "\t- Set the NGINX_PROXY_CONTAINER env var on the acme-companion container to the name of the nginx-proxy container." >&2
echo -e "\t- Label the nginx-proxy container to use with '${default_nginx_proxy_container_label}'." >&2
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
exit 1
elif [[ -z "$(get_docker_gen_container)" ]] && ! is_docker_gen_container "$(get_nginx_proxy_container)"; then
echo "Error: can't get docker-gen container id !" >&2
echo "If you are running a three containers setup, check that you are doing one of the following :" >&2
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
echo -e "\t- Label the docker-gen container to use with 'com.github.nginx-proxy.docker-gen'." >&2
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
echo -e "\t- Label the docker-gen container to use with '${default_nginx_docker_gen_container_label}'." >&2
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
exit 1
fi
check_writable_directory '/etc/nginx/certs'
Expand Down
9 changes: 7 additions & 2 deletions app/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ if [[ "$DEBUG" == true ]]; then
DEBUG=1 && export DEBUG
fi

default_nginx_docker_gen_container_label=com.github.nginx-proxy.docker-gen
default_nginx_proxy_container_label=com.github.nginx-proxy.nginx
runtime_nginx_docker_gen_container_label=${NGINX_DOCKER_GEN_CONTAINER_LABEL:-$default_nginx_docker_gen_container_label}
runtime_nginx_proxy_container_label=${NGINX_PROXY_CONTAINER_LABEL:-$default_nginx_proxy_container_label}

function parse_true() {
case "$1" in

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

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

# If the labeled_cid function dit not return anything ...
Expand Down
23 changes: 23 additions & 0 deletions docs/Getting-containers-IDs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ There are three methods to inform the **acme-companion** container of the **ngin

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

* 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.

* `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.

* `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.
Expand All @@ -14,6 +16,8 @@ And two methods to inform the **acme-companion** container of the **docker-gen**

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

* 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.

* `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.

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**.
Expand All @@ -39,6 +43,25 @@ $ docker run --detach \
nginxproxy/acme-companion
```

`label` method with custom label.
```
$ docker run --detach \
[...]
--label custom.label.nginx \
nginx

$ docker run --detach \
[...]
--label custom.label.docker-gen \
nginxproxy/docker-gen

$ docker run --detach \
[...]
--env NGINX_PROXY_CONTAINER_LABEL=custom.label.nginx
--env NGINX_DOCKER_GEN_CONTAINER_LABEL=custom.label.docker-gen
nginxproxy/acme-companion
```

`environment variable` method
```
$ docker run --detach \
Expand Down
52 changes: 52 additions & 0 deletions test/tests/docker_api/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
nginx_vol='nginx-volumes-from'
nginx_env='nginx-env-var'
nginx_lbl='nginx-label'
nginx_cstm_lbl='nginx-custom-label'
docker_gen='docker-gen-no-label'
docker_gen_lbl='docker-gen-label'
docker_gen_cstm_lbl='docker-gen-custom-label'

case $SETUP in

Expand All @@ -20,6 +22,7 @@ case $SETUP in
"$nginx_vol" \
"$nginx_env" \
"$nginx_lbl" \
"$nginx_cstm_lbl" \
&> /dev/null
}
trap cleanup EXIT
Expand Down Expand Up @@ -78,13 +81,31 @@ case $SETUP in
"$1" \
bash -c "$commands" 2>&1

# Run a nginx-proxy container named nginx-custom-label, with a custom label.
# Store the container id in the custom_labeled_nginx_cid variable.
custom_labeled_nginx_cid="$(docker run --rm -d \
--name "$nginx_cstm_lbl" \
-v /var/run/docker.sock:/tmp/docker.sock:ro \
--label custom.label.nginx \
nginxproxy/nginx-proxy)"

# This should target the nginx-proxy container with the custom label (nginx-custom-label)
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
--volumes-from "$nginx_vol" \
-e "NGINX_PROXY_CONTAINER_LABEL=custom.label.nginx" \
"$1" \
bash -c "$commands" 2>&1

cat > "${GITHUB_WORKSPACE}/test/tests/docker_api/expected-std-out.txt" <<EOF
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
$nginx_vol
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
$nginx_env
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
$labeled_nginx_cid
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
$custom_labeled_nginx_cid
EOF
;;

Expand All @@ -98,8 +119,10 @@ EOF
"$nginx_vol" \
"$nginx_env" \
"$nginx_lbl" \
"$nginx_cstm_lbl" \
"$docker_gen" \
"$docker_gen_lbl" \
"$docker_gen_cstm_lbl" \
&> /dev/null
}
trap cleanup EXIT
Expand Down Expand Up @@ -220,6 +243,30 @@ EOF
"$1" \
bash -c "$commands" 2>&1

# Spawn a nginx container named nginx-custom-label, with a custom label.
custom_labeled_nginx2_cid="$(docker run --rm -d \
--name "$nginx_cstm_lbl" \
--label custom.label.nginx \
nginx:alpine)"

# Spawn a "fake docker-gen" container named docker-gen-custom-label, with a custom label.
custom_labeled_docker_gen_cid="$(docker run --rm -d \
--name "$docker_gen_cstm_lbl" \
--label custom.label.docker-gen \
nginx:alpine)"

# This should target the nginx container whose id or name was obtained with
# the custom label (nginx-custom-label)
# and the docker-gen container whose id or name was obtained with
# the custom label (docker-gen-custom-label)
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
--volumes-from "$nginx_vol" \
-e "NGINX_PROXY_CONTAINER_LABEL=custom.label.nginx" \
-e "NGINX_DOCKER_GEN_CONTAINER_LABEL=custom.label.docker-gen" \
"$1" \
bash -c "$commands" 2>&1

cat > "${GITHUB_WORKSPACE}/test/tests/docker_api/expected-std-out.txt" <<EOF
Container $docker_gen received signal 1
Container $nginx_vol received signal 1
Expand All @@ -246,7 +293,12 @@ Container $docker_gen_lbl received signal 1
Container $nginx_lbl received signal 1
$labeled_docker_gen_cid
$labeled_nginx2_cid
Container $docker_gen_cstm_lbl received signal 1
Container $nginx_cstm_lbl received signal 1
$custom_labeled_docker_gen_cid
$custom_labeled_nginx2_cid
EOF
;;

esac
sleep 60
Loading