FROM ubuntu:16.04

# 维护者信息
MAINTAINER Elastic Docker Team <docker@elastic.co>

RUN echo '#!/bin/sh' > /usr/sbin/policy-rc.d    && echo 'exit 101' >> /usr/sbin/policy-rc.d     && chmod +x /usr/sbin/policy-rc.d               && dpkg-divert --local --rename --add /sbin/initctl     && cp -a /usr/sbin/policy-rc.d /sbin/initctl   && sed -i 's/^exit.*/exit 0/' /sbin/initctl             && echo 'force-unsafe-io' > /etc/dpkg/dpkg.cfg.d/docker-apt-speedup             && echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > /etc/apt/apt.conf.d/docker-clean  && echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> /etc/apt/apt.conf.d/docker-clean  && echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> /etc/apt/apt.conf.d/docker-clean              && echo 'Acquire::Languages "none";' > /etc/apt/apt.conf.d/docker-no-languages                 && echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > /etc/apt/apt.conf.d/docker-gzip-indexes     && echo 'Apt::AutoRemove::SuggestsImportant "false";' > /etc/apt/apt.conf.d/docker-autoremove-suggests
RUN rm -rf /var/lib/apt/lists/
# 安装依赖
RUN apt-get update &&  apt-get clean

FROM commons-container-registry.tencentcloudcr.com/defeng/adoptopenjdk/openjdk:8

# 添加用户和组
RUN addgroup --gid 1000 logstash &&    adduser --disabled-password --gecos=logstash --uid=1000 --gid=1000   --home /usr/share/logstash --no-create-home logstash

# 下载并解压 Logstash
#RUN curl -Lso - https://artifacts.elastic.co/downloads/logstash/logstash-5.3.3.tar.gz |  tar zxf - -C /usr/share &&    mv /usr/share/logstash-5.3.3 /usr/share/logstash &&     chown --recursive logstash:logstash /usr/share/logstash/ &&   ln -s /usr/share/logstash /opt/logstash

COPY logstash-5.3.3/ /usr/share/logstash-5.3.3

RUN mv /usr/share/logstash-5.3.3 /usr/share/logstash &&     chown --recursive logstash:logstash /usr/share/logstash/ &&   ln -s /usr/share/logstash /opt/logstash

# 设置环境变量
ENV PATH=/usr/share/logstash/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# 复制配置文件
COPY config/ /usr/share/logstash/config/
COPY pipeline/logstash.conf /usr/share/logstash/pipeline/

# 设置权限
RUN chown --recursive logstash:logstash /usr/share/logstash/config/ /usr/share/logstash/pipeline/

# 生成本地化文件
RUN locale-gen en_US.UTF-8

# 设置语言环境
ENV LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8

# 复制可执行文件
COPY bin/docker-entrypoint /usr/local/bin/
COPY bin/env2yaml /usr/local/bin/

RUN chmod +x /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/env2yaml
RUN chown --recursive logstash:logstash  /usr/local/bin/docker-entrypoint
USER logstash
# 暴露端口
EXPOSE 5044/tcp 9600/tcp

# 设置入口点
ENTRYPOINT ["/usr/local/bin/docker-entrypoint"]

# 启动命令
CMD ["-f","/usr/share/logstash/pipeline"]
