Skip to content

Instantly share code, notes, and snippets.

@yurtaev
Created July 28, 2019 18:03
Show Gist options
  • Save yurtaev/290a0d0fb48cb76948ea981fc91fe8b0 to your computer and use it in GitHub Desktop.
Save yurtaev/290a0d0fb48cb76948ea981fc91fe8b0 to your computer and use it in GitHub Desktop.
nginx + geoip2 modile
#
# https://github.com/leev/ngx_http_geoip2_module
#
FROM nginx:alpine AS builder
# nginx:alpine contains NGINX_VERSION environment variable, like so:
# ENV NGINX_VERSION 1.15.0
ENV NXG_GEIOP2_VERSION 3.2
# Download sources
RUN wget "http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" -O nginx.tar.gz && \
wget "https://github.com/leev/ngx_http_geoip2_module/archive/${NXG_GEIOP2_VERSION}.tar.gz" -O ngx_geoip2.tar.gz
# For latest build deps, see https://github.com/nginxinc/docker-nginx/blob/master/mainline/alpine/Dockerfile
RUN apk add --no-cache --virtual .build-deps \
gcc \
libc-dev \
make \
openssl-dev \
pcre-dev \
zlib-dev \
linux-headers \
curl \
gnupg \
libxslt-dev \
gd-dev \
geoip-dev \
libmaxminddb-dev
# Reuse same cli arguments as the nginx:alpine image used to build
RUN CONFARGS=$(nginx -V 2>&1 | sed -n -e 's/^.*arguments: //p') \
mkdir /usr/src && \
tar -zxC /usr/src -f nginx.tar.gz && \
tar -xzvf "ngx_geoip2.tar.gz" && \
NXG_GEIOP2_DIR="$(pwd)/ngx_http_geoip2_module-${NXG_GEIOP2_VERSION}" && \
cd /usr/src/nginx-$NGINX_VERSION && \
./configure --with-compat $CONFARGS --add-dynamic-module=$NXG_GEIOP2_DIR && \
make && make install
FROM nginx:alpine
# Extract the dynamic module GEOIP2 from the builder image
COPY --from=builder /usr/local/nginx/modules/ngx_http_geoip2_module.so /usr/local/nginx/modules/ngx_http_geoip2_module.so
RUN rm /etc/nginx/conf.d/default.conf
RUN echo 'load_module "modules/ngx_http_geoip2_module.so";' > /etc/nginx/modules/http_geoip2.conf
RUN sed -i "1iload_module modules/ngx_http_geoip2_module.so;\n" /etc/nginx/nginx.conf
RUN cat /etc/nginx/nginx.conf
EXPOSE 80
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment