Skip to content

Instantly share code, notes, and snippets.

@JoshuaGross
Last active April 19, 2017 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaGross/a36ccc1a77d581e1daa143193a25535a to your computer and use it in GitHub Desktop.
Save JoshuaGross/a36ccc1a77d581e1daa143193a25535a to your computer and use it in GitHub Desktop.
Install and configure DogStatsD to run container-local

Installing

Add this line to a Dockerfile to install and configure DogStatsD in the container. Requires that apt-get is available on the system.

This script will install curl in your environment but not remove it; you should uninstall it if you don't want it available in the container.

These script uses md5sum to validate all external scripts that are downloaded, to prevent tampering or any other unexpected changes.

# BEGIN install local dogstatsd agent
# From: https://gist.github.com/JoshuaGross/a36ccc1a77d581e1daa143193a25535a
ENV DOCKER_DD_AGENT=yes AGENT_VERSION=1:5.12.3-1
RUN (/bin/bash -c "apt-get update && apt-get install -y curl && (curl https://gist.githubusercontent.com/JoshuaGross/a36ccc1a77d581e1daa143193a25535a/raw/5a9e747bd2292a5c43ae3cf1e40b5c822f55da86/dogstatsd-install.sh > dogstatsd-install.sh) && EXPECTED=\"88973ab95b97099f3274398f9ce59210\" && ACTUAL=(\$(md5sum dogstatsd-install.sh)) && (if [[ \"\$ACTUAL\" != \"\$EXPECTED\" ]]; then echo \"Incorrect md5 checksum for dogstatsd-install.sh, failing. Expected: \$EXPECTED. Actual: \$ACTUAL\"; exit 1; fi) && chmod +x dogstatsd-install.sh && ./dogstatsd-install.sh && rm dogstatsd-install.sh")
# END install local dogstatsd agent

md5 hash of dogstatsd-install.sh: 88973ab95b97099f3274398f9ce59210

md5 hash of entrypoint.sh: 833c48d970c84d4813dec4da0092c45a

md5 hash of supervisor.conf: f7273b70b86d2cc0e40745fb3c77a42b

Running

You must then start the dd-agent with a start script. It's recommended to start your own application in the same way:

#!/usr/bin/env bash
set -e

# Start the DataDog agent
echo "Start DogStatsD..."
/etc/dd-agent/entrypoint.sh supervisord -n -c /etc/dd-agent/supervisor.conf &

# Start app
echo "Start app..."
my-app &

# Don't exit until both other subprocesses have exited
# TODO: race them
wait
#!/usr/bin/env bash
set -e
# Much of this is cribbed from docker-dd-agent:
# https://github.com/DataDog/docker-dd-agent/blob/master/dogstatsd/Dockerfile
echo "Installing datadog-agent..."
echo "deb http://apt.datadoghq.com/ stable main" > /etc/apt/sources.list.d/datadog.list \
&& apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C7A7DA52 \
&& apt-get update \
&& apt-get install --no-install-recommends -y datadog-agent \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
|| exit 1
echo "Done installing datadog-agent"
# Use custom supervisor.conf, entrypoint
echo "Downloading entrypoint..."
curl https://raw.githubusercontent.com/DataDog/docker-dd-agent/0498c1aefa6c187d1ee455689074247aefa884f7/dogstatsd/entrypoint.sh > /etc/dd-agent/entrypoint.sh || exit 1
echo "Done downloading entrypoint"
echo "Checking entrypoint hash..."
EXPECTED="833c48d970c84d4813dec4da0092c45a"
ACTUAL=($(md5sum /etc/dd-agent/entrypoint.sh))
if [[ "$ACTUAL" != "$EXPECTED" ]];
then
echo "Wrong md5 for /etc/dd-agent/entrypoint.sh, failing. Expected: $EXPECTED. Actual: $ACTUAL"
exit 1
fi
echo "Done checking entrypoint hash"
echo "Downloading supervisor conf..."
curl https://raw.githubusercontent.com/DataDog/docker-dd-agent/0498c1aefa6c187d1ee455689074247aefa884f7/dogstatsd/supervisor.conf > /etc/dd-agent/supervisor.conf || exit 1
echo "Done downloading supervisor conf"
echo "Checking supervisor conf hash..."
EXPECTED="f7273b70b86d2cc0e40745fb3c77a42b"
ACTUAL=($(md5sum /etc/dd-agent/supervisor.conf))
if [[ "$ACTUAL" != "$EXPECTED" ]];
then
echo "Wrong md5 for /etc/dd-agent/supervisor.conf, failing. Expected: $EXPECTED. Actual: $ACTUAL"
exit 1
fi
echo "Done checking supervisor conf hash"
chmod +x /etc/dd-agent/entrypoint.sh
# 1. Turn syslog off
# 2. Use custom datadog.conf
echo "Using custom datadog.conf..."
mv /etc/dd-agent/datadog.conf.example /etc/dd-agent/datadog.conf \
&& sed -i -e"s/^.*log_to_syslog:.*$/log_to_syslog: no/" /etc/dd-agent/datadog.conf \
|| exit 1
echo "Done using custom datadog.conf"
# Set proper permissions to allow running as a non-root user
echo "Setting permissions..."
chmod g+w /etc/dd-agent/datadog.conf \
&& chmod -R g+w /var/log/datadog \
&& chmod g+w /etc/dd-agent \
&& chmod g+w /opt/datadog-agent/run/ \
|| exit 1
echo "Done setting permissions"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment