ARG NGINX_VERSION=1.26.3
# Stage 1: Build the module
FROM nginx:$NGINX_VERSION AS builder

# Install build tools and MaxMind library
RUN apt-get update && \
    apt-get install -y build-essential libmaxminddb-dev libpcre2-dev zlib1g-dev libssl-dev wget git && \
    rm -rf /var/lib/apt/lists/*

#https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
# Download NGINX source matching the container version
RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
    tar zxf nginx-$NGINX_VERSION.tar.gz

# Clone the specific GeoIP2 module project
RUN git clone https://github.com/leev/ngx_http_geoip2_module.git

# Compile the module as a dynamic module
RUN cd nginx-${NGINX_VERSION} && \
    ./configure --with-compat --add-dynamic-module=../ngx_http_geoip2_module && \
    make modules

# Stage 2: Final Image
FROM nginx:$NGINX_VERSION

# Install only the runtime library
RUN apt-get update && apt-get install -y libmaxminddb0 && rm -rf /var/lib/apt/lists/*

# Copy the compiled .so file from the builder
COPY --from=builder /nginx-$NGINX_VERSION/objs/ngx_http_geoip2_module.so /usr/lib/nginx/modules/

# Copy your GeoIP database and NGINX config
# Ensure these files are in the same folder as this Dockerfile



# Copy your GeoLite2 database into the container
COPY ./dbip-country-lite-2026-01.mmdb /etc/nginx/geoip/GeoLite2-Country.mmdb
COPY ./dbip-country-lite-2026-01.mmdb /data/dbip-country-lite-2026-01.mmdb

# Copy your custom nginx config
COPY ./nginx.conf /etc/nginx/nginx.conf

EXPOSE 80
