Files
kami_apple_exchage/frontend/Dockerfile
danial 5c486e34d3 docs(项目): 添加项目文档并进行代码调整
- 新增 CODEBUDDY.md、GEMINI.md、GEMINI_CN.md 等项目文档
- 更新 Dockerfile 和其他配置文件
- 优化部分代码结构,如 orders.py、tasks.py 等
- 新增 .dockerignore 文件
2025-09-12 19:38:24 +08:00

126 lines
3.2 KiB
Docker

# Multi-stage build for Next.js frontend application with Nginx static hosting
# Stage 1: Build stage
FROM node:24-alpine AS builder
# Install dependencies including bun
RUN apk add --no-cache bash curl
RUN curl -fsSL https://bun.sh/install | bash
# Set up working directory
WORKDIR /app
# Copy package files
COPY package.json bun.lock package-lock.json* ./
# Install dependencies
ENV BUN_INSTALL=/root/.bun
ENV PATH=$BUN_INSTALL/bin:$PATH
RUN bun install
# Copy source code
COPY . .
COPY .env.production .env
# Build static export
RUN bun run build
# Stage 2: Nginx production stage
FROM nginx:alpine AS runner
# Install required system dependencies
RUN apk add --no-cache bash
# Remove default nginx configuration
RUN rm -f /etc/nginx/conf.d/default.conf
# Create nginx configuration for Next.js static export
COPY <<EOF /etc/nginx/conf.d/default.conf
server {
listen 8080;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/atom+xml
image/svg+xml;
# Static files caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|webp)\$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# Handle Next.js static routes
location / {
try_files \$uri \$uri/ \$uri.html /index.html;
}
# Handle Next.js asset paths
location /_next/static/ {
alias /usr/share/nginx/html/_next/static/;
expires 1y;
add_header Cache-Control "public, immutable";
}
# 需要配置反向代理
location /api/ {
proxy_pass http://apple-exchange-api:8000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
# Handle static assets
location /static/ {
alias /usr/share/nginx/html/static/;
expires 1y;
add_header Cache-Control "public, immutable";
}
}
EOF
# Copy built static files from builder stage
COPY --from=builder /app/out /usr/share/nginx/html
# Set correct permissions
RUN chown -R nginx:nginx /usr/share/nginx/html && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /var/log/nginx && \
chown -R nginx:nginx /etc/nginx/conf.d && \
mkdir -p /run/nginx && \
chown -R nginx:nginx /run/nginx
# Create nginx user and group if they don't exist
RUN addgroup -g 1001 -S nginx || true && \
adduser -S -D -H -u 1001 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx || true
# Switch to non-root user (commented out to avoid permission issues)
# USER nginx
# Expose port
EXPOSE 8080
# Start nginx
CMD ["nginx", "-g", "daemon off;"]