test / Dockerfile
devin15's picture
Update Dockerfile
17f5cea verified
raw
history blame
8.39 kB
# 使用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 \
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 && \
# 设置权限
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /var/log/nginx
# 创建nginx配置文件(修改端口为7860)
RUN echo 'user nginx;' > /etc/nginx/nginx.conf && \
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
# 创建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 2>&1 >> /usr/share/nginx/html/index.html && \
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 nginx -t && \
java -version
# 暴露端口7860和443(HTTPS)
EXPOSE 7860
# 创建启动脚本
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 'nginx -g "daemon off;"' >> /start.sh && \
chmod +x /start.sh
# 设置工作目录
WORKDIR /usr/share/nginx/html
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"]