test / Dockerfile
devin15's picture
Update Dockerfile
a3e4843 verified
# 使用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"]