# ========================
# Версии
# ========================
ARG NGINX_VERSION=1.29.4
ARG VTS_VERSION=v0.2.5

# ==============
# Стадия сборки из предзагруженного offile образа, подготовленного скриптом prepare-offline.sh на машине, где есть Интернет
# ==============
FROM nginx-alpine-builddeps:1.29.4-alpine-builddeps AS builder

ARG NGINX_VERSION
ARG VTS_VERSION

WORKDIR /usr/src

# Копируем заранее скачанные тарболы внутрь образа
# (они должны лежать рядом с Dockerfile в ./sources/)
COPY sources/nginx-${NGINX_VERSION}.tar.gz /usr/src/nginx.tar.gz
COPY sources/nginx-module-vts-${VTS_VERSION}.tar.gz /usr/src/nginx-module-vts.tar.gz

# Распаковываем
RUN tar zxf nginx.tar.gz && rm nginx.tar.gz \
 && tar zxf nginx-module-vts.tar.gz && rm nginx-module-vts.tar.gz

# Путь к исходникам
WORKDIR /usr/src/nginx-${NGINX_VERSION}

# Собираем nginx с локальным модулем VTS
RUN ./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --modules-path=/usr/lib/nginx/modules \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --user=nginx \
    --group=nginx \
    --with-compat \
    --with-file-aio \
    --with-threads \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_mp4_module \
    --with-http_random_index_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_v3_module \
    --with-stream \
    --with-stream_realip_module \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --add-module=/usr/src/nginx-module-vts-${VTS_VERSION} \
 && make -j"$(getconf _NPROCESSORS_ONLINE)" \
 && make install

# ==============
# Финальный образ
# ==============
#FROM dockerhub.mos.ru/nginx:${NGINX_VERSION}-alpine
FROM nginx:${NGINX_VERSION}-alpine

ARG NGINX_VERSION
ARG VTS_VERSION

LABEL nginx.version="${NGINX_VERSION}" \
      vts.version="${VTS_VERSION}"

COPY --from=builder /usr/sbin/nginx /usr/sbin/nginx
COPY --from=builder /usr/lib/nginx/modules /usr/lib/nginx/modules

RUN mkdir -p /etc/nginx/conf.d /docker-entrypoint.d \
 && rm -f /etc/nginx/conf.d/default.conf \
 && ln -sf /dev/stdout /var/log/nginx/access.log \
 && ln -sf /dev/stderr /var/log/nginx/error.log \
 && chown -R nginx:nginx /etc/nginx /var/cache/nginx /var/log/nginx /var/run

COPY nginx-vts.conf /etc/nginx/nginx.conf

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

