#inspired from https://github.com/fluent/fluentd-docker-image/blob/master/v1.16/alpine/Dockerfile
FROM alpine:3.17

ARG version=1.16.0

LABEL maintainer "Stefan Reimer<stefan@zero-downtime.net>"
LABEL Description="Fluentd docker image intended as ingestion service" Vendor="Zero Down Time" Version="${version}"

COPY Gemfile* /fluentd/

# skip runtime bundler installation
ENV FLUENTD_DISABLE_BUNDLER_INJECTION 1

RUN apk update \
 && apk add --no-cache \
        ca-certificates \
        ruby ruby-irb ruby-etc ruby-webrick \
        tini ruby-bundler \
 && apk add --no-cache --virtual .build-deps \
        build-base linux-headers \
        ruby-dev gnupg \
 && echo 'gem: --no-document' >> /etc/gemrc \
 && gem install oj -v 3.14.2 \
 && gem install json -v 2.6.3 \
 && gem install rexml -v 3.2.5 \
 && gem install async -v 1.30.3 \
 && gem install async-http -v 0.56.6 \
 && gem install fluentd -v ${version} \
 && gem install bigdecimal -v 1.4.4 \
 # Install additional gems from Gemfile
 && bundle config silence_root_warning true \
 && bundle install --gemfile=/fluentd/Gemfile \
 && apk del .build-deps \
 && rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem /usr/lib/ruby/gems/3.*/gems/fluentd-*/test

RUN addgroup -S fluent && adduser -S -g fluent fluent \
    # for log storage (maybe shared with host)
    && mkdir -p /fluentd/log \
    # configuration/plugins path (default: copied from .)
    && mkdir -p /fluentd/etc /fluentd/plugins \
    && chown -R fluent /fluentd && chgrp -R fluent /fluentd


COPY fluent.conf /fluentd/etc/
COPY entrypoint.sh /bin/

ENV FLUENTD_CONF="fluent.conf"

ENV LD_PRELOAD=""

EXPOSE 24224 5140

USER fluent
ENTRYPOINT ["tini",  "--", "/bin/entrypoint.sh"]
CMD ["fluentd"]