Как собрать образ с использованием модуля geoip для nginx из исходного кода alpine?

How can I include the ngx_http_geoip_module in my Nginx container when using the provided Dockerfile, and how can I start the container with the --with-http_geoip_module flag?
  • 23 января 2024 г. 10:10
Ответы на вопрос 1
To include the `ngx_http_geoip_module` in your Nginx container when building from source using Alpine Linux, you will modify the Dockerfile to download and build the module as part of the Nginx build process. Below is an example of how such a Dockerfile can look.

```Dockerfile
# Start with the Alpine base image
FROM alpine:latest AS builder

# Install build dependencies for Nginx and the GeoIP module
RUN apk add --no-cache \
    build-base \
    gcc \
    gd-dev \
    geoip-dev \
    libxml2-dev \
    libxslt-dev \
    linux-headers \
    make \
    openssl-dev \
    pcre-dev \
    wget \
    zlib-dev

# Set the version of Nginx to download
ENV NGINX_VERSION=1.23.1

# Download Nginx source code
RUN wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz -O nginx.tar.gz && \
    tar -zxf nginx.tar.gz && \
    mv nginx-${NGINX_VERSION} nginx

WORKDIR /nginx

# Configure Nginx with additional module
RUN ./configure \
    --prefix=/usr/share/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 \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
    --with-pcre \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_xslt_module=dynamic \
    --with-http_image_filter_module=dynamic \
    --with-http_geoip_module=dynamic \  # Include the GeoIP module
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-http_slice_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-http_v2_module \
    --with-ipv6

# Build Nginx with the configured options
RUN make && make install

# Minimize the image by using an Alpine base
FROM alpine:latest

# Install necessary run-time dependencies for Nginx
RUN apk add --no-cache \
    libc6-compat \
    gd \
    geoip \
    libxslt \
    pcre \
    zlib

# Copy Nginx and GeoIP modules from the builder
COPY --from=builder /usr/share/nginx /usr/share/nginx
COPY --from=builder /usr/sbin/nginx /usr/sbin/nginx
COPY --from=builder /usr/lib/nginx /usr/lib/nginx
COPY --from=builder /etc/nginx /etc/nginx
COPY --from=builder /var/cache/nginx /var/cache/nginx
COPY --from=builder /var/log/nginx /var/log/nginx
COPY --from=builder /var/run/nginx.pid /var/run/nginx.pid

# Forward request logs to Docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80

STOPSIGNAL SIGTERM

CMD ["nginx", "-g", "daemon off;"]
```

This Dockerfile performs a multi-stage build:

1. The first stage, named `builder`, includes building Nginx with the desired modules.
2. The second stage creates a clean image and copies the built Nginx binaries and modules from the `builder` stage.

Please note that you need to replace `ENV NGINX_VERSION=1.23.1` with the version of Nginx you want to use. This example uses `1.23.1`, but you should use the latest stable version available at the time of building.

Once the Dockerfile is ready, build the image with:

```bash
docker build -t my-nginx-with-geoip .
Похожие вопросы