在 Docker 中为 Nginx:alpine 安装模块

CListery ARE YOU OK?

通过 DockerFile 给基于 nginx:apline 构建的镜像安装 HTTP Echo 模块

环境

  1. 宿主机: 20.04.6 LTS (Focal Fossa)
  2. docker: Docker version 24.0.4, build 3713ee1
  3. docker-compose: docker-compose version 1.27.4, build 40524192
  4. nginx: nginx:1.19.1-alpine
  5. nginx-module
    • HTTP Echo: 0.63

DockerFile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
FROM nginx:1.19.1-alpine as base

RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.cernet.edu.cn/g" /etc/apk/repositories;

FROM base as build

ENV ECHO_NGINX_MODULE_VERSION 0.63

RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.cernet.edu.cn/g" /etc/apk/repositories;

RUN NGINX_VER=`nginx -v 2>&1 | cut -d '/' -f 2` \
&& cd /tmp \
&& wget http://nginx.org/download/nginx-${NGINX_VER}.tar.gz -O nginx-${NGINX_VER}.tar.gz \
&& wget https://github.com/openresty/echo-nginx-module/archive/refs/tags/v${ECHO_NGINX_MODULE_VERSION}.tar.gz -O echo-nginx-module-${ECHO_NGINX_MODULE_VERSION}.tar.gz \
&& apk add --no-cache --virtual dependency gcc libc-dev make openssl-dev pcre-dev zlib-dev linux-headers \
&& tar zxf nginx-${NGINX_VER}.tar.gz \
&& tar zxf echo-nginx-module-${ECHO_NGINX_MODULE_VERSION}.tar.gz \
&& cd nginx-${NGINX_VER} \
&& CONFARGS=$(nginx -V 2>&1 | sed -n -e 's/^configure arguments: //p') \
&& echo $CONFARGS \
&& sh -c "./configure --with-compat ${CONFARGS} --add-dynamic-module=../echo-nginx-module-${ECHO_NGINX_MODULE_VERSION}" \
&& make modules \
&& apk del dependency \
&& rm -rf /var/cache/apk/* \
&& mkdir -p /tmp/objs \
&& cp /tmp/nginx-${NGINX_VER}/objs/*.so /tmp/objs/ \
&& ls /tmp/objs -al

FROM base

COPY --from=build /tmp/objs/ngx_http_echo_module.so /etc/nginx/modules
RUN sed -i '1s/^/load_module \/etc\/nginx\/modules\/ngx_http_echo_module.so;\n/' /etc/nginx/nginx.conf

WORKDIR /www

更多参考

https://nginx.org/en/docs/configure.html
https://www.nginx.com/resources/wiki/modules/

  • 标题: 在 Docker 中为 Nginx:alpine 安装模块
  • 作者: CListery
  • 创建于 : 2023-07-27 08:37:26
  • 更新于 : 2024-11-15 15:53:32
  • 链接: http://clistery.github.io/2023/07/27/docker/apline-nginx-install-module/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
在 Docker 中为 Nginx:alpine 安装模块