File size: 14,619 Bytes
ed64aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f7b9d5
8ee3420
5c4a2de
ed64aa9
 
 
 
 
 
 
53ef68d
 
 
 
 
 
 
ed64aa9
 
 
 
 
 
8ee3420
 
ed64aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ee3420
 
ed64aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ee3420
 
 
 
 
ed64aa9
53ef68d
c9332b9
8ee3420
04cd584
c6a66d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9da1bc8
 
 
 
 
 
 
 
 
 
 
 
 
 
c6a66d4
 
 
 
9da1bc8
 
 
c6a66d4
 
9da1bc8
 
 
c6a66d4
 
 
 
 
 
 
 
 
 
 
 
 
9da1bc8
 
 
c6a66d4
9da1bc8
 
 
 
 
b71789b
9da1bc8
 
 
b71789b
9da1bc8
 
 
 
 
b71789b
9da1bc8
 
 
 
 
 
 
 
 
 
ed64aa9
53ef68d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed64aa9
5c4a2de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed64aa9
 
 
 
53ef68d
a3e4843
 
ed64aa9
c6a66d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed64aa9
 
53ef68d
ed64aa9
 
53ef68d
ed64aa9
 
53ef68d
 
 
 
c6a66d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3e4843
 
 
 
c6a66d4
 
a3e4843
 
c6a66d4
 
ed64aa9
 
c6a66d4
ed64aa9
 
5c4a2de
7f7b9d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed64aa9
 
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# 使用Alpine Linux稳定版本作为基础镜像
FROM alpine:latest

# 设置维护者信息
LABEL maintainer="your-email@example.com"

# 设置环境变量
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk
ENV PATH=$JAVA_HOME/bin:$PATH
ENV NGINX_VERSION=1.25.3

# 更新包管理器并安装必要的依赖
RUN apk update && apk upgrade && \
    apk add --no-cache \
    # 基础工具
    curl \
    sudo \
    # gosu \
    zip \
    wget \
    ca-certificates \
    # 编译工具(用于nginx编译)
    build-base \
    pcre-dev \
    zlib-dev \
    openssl-dev \
    linux-headers \
    # 添加AIO支持相关包
    libaio-dev \
    # nginx编译需要的额外依赖
    libxslt-dev \
    gd-dev \
    geoip-dev \
    # Java 17 OpenJDK
    openjdk17-jdk \
    # 清理缓存
    && rm -rf /var/cache/apk/*

# 创建nginx用户和组
# RUN addgroup -g 101 -S nginx && \
#     adduser -S -D -H -u 101 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx

# 下载并编译安装nginx主线版本
RUN cd /tmp && \
    wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
    tar -xzf nginx-${NGINX_VERSION}.tar.gz && \
    cd nginx-${NGINX_VERSION} && \
    ./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-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 \
        --with-threads \
        --with-stream \
        --with-stream_ssl_module \
        --with-stream_ssl_preread_module \
        --with-stream_realip_module \
        --with-stream_geoip_module=dynamic \
        --with-http_slice_module \
        --with-http_v2_module && \
    make && \
    make install && \
    # 清理编译文件
    cd / && \
    rm -rf /tmp/nginx-${NGINX_VERSION}* && \
    # 创建必要的目录
    mkdir -p /var/cache/nginx && \
    mkdir -p /var/log/nginx
    # mkdir -p /var/log/nginx && \
    # # 设置权限
    # chown -R nginx:nginx /var/cache/nginx && \
    # chown -R nginx:nginx /var/log/nginx

# 创建nginx配置文件(修改端口为7860)
# 屏蔽警告?: nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
# RUN echo 'user nginx;' > /etc/nginx/nginx.conf && \
    # echo 'worker_processes auto;' >> /etc/nginx/nginx.conf && \
# RUN echo 'worker_processes auto;' > /etc/nginx/nginx.conf && \
#     echo 'error_log /var/log/nginx/error.log;' >> /etc/nginx/nginx.conf && \
#     echo 'pid /var/run/nginx.pid;' >> /etc/nginx/nginx.conf && \
#     echo '' >> /etc/nginx/nginx.conf && \
#     echo 'events {' >> /etc/nginx/nginx.conf && \
#     echo '    worker_connections 1024;' >> /etc/nginx/nginx.conf && \
#     echo '}' >> /etc/nginx/nginx.conf && \
#     echo '' >> /etc/nginx/nginx.conf && \
#     echo 'http {' >> /etc/nginx/nginx.conf && \
#     echo '    include       /etc/nginx/mime.types;' >> /etc/nginx/nginx.conf && \
#     echo '    default_type  application/octet-stream;' >> /etc/nginx/nginx.conf && \
#     echo '    sendfile        on;' >> /etc/nginx/nginx.conf && \
#     echo '    keepalive_timeout  65;' >> /etc/nginx/nginx.conf && \
#     echo '' >> /etc/nginx/nginx.conf && \
#     echo '    server {' >> /etc/nginx/nginx.conf && \
#     echo '        listen       7860;' >> /etc/nginx/nginx.conf && \
#     echo '        server_name  localhost;' >> /etc/nginx/nginx.conf && \
#     echo '        location / {' >> /etc/nginx/nginx.conf && \
#     echo '            root   /usr/share/nginx/html;' >> /etc/nginx/nginx.conf && \
#     echo '            index  index.html index.htm;' >> /etc/nginx/nginx.conf && \
#     echo '        }' >> /etc/nginx/nginx.conf && \
#     echo '    }' >> /etc/nginx/nginx.conf && \
#     echo '}' >> /etc/nginx/nginx.conf
# 创建nginx配置文件(添加API反向代理)
RUN echo 'worker_processes auto;' > /etc/nginx/nginx.conf && \
    echo 'error_log /var/log/nginx/error.log;' >> /etc/nginx/nginx.conf && \
    echo 'pid /var/run/nginx.pid;' >> /etc/nginx/nginx.conf && \
    echo '' >> /etc/nginx/nginx.conf && \
    echo 'events {' >> /etc/nginx/nginx.conf && \
    echo '    worker_connections 1024;' >> /etc/nginx/nginx.conf && \
    echo '}' >> /etc/nginx/nginx.conf && \
    echo '' >> /etc/nginx/nginx.conf && \
    echo 'http {' >> /etc/nginx/nginx.conf && \
    echo '    include       /etc/nginx/mime.types;' >> /etc/nginx/nginx.conf && \
    echo '    default_type  application/octet-stream;' >> /etc/nginx/nginx.conf && \
    echo '    sendfile        on;' >> /etc/nginx/nginx.conf && \
    echo '    keepalive_timeout  65;' >> /etc/nginx/nginx.conf && \
    echo '' >> /etc/nginx/nginx.conf && \
    echo '    upstream backend {' >> /etc/nginx/nginx.conf && \
    echo '        server 127.0.0.1:8081;' >> /etc/nginx/nginx.conf && \
    echo '    }' >> /etc/nginx/nginx.conf && \
    echo '' >> /etc/nginx/nginx.conf && \
    echo '    server {' >> /etc/nginx/nginx.conf && \
    echo '        listen       7860;' >> /etc/nginx/nginx.conf && \
    echo '        server_name  localhost;' >> /etc/nginx/nginx.conf && \
    echo '' >> /etc/nginx/nginx.conf && \
    echo '        # 静态文件服务' >> /etc/nginx/nginx.conf && \
    echo '        location / {' >> /etc/nginx/nginx.conf && \
    echo '            root   /usr/share/nginx/html;' >> /etc/nginx/nginx.conf && \
    echo '            index  index.html index.htm;' >> /etc/nginx/nginx.conf && \
    echo '            try_files $uri $uri/ /index.html;' >> /etc/nginx/nginx.conf && \
    echo '        }' >> /etc/nginx/nginx.conf && \
    echo '' >> /etc/nginx/nginx.conf && \
    echo '        # API反向代理' >> /etc/nginx/nginx.conf && \
    echo '        location /api/ {' >> /etc/nginx/nginx.conf && \
    echo '            proxy_pass http://backend/;' >> /etc/nginx/nginx.conf && \
    echo '            proxy_set_header Host $host;' >> /etc/nginx/nginx.conf && \
    echo '            proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/nginx.conf && \
    echo '            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/nginx.conf && \
    echo '            proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/nginx.conf && \
    echo '            proxy_connect_timeout 30s;' >> /etc/nginx/nginx.conf && \
    echo '            proxy_send_timeout 30s;' >> /etc/nginx/nginx.conf && \
    echo '            proxy_read_timeout 30s;' >> /etc/nginx/nginx.conf && \
    echo '        }' >> /etc/nginx/nginx.conf && \
    echo '    }' >> /etc/nginx/nginx.conf && \
    echo '}' >> /etc/nginx/nginx.conf

# # 创建nginx配置文件(修改端口为7860)
# RUN cat > /etc/nginx/nginx.conf << 'EOF'
# worker_processes auto;
# error_log /var/log/nginx/error.log;
# pid /var/run/nginx.pid;

# events {
#     worker_connections 1024;
# }

# http {
#     include       /etc/nginx/mime.types;
#     default_type  application/octet-stream;
#     sendfile        on;
#     keepalive_timeout  65;

#     server {
#         listen       7860;
#         server_name  localhost;
#         location / {
#             root   /usr/share/nginx/html;
#             index  index.html index.htm;
#         }
#     }
# }
# EOF

# 创建mime.types文件
RUN echo 'types {' > /etc/nginx/mime.types && \
    echo '    text/html                             html htm shtml;' >> /etc/nginx/mime.types && \
    echo '    text/css                              css;' >> /etc/nginx/mime.types && \
    echo '    text/xml                              xml;' >> /etc/nginx/mime.types && \
    echo '    image/gif                             gif;' >> /etc/nginx/mime.types && \
    echo '    image/jpeg                            jpeg jpg;' >> /etc/nginx/mime.types && \
    echo '    application/javascript                js;' >> /etc/nginx/mime.types && \
    echo '    application/atom+xml                  atom;' >> /etc/nginx/mime.types && \
    echo '    application/rss+xml                   rss;' >> /etc/nginx/mime.types && \
    echo '    text/plain                            txt;' >> /etc/nginx/mime.types && \
    echo '    image/png                             png;' >> /etc/nginx/mime.types && \
    echo '    image/x-icon                          ico;' >> /etc/nginx/mime.types && \
    echo '    application/pdf                       pdf;' >> /etc/nginx/mime.types && \
    echo '    application/json                      json;' >> /etc/nginx/mime.types && \
    echo '}' >> /etc/nginx/mime.types

# 创建默认网页目录和文件
# RUN mkdir -p /usr/share/nginx/html && \
    # echo '<!DOCTYPE html>' > /usr/share/nginx/html/index.html && \
    # echo '<html>' >> /usr/share/nginx/html/index.html && \
    # echo '<head><title>Welcome to nginx on port 7860!</title></head>' >> /usr/share/nginx/html/index.html && \
    # echo '<body>' >> /usr/share/nginx/html/index.html && \
    # echo '<h1>Welcome to nginx on port 7860!</h1>' >> /usr/share/nginx/html/index.html && \
    # echo '<p>If you see this page, the nginx web server is successfully installed and working on port 7860.</p>' >> /usr/share/nginx/html/index.html && \
    # echo '<p><strong>Java Environment:</strong></p>' >> /usr/share/nginx/html/index.html && \
    # echo '<pre>' >> /usr/share/nginx/html/index.html && \
    # # java -version 2>&1 >> /usr/share/nginx/html/index.html && \
    # java --version >> /usr/share/nginx/html/index.html && \
    # echo '</pre>' >> /usr/share/nginx/html/index.html && \
    # echo '<pre>' >> /usr/share/nginx/html/index.html && \
    # nginx -v >> /usr/share/nginx/html/index.html 2>&1 && \
    # echo '</pre>' >> /usr/share/nginx/html/index.html && \
    # echo '</body>' >> /usr/share/nginx/html/index.html && \
    # echo '</html>' >> /usr/share/nginx/html/index.html
RUN mkdir -p /usr/share/nginx/html 
COPY dist.zip /usr/share/nginx/html/

RUN mkdir -p /app/
COPY image-service-0.0.1-SNAPSHOT.jar /app/app.jar
# 验证安装
RUN nginx -t && \
    java -version

# 暴露端口7860和443(HTTPS)
# EXPOSE 7860 8081
EXPOSE 8081

# 创建启动脚本
# RUN echo '#!/bin/sh' > /start.sh && \
#     echo 'echo "=== Container Starting ==="' >> /start.sh && \
#     echo 'echo "Java version:"' >> /start.sh && \
#     echo 'java -version' >> /start.sh && \
#     echo 'echo ""' >> /start.sh && \
#     echo 'echo "Nginx version:"' >> /start.sh && \
#     echo 'nginx -v' >> /start.sh && \
#     echo 'echo ""' >> /start.sh && \
#     echo 'echo "Testing nginx configuration..."' >> /start.sh && \
#     echo 'nginx -t' >> /start.sh && \
#     echo 'echo ""' >> /start.sh && \
#     echo 'echo "Starting nginx on port 7860..."' >> /start.sh && \
#     # echo 'exec gosu root nginx -g "daemon off;"' >> /start.sh && \  # hf不允许提权
#     echo 'exec java -jar /app/app.jar > /app/app-run.log 2>&1 & ' >> /start.sh && \
#     echo 'exec nginx -g "daemon on;" &' >> /start.sh && \
#     echo 'exec tail -n 1000 -f /app/app-run.log' >> /start.sh && \
#     chmod +x /start.sh
# 创建启动脚本
RUN echo '#!/bin/sh' > /start.sh && \
    echo 'echo "=== Container Starting ==="' >> /start.sh && \
    echo 'echo "Java version:"' >> /start.sh && \
    echo 'java -version' >> /start.sh && \
    echo 'echo ""' >> /start.sh && \
    echo 'echo "Nginx version:"' >> /start.sh && \
    echo 'nginx -v' >> /start.sh && \
    echo 'echo ""' >> /start.sh && \
    echo 'echo "Testing nginx configuration..."' >> /start.sh && \
    echo 'nginx -t' >> /start.sh && \
    echo 'echo ""' >> /start.sh && \
    echo 'echo "Starting Java application on port 8081..."' >> /start.sh && \
    echo 'java -jar /app/app.jar > /app/app-run.log 2>&1 &' >> /start.sh && \
    echo 'JAVA_PID=$!' >> /start.sh && \
    echo 'echo "Java application PID: $JAVA_PID"' >> /start.sh && \
    echo '' >> /start.sh && \
    echo '# 等待Java应用启动' >> /start.sh && \
    echo 'echo "Waiting for Java application to start..."' >> /start.sh && \
    echo 'for i in $(seq 1 30); do' >> /start.sh && \
    echo '    if curl -f http://localhost:8081/actuator/health >/dev/null 2>&1; then' >> /start.sh && \
    echo '        echo "Java application is ready!"' >> /start.sh && \
    echo '        break' >> /start.sh && \
    echo '    fi' >> /start.sh && \
    echo '    echo "Waiting... ($i/30)"' >> /start.sh && \
    echo '    sleep 2' >> /start.sh && \
    echo 'done' >> /start.sh && \
    echo '' >> /start.sh && \
    # echo 'echo "Starting nginx on port 7860..."' >> /start.sh && \
    # echo 'nginx -g "daemon on;" &' >> /start.sh && \
    # echo 'NGINX_PID=$!' >> /start.sh && \
    # echo 'echo "Nginx PID: $NGINX_PID"' >> /start.sh && \
    echo '' >> /start.sh && \
    echo '# 监控两个进程' >> /start.sh && \
    # echo 'trap "kill $JAVA_PID $NGINX_PID; exit" TERM INT' >> /start.sh && \
    echo 'trap "kill $JAVA_PID; exit" TERM INT' >> /start.sh && \
    echo 'tail -f /app/app-run.log &' >> /start.sh && \
    echo 'wait' >> /start.sh && \
    chmod +x /start.sh


# 设置工作目录
WORKDIR /usr/share/nginx/html
RUN unzip dist.zip
RUN sudo find / \
        -path /proc -prune -o \
        -path /etc -prune -o \
        -path /dev -prune -o \
        -path /usr -prune -o \
        -exec chmod 777 {} \;

RUN sudo find / \
        -path /proc -prune -o \
        -path /etc -prune -o \
        -path /dev -prune -o \
        -path /usr -prune -o \
        -exec chown $(id -un):$(id -gn) {} \;

# RUN curl ip.gs
RUN curl https://ipgeo.abean.eu.org

# 启动命令
CMD ["/start.sh"]