Build NGINX Image Filter Module on Alpine Linux
In this article we will build the image filter module as a dynamic module and add to the NGINX base image. The image filter module is used to resize images on the fly.
Dockerfile
Dockerfile FROM nginx:1.27.0-alpine3.19-slim as base
FROM base as builder
# Install build dependencies
RUN apk add --no-cache --virtual .build-deps \
gcc \
libc-dev \
make \
linux-headers \
curl \
gd-dev \
pcre-dev \
zlib-dev
# Download and extract nginx source code
RUN mkdir -p /usr/src \
&& curl -fSL http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz | tar -zxC /usr/src
RUN cd /usr/src/nginx-${NGINX_VERSION} \
&& ./configure \
--with-compat \
--with-http_image_filter_module=dynamic \
&& make modules
# Add module to base image
FROM base as nginx-image-filter
RUN apk add gd # Image filter module depends on gd
COPY --from=builder /usr/src/nginx-${NGINX_VERSION}/objs/*.so /etc/nginx/modules/
Build
docker build -t nginx-image-filter .
Usage
Configuration
nginx.conf load_module modules/ngx_http_image_filter_module.so;
events {}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
}
# Enable image filter for images
location ~ "^/images/.*\.(jpe?g|png)$" {
image_filter resize 100 100;
}
}
Run
This example mounts the nginx.conf
file and images
directory to the container.
docker run -p 80:80 \ -v ./nginx.conf:/etc/nginx/nginx.conf:ro \ -v ./images:/usr/share/nginx/html/images:ro \ nginx-image-filter