FROM nginx:alpine AS builder

RUN apk add --no-cache --virtual .build-deps \
    gcc \
    geoip-dev \
    git \
    libc-dev \
    libmaxminddb \
    libmaxminddb-dev \
    make \
    openssl-dev \
    pcre-dev \
    python3-dev \
    py3-pip \
    zlib-dev

WORKDIR /usr/local

RUN git clone https://github.com/leev/ngx_http_geoip2_module.git --depth=1

RUN wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
    mkdir -p /usr/src && \
    tar -zxC /usr/src -f nginx-${NGINX_VERSION}.tar.gz

RUN CONFARGS=$(nginx -V 2>&1 | sed -n -e 's/^.*arguments: //p') \
    cd /usr/src/nginx-$NGINX_VERSION && \
    ./configure --with-compat $CONFARGS \
    --add-dynamic-module=/usr/local/ngx_http_geoip2_module/ && \
    make && \
    make modules && \
    make install && \
    mkdir -p /usr/local/nginx/modules/


FROM nginx:alpine
COPY --from=builder /usr/local/nginx/modules/ngx_http_geoip2_module.so /usr/local/nginx/modules/ngx_http_geoip2_module.so

RUN apk add \
    libmaxminddb \
    libmaxminddb-dev

RUN mkdir -p /etc/nginx/conf

WORKDIR /usr/share/nginx/html

EXPOSE 80 443

VOLUME [ "/usr/share/nginx/html" ]

ENTRYPOINT [ "/usr/sbin/nginx", "-c", "/etc/nginx/conf/nginx.conf" ]
