FROM buildpack-deps:stable

ENV NGINX_VERSION nginx-1.19.2
ENV NGINX_RTMP_MODULE_VERSION 1.2.1

ADD startscript.sh /usr/sbin/startscript.sh
ADD config /etc/nginx/

RUN apt-get update && \
    apt-get install -y ca-certificates openssl libssl-dev ffmpeg && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    mkdir -p /tmp/build/ && \
    cd /tmp/build/ && \
    wget -O ${NGINX_VERSION}.tar.gz https://nginx.org/download/${NGINX_VERSION}.tar.gz && \
    tar -zxf ${NGINX_VERSION}.tar.gz &&\
    git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git &&\
    cd /tmp/build/${NGINX_VERSION} && \
    ./configure \
        --sbin-path=/usr/local/sbin/nginx \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --pid-path=/var/run/nginx/nginx.pid \
        --lock-path=/var/lock/nginx/nginx.lock \
        --http-log-path=/var/log/nginx/access.log \
        --http-client-body-temp-path=/tmp/nginx-client-body \
        --with-http_ssl_module \
        --with-threads \
        --with-select_module \
        --with-poll_module \
        --with-file-aio \
        --with-stream=dynamic \
        --with-stream_ssl_module \
        --with-stream_realip_module \
        --with-stream_ssl_preread_module \
        --with-compat \
        --add-module=../nginx-rtmp-module \
        --with-debug\
        --builddir=${NGINX_VERSION} && \
    make -j $(getconf _NPROCESSORS_ONLN) && \
    make install && \
    mkdir /var/lock/nginx && \
    rm -rf /tmp/build && \
    ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log && \ 
    useradd -r -d /var/cache/nginx/ -s /sbin/nologin -U nginx && \
    ln -sf /usr/lib64/nginx/modules /etc/nginx/modules && \
    chown R nginx:users /etc/nginx

USER nginx:users

EXPOSE 1935
CMD ["bash", "/usr/sbin/startscript.sh"]