From f46a2b6ee151f8e5abdb446a21b8ad0fc603f764 Mon Sep 17 00:00:00 2001 From: Marcus Young Date: Tue, 12 Dec 2017 06:29:20 -0600 Subject: [PATCH 1/3] Modify to work with AWS ECS --- app/entrypoint.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/entrypoint.sh b/app/entrypoint.sh index 94fe0c92..94d6ec01 100755 --- a/app/entrypoint.sh +++ b/app/entrypoint.sh @@ -1,9 +1,13 @@ #!/bin/bash -# shellcheck disable=SC2155 +# shellcheck disable=SC2155,SC2002 -set -u +set -e -export CONTAINER_ID=$(cat /proc/self/cgroup | sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' | head -n 1) +if [[ -n "${DOCKER_PROVIDER}" ]] && [[ "${DOCKER_PROVIDER,,}" == "aws" ]]; then + export CONTAINER_ID=$(cat "${ECS_CONTAINER_METADATA_FILE}" | grep ContainerID | sed 's/.*: "\(.*\)",/\1/g') +else + export CONTAINER_ID=$(cat /proc/self/cgroup | sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' | head -n 1) +fi if [[ -z "$CONTAINER_ID" ]]; then echo "Error: can't get my container ID !" >&2 From 61b2f29ded75ad01a1b07b8a90fa2bdbe181a39e Mon Sep 17 00:00:00 2001 From: Marcus Young Date: Tue, 12 Dec 2017 10:49:59 -0600 Subject: [PATCH 2/3] Minor PR tweaks --- app/entrypoint.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/entrypoint.sh b/app/entrypoint.sh index 94d6ec01..f4a6f287 100755 --- a/app/entrypoint.sh +++ b/app/entrypoint.sh @@ -1,12 +1,12 @@ #!/bin/bash -# shellcheck disable=SC2155,SC2002 +# shellcheck disable=SC2155 -set -e +set -u -if [[ -n "${DOCKER_PROVIDER}" ]] && [[ "${DOCKER_PROVIDER,,}" == "aws" ]]; then - export CONTAINER_ID=$(cat "${ECS_CONTAINER_METADATA_FILE}" | grep ContainerID | sed 's/.*: "\(.*\)",/\1/g') +if [[ -n "${DOCKER_PROVIDER:-}" ]] && [[ "${DOCKER_PROVIDER,,}" == "aws" ]]; then + export CONTAINER_ID=$(grep ContainerID "${ECS_CONTAINER_METADATA_FILE}" | sed 's/.*: "\(.*\)",/\1/g') else - export CONTAINER_ID=$(cat /proc/self/cgroup | sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' | head -n 1) + export CONTAINER_ID=$(sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' /proc/self/cgroup | head -n 1) fi if [[ -z "$CONTAINER_ID" ]]; then From 7ffa68161e74fda24d041d600b02c8664c023fb6 Mon Sep 17 00:00:00 2001 From: Marcus Young Date: Sat, 23 Dec 2017 12:30:31 -0600 Subject: [PATCH 3/3] Alternate way to set for AWS ECS --- README.md | 4 ++++ app/entrypoint.sh | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5c552474..ac7b736d 100644 --- a/README.md +++ b/README.md @@ -215,6 +215,10 @@ $ docker run -d \ * `ACME_TOS_HASH` - Let´s you pass an alternative TOS hash to simp_le, to support other CA´s ACME implentation. +* `DOCKER_PROVIDER` - Set this to change behavior on container ID retrieval. Optional. Current supported values: + * No value (empty, not set): no change in behavior. + * `ecs` [Amazon ECS using ECS_CONTAINER_METADATA_FILE environment variable](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-metadata.html) + #### Examples: If you want other examples how to use this container, look at: diff --git a/app/entrypoint.sh b/app/entrypoint.sh index f4a6f287..10e0400d 100755 --- a/app/entrypoint.sh +++ b/app/entrypoint.sh @@ -3,11 +3,22 @@ set -u -if [[ -n "${DOCKER_PROVIDER:-}" ]] && [[ "${DOCKER_PROVIDER,,}" == "aws" ]]; then - export CONTAINER_ID=$(grep ContainerID "${ECS_CONTAINER_METADATA_FILE}" | sed 's/.*: "\(.*\)",/\1/g') -else - export CONTAINER_ID=$(sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' /proc/self/cgroup | head -n 1) -fi +DOCKER_PROVIDER=${DOCKER_PROVIDER:-docker} + +case "${DOCKER_PROVIDER}" in +ecs|ECS) + # AWS ECS. Enabled in /etc/ecs/ecs.config (http://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-metadata.html) + if [[ -n "${ECS_CONTAINER_METADATA_FILE:-}" ]]; then + export CONTAINER_ID=$(grep ContainerID "${ECS_CONTAINER_METADATA_FILE}" | sed 's/.*: "\(.*\)",/\1/g') + else + echo "${DOCKER_PROVIDER} specified as 'ecs' but not available. See: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-metadata.html" + exit 1 + fi + ;; +*) + export CONTAINER_ID=$(sed -nE 's/^.+docker[\/-]([a-f0-9]{64}).*/\1/p' /proc/self/cgroup | head -n 1) + ;; +esac if [[ -z "$CONTAINER_ID" ]]; then echo "Error: can't get my container ID !" >&2