diff --git a/app/entrypoint.sh b/app/entrypoint.sh index 7f0c7a7a..b2a37442 100755 --- a/app/entrypoint.sh +++ b/app/entrypoint.sh @@ -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' diff --git a/app/functions.sh b/app/functions.sh index 2fc8fec7..15e66b1b 100644 --- a/app/functions.sh +++ b/app/functions.sh @@ -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 @@ -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. @@ -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 ... diff --git a/docs/Getting-containers-IDs.md b/docs/Getting-containers-IDs.md index b998571f..8461f509 100644 --- a/docs/Getting-containers-IDs.md +++ b/docs/Getting-containers-IDs.md @@ -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. @@ -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**. @@ -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 \ diff --git a/test/tests/docker_api/run.sh b/test/tests/docker_api/run.sh index ecb5a9a7..2a4e6fc0 100755 --- a/test/tests/docker_api/run.sh +++ b/test/tests/docker_api/run.sh @@ -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 @@ -20,6 +22,7 @@ case $SETUP in "$nginx_vol" \ "$nginx_env" \ "$nginx_lbl" \ + "$nginx_cstm_lbl" \ &> /dev/null } trap cleanup EXIT @@ -78,6 +81,22 @@ 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" < /dev/null } trap cleanup EXIT @@ -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" <