# Docker源码编译Nginx[可指定模块]

# 0x00 创建目录结构,结合docker-compose使用

/home/workdir
    - compose
    -- nginx
        --- conf.d
        ---- default.conf
        --- log
        *** Dockerfile
        *** nginx.conf
    - wwwroot
    -- demo
        *** index.html
    * docker-compose.yml


# 约定:
#      - 表示目录
#      * 表示文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 0x01 编写Dockerfile

路径:/home/workdir/compose/nginx/Dockerfile

参考:https://github.com/fabiocicerchia/nginx-lua/blob/master/nginx/1.18.0/debian/10.2-slim

便于以后添加插件

FROM debian:10.2-slim

LABEL maintainer="info@fabiocicerchia.it"
LABEL Author="https://github.com/fabiocicerchia/nginx-lua/blob/master/nginx/1.18.0/debian/10.2-slim"

ARG BUILD_DATE
ARG BUILD_VERSION
ARG VCS_REF

LABEL org.label-schema.schema-version="1.0"
LABEL org.label-schema.build-date=$BUILD_DATE
LABEL org.label-schema.name="fabiocicerchia/nginx-lua"
LABEL org.label-schema.description="Nginx 1.18.0 with LUA support based on debian 10.2-slim."
LABEL org.label-schema.url="https://github.com/fabiocicerchia/nginx-lua"
LABEL org.label-schema.vcs-url="https://github.com/fabiocicerchia/nginx-lua"
LABEL org.label-schema.vcs-ref=$VCS_REF
LABEL org.label-schema.version=$BUILD_VERSION
LABEL org.label-schema.docker.cmd="docker run -p 80:80 -d fabiocicerchia/nginx-lua:1.18.0-debian10.2-slim"

# https://github.com/openresty/luajit2
ENV VER_LUAJIT 2.1-20200102

# https://github.com/openresty/lua-nginx-module
# Production ready.
ENV VER_LUA_NGINX_MODULE 0.10.15

# https://github.com/openresty/lua-resty-core
# This library is production ready.
ENV VER_LUA_RESTY_CORE 0.1.17
ENV LUA_LIB_DIR /usr/local/share/lua/5.1

# https://github.com/openresty/lua-resty-lrucache
# This library is considered production ready.
ENV VER_LUA_RESTY_LRUCACHE 0.09

# https://github.com/nginx/nginx
ENV VER_NGINX 1.18.0

# https://github.com/vision5/ngx_devel_kit
# The NDK is now considered to be stable.
ENV VER_NGX_DEVEL_KIT 0.3.1

# https://github.com/Yelp/dumb-init
ENV VER_DUMBINIT 1.2.2

ENV LUAJIT_LIB /usr/local/lib
ENV LUAJIT_INC /usr/local/include/luajit-2.1
ENV LD_LIBRARY_PATH /usr/local/lib/:$LD_LIBRARY_PATH
ENV DEBIAN_FRONTEND noninteractive

RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends --no-install-suggests \
    ca-certificates \
    libgeoip-dev \
    libpcre3-dev \
    libssl-dev \
    zlib1g-dev \
&& apt-get install -y --no-install-recommends --no-install-suggests \
    curl \
    g++ \
    gzip \
    make \
    tar \
# OpenResty LUAJIT2
# ##############################################################################
&& curl -Lo /luajit.tar.gz https://github.com/openresty/luajit2/archive/v${VER_LUAJIT}.tar.gz \
    && tar xvzf /luajit.tar.gz && rm /luajit.tar.gz \
    && cd /luajit2-${VER_LUAJIT} \
    && make -j "$(nproc)" \
    && make install \
    && cd / \
# LUA Resty Core
# ##############################################################################
&& curl -Lo /lua-resty-core.tar.gz https://github.com/openresty/lua-resty-core/archive/v${VER_LUA_RESTY_CORE}.tar.gz \
    && tar xvzf /lua-resty-core.tar.gz && rm /lua-resty-core.tar.gz \
    && cd /lua-resty-core-${VER_LUA_RESTY_CORE} \
    && make -j "$(nproc)" \
    && make install \
    && cd / \
# LUA Resty LRUCache
# ##############################################################################
&& curl -Lo /lua-resty-lrucache.tar.gz https://github.com/openresty/lua-resty-lrucache/archive/v${VER_LUA_RESTY_LRUCACHE}.tar.gz \
    && tar xvzf /lua-resty-lrucache.tar.gz && rm /lua-resty-lrucache.tar.gz \
    && cd /lua-resty-lrucache-${VER_LUA_RESTY_LRUCACHE} \
    && make -j "$(nproc)" \
    && make install \
    && cd / \
# NGX Devel Kit
# ##############################################################################
&& curl -Lo /ngx_devel_kit.tar.gz https://github.com/vision5/ngx_devel_kit/archive/v${VER_NGX_DEVEL_KIT}.tar.gz \
    && tar xvzf /ngx_devel_kit.tar.gz && rm /ngx_devel_kit.tar.gz \
# Lua Nginx Module
# ##############################################################################
&& curl -Lo /lua-nginx.tar.gz https://github.com/openresty/lua-nginx-module/archive/v${VER_LUA_NGINX_MODULE}.tar.gz \
    && tar xvzf /lua-nginx.tar.gz && rm /lua-nginx.tar.gz \
# NGINX
# ##############################################################################
# create nginx user/group first, to be consistent throughout docker variants
&& addgroup --system --gid 32548 nginx \
&& adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 32548 nginx \
# we're on an architecture upstream doesn't officially build for
# let's build binaries from the published packaging sources
    && curl -Lo /nginx.tar.gz https://nginx.org/download/nginx-${VER_NGINX}.tar.gz \
    && tar xvzf /nginx.tar.gz && rm /nginx.tar.gz \
    && cd /nginx-${VER_NGINX} \
    && mkdir -p /var/cache/nginx/client_temp \
        /var/cache/nginx/proxy_temp \
        /var/cache/nginx/fastcgi_temp \
        /var/cache/nginx/uwsgi_temp \
        /var/cache/nginx/scgi_temp \
    && ./configure \
        --prefix=/etc/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 \
        --user=nginx \
        --group=nginx \
        --with-compat \
        --with-file-aio \
        --with-threads \
        --with-http_addition_module \
        --with-http_auth_request_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_mp4_module \
        --with-http_random_index_module \
        --with-http_realip_module \
        --with-http_secure_link_module \
        --with-http_slice_module \
        --with-http_ssl_module \
        --with-http_stub_status_module \
        --with-http_sub_module \
        --with-http_v2_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-stream \
        --with-stream_realip_module \
        --with-stream_ssl_module \
        --with-stream_ssl_preread_module \
        --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' \
        --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' \
        --add-module=/lua-nginx-module-${VER_LUA_NGINX_MODULE} \
        --add-module=/ngx_devel_kit-${VER_NGX_DEVEL_KIT} \
        --with-http_dav_module \
        --with-http_geoip_module \
    && make -j "$(nproc)" build \
    && make install \
# Bring in tzdata so users could set the timezones through the environment
# variables
&& apt-get install -y --no-install-recommends --no-install-suggests tzdata \
# Bring in curl and ca-certificates to make registering on DNS SD easier
&& apt-get install -y --no-install-recommends --no-install-suggests curl ca-certificates \
# forward request and error logs to docker log collector
&& ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
# dumb-init
# ##############################################################################
&& curl -Lo /usr/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v${VER_DUMBINIT}/dumb-init_${VER_DUMBINIT}_x86_64 \
&& chmod +x /usr/bin/dumb-init \
# Cleanup
# ##############################################################################
&& rm -rf /lua-nginx-module-${VER_LUA_NGINX_MODULE} \
&& rm -rf /lua-resty-core-${VER_LUA_RESTY_CORE} \
&& rm -rf /lua-resty-lrucache-${VER_LUA_RESTY_LRUCACHE} \
&& rm -rf /luajit2-${VER_LUAJIT} \
&& rm -rf /nginx-${VER_NGINX} \
&& rm -rf /ngx_devel_kit-${VER_NGX_DEVEL_KIT} \
&& apt-get autoremove -y \
&& apt-get remove -y \
    curl \
    g++ \
    make \
&& rm -rf /var/lib/apt/lists/*

HEALTHCHECK --interval=30s --timeout=3s CMD curl --fail http://localhost/ || exit 1

EXPOSE 80
EXPOSE 443

STOPSIGNAL SIGTERM

ENTRYPOINT ["dumb-init"]

CMD ["nginx", "-g", "daemon off;"]
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

# 0x02 编写docker-compose.yml

路径:/home/workdir/docker-compose.yml

version: "3.7"

services:
    nginx:
        build:
            context: .
            dockerfile: compose/nginx/Dockerfile
        restart: always
        container_name: nginx
        volumes:
            - ./wwwroot/demo:/home/wwwroot
            - ./compose/nginx/conf.d:/etc/nginx/conf.d
            - ./compose/nginx/nginx.conf:/etc/nginx/nginx.conf
            - ./compose/nginx/log:/var/log/nginx

            # 需要注意:主机文件挂载到容器内存在几种形式
            # 1. 用主机内的文件替换容器内的文件,比如容器内存在nginx.conf,主机内也存在nginx.conf,
            # 这时需要用容器的文件代替主机文件,./compose/nginx/nginx.conf:/etc/nginx/nginx.conf就会覆盖容器内的原有文件
            # 2. 将主机文件夹下的文件全部挂载到容器内,比如将主机内的/home/wwwroot/demo下的所有文件映射到容器,
            # 这时./wwwroot/demo:/home/wwwroot就会在容器内创建一个/home/wwwroot文件夹,主机demo文件夹内的所有文件都会映射到容器/home/wwwroot文件夹下
            # 3. 将容器内的文件夹挂载到主机,比如容器内/var/log/nginx文件下的日志文件会全部映射到主机./compose/nginx/log文件夹下

        ports:
            - "80:80"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 0x03 启动

cd /home/workdir
docker-compose up
1
2
上次更新:: 10/11/2020, 1:25:58 AM