Skip to content

Commit

Permalink
docker: major rewrite following grafana/grafana-docker#146
Browse files Browse the repository at this point in the history
close #31 #32
  • Loading branch information
fg2it committed Apr 13, 2018
1 parent 0878851 commit d6e3eb0
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 98 deletions.
47 changes: 30 additions & 17 deletions docker/v4.x/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
ARG RELEASE=jessie
ARG RELEASE=stretch
FROM arm32v7/debian:${RELEASE}-slim

ARG GF_UID="472"
ARG GF_GID="472"

ARG REPO_TAG
ARG PKG_NAME
ARG BUILD_DATE
Expand All @@ -11,26 +14,36 @@ LABEL org.label-schema.build-date=$BUILD_DATE
LABEL org.label-schema.vcs-url="https://github.com/fg2it/grafana-on-raspberry"
LABEL org.label-schema.vcs-ref=$VCS_REF


RUN apt-get update && \
apt-get -y --no-install-recommends install libfontconfig curl ca-certificates && \
apt-get clean && \
curl -o /tmp/grafana.deb -L https://github.com/fg2it/grafana-on-raspberry/releases/download/${REPO_TAG}/grafana_${PKG_NAME}_armhf.deb && \
curl -o /usr/sbin/gosu -fsSL "https://github.com/tianon/gosu/releases/download/1.10/gosu-$(dpkg --print-architecture)" && \
chmod +x /usr/sbin/gosu && \
apt-get remove -y curl && \
apt-get autoremove -y && \
ENV GRAFANA_URL="https://github.com/fg2it/grafana-on-raspberry/releases/download/${REPO_TAG}/grafana-${PKG_NAME}.linux-armhf.tar.gz" \
PATH=/usr/share/grafana/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
GF_PATHS_CONFIG="/etc/grafana/grafana.ini" \
GF_PATHS_DATA="/var/lib/grafana" \
GF_PATHS_HOME="/usr/share/grafana" \
GF_PATHS_LOGS="/var/log/grafana" \
GF_PATHS_PLUGINS="/var/lib/grafana/plugins" \
GF_PATHS_PROVISIONING="/etc/grafana/provisioning"

RUN apt-get update && apt-get install -qq -y tar libfontconfig curl ca-certificates && \
mkdir -p "$GF_PATHS_HOME/.aws" && \
curl -L "$GRAFANA_URL" | tar xfz - --strip-components=1 -C "$GF_PATHS_HOME" && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/* && \
dpkg -i /tmp/grafana.deb || true && \
rm /tmp/grafana.deb


VOLUME ["/var/lib/grafana", "/var/log/grafana", "/etc/grafana"]
groupadd -r -g $GF_GID grafana && \
useradd -r -u $GF_UID -g grafana grafana && \
mkdir -p "$GF_PATHS_PROVISIONING/datasources" \
"$GF_PATHS_PROVISIONING/dashboards" \
"$GF_PATHS_LOGS" \
"$GF_PATHS_PLUGINS" \
"$GF_PATHS_DATA" && \
cp "$GF_PATHS_HOME/conf/sample.ini" "$GF_PATHS_CONFIG" && \
cp "$GF_PATHS_HOME/conf/ldap.toml" /etc/grafana/ldap.toml && \
chown -R grafana:grafana "$GF_PATHS_DATA" "$GF_PATHS_HOME/.aws" "$GF_PATHS_LOGS" "$GF_PATHS_PLUGINS" && \
chmod 777 "$GF_PATHS_DATA" "$GF_PATHS_HOME/.aws" "$GF_PATHS_LOGS" "$GF_PATHS_PLUGINS"

EXPOSE 3000

COPY ./run.sh /run.sh

USER grafana
WORKDIR /

ENTRYPOINT ["/run.sh"]
ENTRYPOINT [ "/run.sh" ]
81 changes: 36 additions & 45 deletions docker/v4.x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ For example,
```

The following documentation is a mere adaptation of the [official
one](https://github.com/grafana/grafana-docker/tree/7eed5279e62fb1ebb78bef11e45e015cd09f4f0e).
one](https://github.com/grafana/grafana-docker/blob/61f378236434fca515248c4012bb1414cc77386c/README.md).

## Caution

Expand All @@ -51,9 +51,20 @@ docker run -d --name=grafana -p 3000:3000 fg2it/grafana-armhf:<tag>

Try it out, default admin user is admin/admin.

In case port 3000 is closed for external clients or there is no access
to the browser - you may test it by issuing:

```bash
curl -i localhost:3000/login
```

Make sure that you are getting "...200 OK" in response.
After that continue testing by modifying your client request to grafana.

## Configuring your Grafana container

All options defined in conf/grafana.ini can be overridden using the syntax `GF_<SectionName>_<KeyName>`. For example:
All options defined in conf/grafana.ini can be overridden using environment
variables by using the syntax `GF_<SectionName>_<KeyName>`. For example:

```bash
docker run \
Expand All @@ -65,56 +76,60 @@ docker run \
fg2it/grafana-armhf:<tag>
```

You can use your own grafana.ini file by using environment variable `GF_PATHS_CONFIG`.

More information in the grafana configuration [documentation](http://docs.grafana.org/installation/configuration/).

## Grafana container with persistent storage (recommended)

```bash
# create /var/lib/grafana as persistent volume storage
docker run -d -v /var/lib/grafana --name grafana-storage hypriot/armhf-busybox
# create a persistent volume for your data in /var/lib/grafana (database and plugins)
docker volume create --name grafana-storage

# start grafana
docker run \
-d \
-p 3000:3000 \
--name=grafana \
--volumes-from grafana-storage \
-v grafana-storage:/var/lib/grafana \
fg2it/grafana-armhf:<tag>
```
Note: An unnamed volume will be created for you when you boot Grafana,
using `docker volume create grafana-storage` just makes it easier to find
by giving it a name.

You need at least docker v1.9 for this. See the [data volume](https://docs.docker.com/engine/tutorials/dockervolumes/#/data-volumes)
documentation on docker.

## Installing plugins for Grafana

> This is the way recommended in the [grafana official documentation](https://github.com/grafana/grafana-docker/tree/7eed5279e62fb1ebb78bef11e45e015cd09f4f0e#grafana-container-with-persistent-storage-recommended).
Nevertheless, at some point I found this sharp underrated
[answer](http://serverfault.com/a/760244) to "docker volume container or
docker volume?" on severfault which refers to
[this](https://github.com/docker/docker/issues/20465) issue
and [this](https://github.com/docker/docker/issues/17798) one (especially [this](https://github.com/docker/docker/issues/17798#issuecomment-154815207) post
and [this](https://github.com/docker/docker/issues/17798#issuecomment-154820406)
one). So, I would advise to stop using data volume containers and switch to data volumes using the `docker volume` command.
Pass the plugins you want installed to docker with the `GF_INSTALL_PLUGINS` environment variable as a comma separated list. This will pass each plugin name to `grafana-cli plugins install ${plugin}`.

```bash
docker volume create --name grafana-storage
docker run \
-d \
-p 3000:3000 \
--name=grafana \
-v grafana-storage:/var/lib/grafana \
-e "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource" \
fg2it/grafana-armhf:<tag>
```

You need at least docker v1.9 for this. See the [data volume](https://docs.docker.com/engine/tutorials/dockervolumes/#/data-volumes)
documentation on docker.
## Building a custom Grafana image with pre-installed plugins

## Installing plugins for Grafana (since v3)
The `custom/` folder includes a `Dockerfile` that can be used to build a custom Grafana image. It accepts `GRAFANA_VERSION` and `GF_INSTALL_PLUGINS` as build arguments.

Pass the plugins you want installed to docker with the `GF_INSTALL_PLUGINS` environment variable as a comma seperated list. This will pass each plugin name to `grafana-cli plugins install ${plugin}`.
Example of how to build and run:

```bash
cd custom
docker build -t grafana:v5.0.4-with-plugins \
--build-arg "GRAFANA_VERSION=v5.0.4" \
--build-arg "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource" .
docker run \
-d \
-p 3000:3000 \
--name=grafana \
-e "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource" \
fg2it/grafana-armhf:<tag>
grafana:v5.0.4-with-plugins
```

## Running specific version of Grafana
Expand All @@ -128,30 +143,6 @@ docker run \
fg2it/grafana-armhf:v2.6.0
```

## Building a custom Grafana image with pre-installed plugins

Dockerfile:

```Dockerfile
FROM fg2it/grafana:5.0.0
ENV GF_PATHS_PLUGINS=/opt/grafana-plugins
RUN mkdir -p $GF_PATHS_PLUGINS
RUN grafana-cli --pluginsDir $GF_PATHS_PLUGINS plugins install grafana-clock-panel
```

Add lines with `RUN grafana-cli ...` for each plugin you wish to install in your custom image. Don't forget to specify what version of Grafana you wish to build from (replace 5.0.0 in the example).

Example of how to build and run:

```bash
docker build -t grafana:5.0.0-custom .
docker run \
-d \
-p 3000:3000 \
--name=grafana \
grafana:5.0.0-custom
```

## Configuring AWS credentials for CloudWatch support

```bash
Expand Down
2 changes: 1 addition & 1 deletion docker/v4.x/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def push(docker_tag):
print("""git tag : {}
grafana : {}
docker tag: {}
base image: {}""".format(args.git_tag,
dist : {}""".format(args.git_tag,
pkg_name,
docker_tag,
args.release)
Expand Down
16 changes: 16 additions & 0 deletions docker/v4.x/custom/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ARG GRAFANA_VERSION

FROM fg2it/grafana-armhf:${GRAFANA_VERSION}

USER grafana

ARG GF_INSTALL_PLUGINS=""

RUN if [ ! -z "${GF_INSTALL_PLUGINS}" ]; then \
OLDIFS=$IFS; \
IFS=','; \
for plugin in ${GF_INSTALL_PLUGINS}; do \
IFS=$OLDIFS; \
grafana-cli --pluginsDir "$GF_PATHS_PLUGINS" plugins install ${plugin}; \
done; \
fi
86 changes: 51 additions & 35 deletions docker/v4.x/run.sh
Original file line number Diff line number Diff line change
@@ -1,51 +1,67 @@
#!/bin/bash -e

: "${GF_PATHS_CONFIG:=/etc/grafana/grafana.ini}"
: "${GF_PATHS_DATA:=/var/lib/grafana}"
: "${GF_PATHS_LOGS:=/var/log/grafana}"
: "${GF_PATHS_PLUGINS:=/var/lib/grafana/plugins}"
: "${GF_PATHS_PROVISIONING:=/etc/grafana/provisioning}"
PERMISSIONS_OK=0

if [ ! -r "$GF_PATHS_CONFIG" ]; then
echo "GF_PATHS_CONFIG='$GF_PATHS_CONFIG' is not readable."
PERMISSIONS_OK=1
fi

if [ ! -w "$GF_PATHS_DATA" ]; then
echo "GF_PATHS_DATA='$GF_PATHS_DATA' is not writable."
PERMISSIONS_OK=1
fi

if [ ! -r "$GF_PATHS_HOME" ]; then
echo "GF_PATHS_HOME='$GF_PATHS_HOME' is not readable."
PERMISSIONS_OK=1
fi

if [ $PERMISSIONS_OK -eq 1 ]; then
echo "You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migration-from-a-previous-version-of-the-docker-container-to-5-1-or-later"
fi

if [ ! -d "$GF_PATHS_PLUGINS" ]; then
mkdir "$GF_PATHS_PLUGINS"
fi

chown -R grafana:grafana "$GF_PATHS_DATA" "$GF_PATHS_LOGS" || true

if [ ! -z ${GF_AWS_PROFILES+x} ]; then
mkdir -p ~grafana/.aws/
> ~grafana/.aws/credentials

for profile in ${GF_AWS_PROFILES}; do
access_key_varname="GF_AWS_${profile}_ACCESS_KEY_ID"
secret_key_varname="GF_AWS_${profile}_SECRET_ACCESS_KEY"
region_varname="GF_AWS_${profile}_REGION"

if [ ! -z "${!access_key_varname}" -a ! -z "${!secret_key_varname}" ]; then
echo "[${profile}]" >> ~grafana/.aws/credentials
echo "aws_access_key_id = ${!access_key_varname}" >> ~grafana/.aws/credentials
echo "aws_secret_access_key = ${!secret_key_varname}" >> ~grafana/.aws/credentials
if [ ! -z "${!region_varname}" ]; then
echo "region = ${!region_varname}" >> ~grafana/.aws/credentials
fi
fi
done
> "$GF_PATHS_HOME/.aws/credentials"

for profile in ${GF_AWS_PROFILES}; do
access_key_varname="GF_AWS_${profile}_ACCESS_KEY_ID"
secret_key_varname="GF_AWS_${profile}_SECRET_ACCESS_KEY"
region_varname="GF_AWS_${profile}_REGION"

if [ ! -z "${!access_key_varname}" -a ! -z "${!secret_key_varname}" ]; then
echo "[${profile}]" >> "$GF_PATHS_HOME/.aws/credentials"
echo "aws_access_key_id = ${!access_key_varname}" >> "$GF_PATHS_HOME/.aws/credentials"
echo "aws_secret_access_key = ${!secret_key_varname}" >> "$GF_PATHS_HOME/.aws/credentials"
if [ ! -z "${!region_varname}" ]; then
echo "region = ${!region_varname}" >> "$GF_PATHS_HOME/.aws/credentials"
fi
fi
done

chown grafana:grafana -R ~grafana/.aws
chmod 600 ~grafana/.aws/credentials
chmod 600 "$GF_PATHS_HOME/.aws/credentials"
fi

if [ ! -z "${GF_INSTALL_PLUGINS}" ]; then
OLDIFS=$IFS
IFS=','
for plugin in ${GF_INSTALL_PLUGINS}; do
IFS=$OLDIFS
gosu grafana grafana-cli --pluginsDir "${GF_PATHS_PLUGINS}" plugins install ${plugin}
grafana-cli --pluginsDir "${GF_PATHS_PLUGINS}" plugins install ${plugin}
done
fi

exec gosu grafana /usr/sbin/grafana-server \
--homepath=/usr/share/grafana \
--config="$GF_PATHS_CONFIG" \
cfg:default.log.mode="console" \
cfg:default.paths.data="$GF_PATHS_DATA" \
cfg:default.paths.logs="$GF_PATHS_LOGS" \
cfg:default.paths.plugins="$GF_PATHS_PLUGINS" \
cfg:default.paths.provisioning=$GF_PATHS_PROVISIONING \
"$@"
exec grafana-server \
--homepath="$GF_PATHS_HOME" \
--config="$GF_PATHS_CONFIG" \
"$@" \
cfg:default.log.mode="console" \
cfg:default.paths.data="$GF_PATHS_DATA" \
cfg:default.paths.logs="$GF_PATHS_LOGS" \
cfg:default.paths.plugins="$GF_PATHS_PLUGINS" \
cfg:default.paths.provisioning="$GF_PATHS_PROVISIONING"

0 comments on commit d6e3eb0

Please sign in to comment.