Skip to content

Commit

Permalink
add Dockerfile that allows configuration through environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Pankonen committed Jun 16, 2017
1 parent 33d45a7 commit cbbd64b
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM alpine:3.4

RUN apk --update upgrade \
&& apk add curl ca-certificates \
&& update-ca-certificates \
&& rm -rf /var/cache/apk/*

COPY ldr /usr/bin/
COPY docker-entrypoint.sh /usr/bin/

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 8030
CMD ["ldr"]
7 changes: 7 additions & 0 deletions Dockerfile-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:3.4

RUN apk --update upgrade \
&& apk add go godep \
&& rm -rf /var/cache/apk/*

WORKDIR /build
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,57 @@ We have done extensive load tests on the relay proxy in AWS / EC2. We have also

* If using an Elastic Load Balancer in front of the relay proxy, you may need to [pre-warm](https://aws.amazon.com/articles/1636185810492479) the load balancer whenever connections to the relay proxy are cycled. This might happen when you deploy a large number of new servers that connect to the proxy, or upgrade the relay proxy itself.


Docker
-------

To build the ld-relay container:

$ docker build -t ld-relay-build -f Dockerfile-build . # create the build container image
$ docker run -v $(pwd):/build -t -i -e CGO_ENABLED=0 -e GOOS=linux ld-relay-build godep go build -a -installsuffix cgo -o ldr # create the ldr binary
$ docker build -t ld-relay . # build the ld-relay container image
$ docker rmi ld-relay-build # remove the build container image that is no longer needed

To run a single environment, without Redis:

$ docker run --name ld-relay -e LD_ENV_test="sdk-test-apiKey" ld-relay

To run multiple environments, without Redis:

$ docker run --name ld-relay -e LD_ENV_test="sdk-test-apiKey" -e LD_ENV_prod="sdk-prod-apiKey" ld-relay

To run a single environment, with Redis:

$ docker run --name redis redis:alpine
$ docker run --name ld-relay --link redis:redis -e USE_REDIS=1 -e LD_ENV_test="sdk-test-apiKey" ld-relay

To run multiple environment, with Redis:

$ docker run --name redis redis:alpine
$ docker run --name ld-relay --link redis:redis -e USE_REDIS=1 -e LD_ENV_test="sdk-test-apiKey" -e LD_PREFIX_test="ld:default:test" -e LD_ENV_prod="sdk-prod-apiKey" -e LD_PREFIX_prod="ld:default:prod" ld-relay


Docker Environment Variables
-------

`LD_ENV_${environment}`: At least one `LD_ENV_${environment}` variable is recommended. The value should be the api key for that specific environment. Multiple environments can be listed

`LD_PREFIX_${environment}`: This variable is optional. Configures a Redis prefix for that specific environment. Multiple environments can be listed

`USE_REDIS`: This variable is optional. If set to 1, Redis configuration will be added

`REDIS_HOST`: This variable is optional. Sets the hostname of the Redis server. If linked to a redis container that sets `REDIS_PORT` to `tcp://172.17.0.2:6379`, `REDIS_HOST` will use this value as the default. If not, the default value is `redis`

`REDIS_PORT`: This variable is optional. Sets the port of the Redis server. If linked to a redis container that sets `REDIS_PORT` to `REDIS_PORT=tcp://172.17.0.2:6379`, `REDIS_PORT` will use this value as the default. If not, the defualt value is `6379`

`REDIS_TTL`: This variable is optional. Sets the TTL in milliseconds, defaults to `30000`

`USE_EVENTS`: This variable is optional. If set to 1, enables event buffering

`EVENTS_HOST`: This variable is optional. URI of the LaunchDarkly events endpoint, defaults to `https://events.launchdarkly.com`

`EVENTS_SEND`: This variable is optional. Defaults to `true`

`EVENTS_FLUSH_INTERVAL`: This variable is optional. Sets how often events are flushed, defaults to `5` (seconds)

`EVENTS_SAMPLING_INTERVAL`: This variable is optional. Defaults to `10000`

14 changes: 13 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
machine:
services:
- docker

dependencies:
pre:
- rm -rf ~/.go_workspace/src/github.com/launchdarkly/ld-relay
Expand All @@ -17,9 +21,17 @@ dependencies:
test:
override:
- godep go test
- docker build -t ld-relay-build -f Dockerfile-build .
- docker run --rm -v `pwd`:/build -t -i -e CGO_ENABLED=0 -e GOOS=linux ld-relay-build godep go build -a -installsuffix cgo -o ldr
- docker build -t "ld-relay:$CIRCLE_BUILD_NUM" .

deployment:
s3:
branch:
branch: /.*/
commands:
- ./scripts/upload_to_dockerhub.sh
- ./scripts/upload_to_s3.sh
tag:
tag: /.*/
commands:
- ./scripts/upload_to_dockerhub.sh
62 changes: 62 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/sh
if [ ! -f /etc/ld-relay.conf ]; then
# only create /etc/ld-relay.conf if it doesn't exist already

if ! env | grep -q LD_ENV_ ; then
echo "WARNING: at least one LD_ENV_ should be set" >&2
fi

echo "
[main]
streamUri = \"${STREAM_URI:-https://stream.launchdarkly.com}\"
baseUri = \"${BASE_URI:-https://app.launchdarkly.com}\"
exitOnError = ${EXIT_ON_ERROR:-false}
port = ${PORT:-8030}
heartbeatIntervalSecs = ${HEARTBEAT_INTERVAL:-15}
" > /etc/ld-relay.conf

if [ "$USE_REDIS" = 1 ]; then
if echo "$REDIS_PORT" | grep -q 'tcp://'; then
# REDIS_PORT gets set to tcp://$docker_ip:6379 when linking to a redis container
# default to using those values if they exist
REDIS_HOST_PART="${REDIS_PORT%:*}"
REDIS_HOST="${REDIS_HOST_PART##*/}"
REDIS_PORT="${REDIS_PORT##*:}"
fi

echo "
[redis]
host = \"${REDIS_HOST:-redis}\"
port = ${REDIS_PORT:-6379}
localTtl = ${REDIS_TTL:-30000}
" >> /etc/ld-relay.conf
fi

if [ "$USE_EVENTS" = 1 ]; then
echo "
[events]
eventsUri = \"${EVENTS_HOST:-https://events.launchdarkly.com}\"
sendEvents = ${EVENTS_SEND:-true}
flushIntervalSecs = ${EVENTS_FLUSH_INTERVAL:-5}
samplingInterval = ${EVENTS_SAMPLING_INTERVAL:-0}
capacity = ${EVENTS_CAPACITY:-10000}
" >> /etc/ld-relay.conf
fi

for environment in $(env | grep LD_ENV_ ); do
env_name="$(echo "$environment" | sed 's/^LD_ENV_//' | cut -d'=' -f1)"
env_key="$(eval echo "\$$(echo "$environment" | cut -d'=' -f1)")"
env_prefix="$(eval echo "\$LD_PREFIX_${env_name}")"

echo "
[environment \"$env_name\"]
apiKey = \"$env_key\"" >> /etc/ld-relay.conf

if [ -n "$env_prefix" ]; then
echo "prefix = \"$env_prefix\"" >> /etc/ld-relay.conf
fi
done

fi

exec "$@"
30 changes: 30 additions & 0 deletions scripts/upload_to_dockerhub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

set -e

if [ -n "$CIRCLE_TAG" ]; then
TAG="$CIRCLE_TAG"
elif
TAG="$CIRCLE_BRANCH"
fi

if [ -z "$TAG" ]; then
echo "Skipping: unable to determine branch or tag"
exit
fi

if [ -z "$DOCKER_USERNAME" ] || [ -z "$DOCKER_PASSWORD" ] || [ -z "$DOCKER_REPO" ]; then
echo "Skipping: DOCKER_USERNAME, DOCKER_PASSWORD, DOCKER_REPO environment variables not set"
exit
fi

# dockerhub requires an email address, but doesn't use it for anything
echo "[email protected]" | docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
docker tag "ld-relay:$CIRCLE_BUILD_NUM" "$DOCKER_REPO:$TAG"
docker push "$DOCKER_REPO:$TAG"

if [ "$TAG" = "master" ]; then
# tag the master branch as latest
docker tag "$DOCKER_REPO:$TAG" "$DOCKER_REPO:latest"
docker push "$DOCKER_REPO:latest"
fi
4 changes: 4 additions & 0 deletions scripts/upload_to_s3.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#/bin/bash
set -e
if [ -z "$bucket" ]; then
echo "Missing S3 credentials, skipping upload"
exit
fi
REV=$(git rev-parse HEAD | cut -c1-6)
VERSION=2.2.0.${REV}
ls ${CIRCLE_ARTIFACTS}/${VERSION}
Expand Down

0 comments on commit cbbd64b

Please sign in to comment.