This commit is contained in:
Chu Văn Hải committed 2025-12-05 13:52:24 +07:00
commit f0d13cd510
29 files changed
+6868

No files matched your search

@@ -0,0 +1,93 @@
# Express.js Optimized Dockerfile
# Auto-generated for {{ app_name }}
# Node.js Version: {{ node_version }}
# Package Manager: {{ package_manager }}
# ============================================================================
# Build arguments
# ============================================================================
ARG NODE_VERSION={{ node_version }}
ARG PACKAGE_MANAGER={{ package_manager }}
ARG BUILD_COMMAND={{ build_command }}
ARG APP_PORT={{ framework_settings_port }}
ARG NODE_ENV=production
# ============================================================================
# Base stage
# ============================================================================
FROM gitlab.forlife.one:5050/share/node:${NODE_VERSION}-alpine AS base
# Install dependencies for native modules and health checks
RUN apk update && apk add imagemagick && apk add graphicsmagick && apk add tmux && apk add ffmpeg
RUN apk add --no-cache \
libc6-compat \
python3 \
make \
g++ \
curl \
dumb-init
# ============================================================================
# Production stage
# ============================================================================
FROM base AS production
COPY . ./app
WORKDIR /app
RUN npm install
# Build arguments
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
# Set build environment
ENV NODE_ENV=production
# Create build info endpoint
RUN mkdir -p src/common && \
echo 'export const BUILD_INFO = {"version":"{{ docker_image_tag }}","commit":"'${GIT_COMMIT}'","buildDate":"'${BUILD_DATE}'","framework":"expressjs","nodeVersion":"{{ node_version }}"}' > src/common/build-info.ts
# Build app
RUN npm run build
# Create non-root user
RUN addgroup --system --gid 1001 expressjs && adduser --system --uid 1001 expressjs
# Ensure dist folder is owned by expressjs user
RUN chown -R expressjs:expressjs /app/dist
# Set environment variables
ENV NODE_ENV=production
ENV PORT=${APP_PORT}
ENV APP_PORT=${APP_PORT}
ENV NODE_OPTIONS="--max-old-space-size={{ MEMORY_LIMIT_MB }}"
RUN echo '#!/bin/sh\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/health)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
exit 1' > /health-check.sh && \
chmod +x /health-check.sh
# Create logs directory
RUN mkdir -p ./logs && chown -R expressjs:expressjs ./logs
RUN ([ -d "./src/public" ] && cp -r ./src/public ./dist && chown -R expressjs:expressjs ./dist/public) || true
# Switch to non-root user
USER expressjs
# Expose port
EXPOSE ${APP_PORT}
# Health check
#HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 CMD /health-check.sh
# Start the application with dumb-init for proper signal handling
#CMD ["dumb-init", "node", "dist/main"]
CMD {{ RUN_CMD }}
@@ -0,0 +1,106 @@
# Define a build-time variable for the PHP version, with a default of 8.1
ARG PHP_VERSION={{ php_version }}
# Stage 1: Build Stage (Optional but good practice)
# Use the PHP_VERSION argument in the FROM instruction
FROM gitlab.forlife.one:5050/share/php:${PHP_VERSION}-fpm-alpine AS base
# Set working directory inside the container
WORKDIR /var/www/html
# -----------------------------------------------
# 1. INSTALL SYSTEM DEPENDENCIES & PHP EXTENSIONS
# -----------------------------------------------
# Added redis extension installation via PECL.
RUN apk update && apk add --no-cache \
git \
curl \
libpng-dev \
libjpeg-turbo-dev \
libwebp-dev \
freetype-dev \
zip \
unzip \
libzip-dev \
oniguruma-dev \
autoconf \
g++ \
make \
nginx \
supervisor \
dcron \
postgresql-dev \
libxml2-dev \
&& pecl install redis \
&& docker-php-ext-enable redis \
# Configure and install other extensions
&& docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
&& docker-php-ext-install -j$(nproc) gd pdo_mysql pdo_pgsql opcache bcmath exif pcntl sockets zip mbstring dom \
# Clean up
&& rm -rf /var/cache/apk/*
# Setup Laravel scheduler cron job to run every minute
# This cron job will be run by the 'www-data' user
RUN echo "* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1" > /etc/crontabs/www-data
# ============================================================================
# Production stage
# ============================================================================
FROM base AS production
WORKDIR /var/www/html
# -----------------------------------------------
# 2. APPLICATION SETUP & DEPENDENCIES
# -----------------------------------------------
# Install Composer v2, suitable for modern PHP versions
#COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
# Copy the Laravel application files
COPY . .
# Install Composer dependencies (Assuming composer.lock is present)
RUN composer install --no-dev --optimize-autoloader --no-interaction
# Optimize Laravel
RUN php artisan optimize && \
php artisan route:clear && \
php artisan route:cache && \
php artisan config:clear && \
php artisan config:cache && \
php artisan view:clear && \
php artisan view:cache
#RUN php artisan storage:link
# remove this line if you do not want to run migrations on each build
#RUN php artisan migrate --force
# Set appropriate permissions for Laravel (www-data is the default FPM user on Alpine)
RUN chown -R www-data:www-data /var/www/html \
&& chmod -R 775 storage bootstrap/cache
RUN ([ -d "public/cms/backend/uploads" ] && chmod -R 775 public/cms/backend/uploads) || true
# -----------------------------------------------
# 3. CONFIGURATION & ENTRYPOINT (THE FIX)
# -----------------------------------------------
RUN mkdir -p /run/nginx \
&& chown -R www-data:www-data /run/nginx \
&& chown -R www-data:www-data /var/lib/nginx
# Copy Nginx Configuration (Assuming it's in the 'docker' subdirectory)
# Nginx listens on port 80 and proxies PHP requests to FPM (port 9000).
COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/laravel.conf /etc/nginx/http.d/site.conf
COPY docker/php.ini /usr/local/etc/php/php.ini
# Copy Supervisor Configuration (Assuming it's in the 'docker' subdirectory)
# Supervisor manages both Nginx and PHP-FPM processes.
COPY docker/supervisord.conf /etc/supervisord.conf
EXPOSE 3000
# Start Supervisor, which runs Nginx, PHP-FPM, and Cron
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
@@ -0,0 +1,94 @@
# Express.js Optimized Dockerfile
# Auto-generated for {{ app_name }}
# Node.js Version: {{ node_version }}
# Package Manager: {{ package_manager }}
# ============================================================================
# Build arguments
# ============================================================================
ARG NODE_VERSION={{ node_version }}
ARG PACKAGE_MANAGER={{ package_manager }}
ARG BUILD_COMMAND={{ build_command }}
ARG APP_PORT={{ framework_settings_port }}
ARG NODE_ENV=production
# ============================================================================
# Base stage
# ============================================================================
FROM gitlab.forlife.one:5050/share/node:${NODE_VERSION}-alpine AS base
# Install dependencies for native modules and health checks
RUN apk update && apk add imagemagick && apk add graphicsmagick && apk add tmux && apk add ffmpeg
RUN apk add --no-cache \
libc6-compat \
python3 \
make \
g++ \
curl \
dumb-init
# ============================================================================
# Production stage
# ============================================================================
FROM base AS production
COPY . ./app
WORKDIR /app
RUN yarn install
# Build arguments
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
# Set build environment
ENV NODE_ENV=production
# Create build info endpoint
RUN mkdir -p src/common && \
echo 'export const BUILD_INFO = {"version":"{{ docker_image_tag }}","commit":"'${GIT_COMMIT}'","buildDate":"'${BUILD_DATE}'","framework":"nestjs","nodeVersion":"{{ node_version }}"}' > src/common/build-info.ts
# Build app
RUN npm run build
# Create non-root user
RUN addgroup --system --gid 1001 nestjs && adduser --system --uid 1001 nestjs
# Ensure dist folder is owned by nestjs user
RUN chown -R nestjs:nestjs /app/dist
# Set environment variables
ENV NODE_ENV=production
ENV PORT=${APP_PORT}
ENV APP_PORT=${APP_PORT}
ENV NODE_OPTIONS="--max-old-space-size={{ MEMORY_LIMIT_MB }}"
# Create health check script
RUN echo '#!/bin/sh\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/health)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
exit 1' > /health-check.sh && \
chmod +x /health-check.sh
# Create logs directory
RUN mkdir -p ./logs && chown -R nestjs:nestjs ./logs
RUN ([ -d "./src/public" ] && cp -r ./src/public ./dist && chown -R nestjs:nestjs ./dist/public) || true
# Switch to non-root user
USER nestjs
# Expose port
EXPOSE ${APP_PORT}
# Health check
#HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 CMD /health-check.sh
# Start the application with dumb-init for proper signal handling
#CMD ["dumb-init", "node", "dist/main"]
CMD {{ RUN_CMD }}
@@ -0,0 +1,103 @@
# Next.js Optimized Dockerfile with Dynamic Build Args
# Auto-generated for {{ app_name }}
# Node.js Version: {{ node_version }}
# Package Manager: {{ package_manager }}
# ============================================================================
# Build arguments - Base Configuration
# ============================================================================
ARG NODE_VERSION={{ node_version }}
ARG PACKAGE_MANAGER={{ package_manager }}
ARG BUILD_COMMAND={{ build_command }}
ARG START_COMMAND={{ start_command }}
ARG APP_PORT={{ framework_settings_port }}
ARG NODE_ENV=production
ARG APP_VERSION={{ docker_image_tag }}
ARG FRAMEWORK=nextjs
# ============================================================================
# Base stage - Alpine for smaller size
# ============================================================================
FROM gitlab.forlife.one:5050/share/node:${NODE_VERSION}-alpine AS base
# Install dependencies
RUN apk add --no-cache \
libc6-compat \
python3 \
make \
g++ curl
# Install dependencies only when needed
FROM base AS deps
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else {{ package_manager }} install; \
fi
#RUN {{ package_manager }} install
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED=1
RUN yarn build
# If using npm comment out above and use below instead
# RUN npm run build
# Production image, copy all the files and run next
FROM base AS production
WORKDIR /app
ENV NODE_ENV=production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_OPTIONS="--max-old-space-size={{ MEMORY_LIMIT_MB }}"
# Create health check script
RUN echo '#!/bin/sh\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/health)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
exit 1' > /health-check.sh && \
chmod +x /health-check.sh
# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
EXPOSE 3000
ENV HOSTNAME=0.0.0.0
ENV PORT=3000
ENV APP_PORT=3000
# Health check
#HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 CMD /health-check.sh
#CMD ["node", "server.js"]
CMD {{ RUN_CMD }}
@@ -0,0 +1,189 @@
# React.js Optimized Dockerfile (No Nginx)
# Auto-generated for {{ app_name }}
# Node.js Version: {{ node_version }}
# Package Manager: {{ package_manager }}
# ============================================================================
# Build arguments
# ============================================================================
ARG NODE_VERSION={{ node_version }}
ARG PACKAGE_MANAGER={{ package_manager }}
ARG BUILD_COMMAND={{ build_command }}
ARG APP_PORT={{ framework_settings_port }}
# ============================================================================
# Base stage
# ============================================================================
FROM gitlab.forlife.one:5050/share/node:${NODE_VERSION}-alpine AS base
# Install dependencies
RUN apk add --no-cache \
libc6-compat \
python3 \
make \
g++ curl
WORKDIR /app
# ============================================================================
# Dependencies stage
# ============================================================================
FROM base AS deps
# Copy package files
COPY package*.json yarn.lock* pnpm-lock.yaml* ./
# Install dependencies
#RUN yarn install
RUN {{ package_manager }} install
# ============================================================================
# Builder stage
# ============================================================================
FROM base AS builder
# Copy dependencies
COPY --from=deps /app/node_modules ./node_modules
# Copy source code
COPY . .
# Build arguments for runtime configuration
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
ARG REACT_APP_VERSION={{ docker_image_tag }}
# Set build environment
ENV NODE_ENV=production
ENV GENERATE_SOURCEMAP=false
ENV REACT_APP_VERSION=${REACT_APP_VERSION}
ENV REACT_APP_BUILD_DATE=${BUILD_DATE}
ENV REACT_APP_GIT_COMMIT=${GIT_COMMIT}
# Create build info file
RUN echo '{"version":"{{ docker_image_tag }}","commit":"'${GIT_COMMIT}'","buildDate":"'${BUILD_DATE}'","framework":"reactjs","nodeVersion":"{{ node_version }}"}' > public/build-info.json
# Require BuildKit enabled
# Mount secret nếu tồn tại, copy vào thư mục app
RUN --mount=type=secret,id=env,target=/run/secrets/env \
cp /run/secrets/env .env || echo ".env not found"
# ============================================================================
# Build the application
# ============================================================================
RUN \
if [ -f yarn.lock ]; then \
yarn build; \
elif [ -f package-lock.json ]; then \
npm run build; \
elif [ -f pnpm-lock.yaml ]; then \
pnpm build; \
else \
npm run build; \
fi
# ============================================================================
# Production stage with serve
# ============================================================================
FROM node:${NODE_VERSION}-alpine AS production
# Install curl for health checks
RUN apk add --no-cache curl
WORKDIR /app
# Install serve globally for serving static files
RUN npm install -g serve
# Copy built React app
COPY --from=builder /app/build ./build
# Create package.json for serving
RUN echo '{ \
"name": "{{ app_name }}-server", \
"version": "{{ docker_image_tag }}", \
"scripts": { \
"start": "serve -s build -l 3000" \
}, \
"dependencies": { \
"serve": "latest" \
} \
}' > package.json
# Create serve.json configuration
RUN echo '{ \
"public": "build", \
"rewrites": [ \
{ "source": "**", "destination": "/index.html" } \
], \
"headers": [ \
{ \
"source": "**/*.@(jpg|jpeg|gif|png|svg|ico|css|js)", \
"headers": [ \
{ \
"key": "Cache-Control", \
"value": "public, max-age=31536000, immutable" \
} \
] \
}, \
{ \
"source": "**", \
"headers": [ \
{ \
"key": "X-Content-Type-Options", \
"value": "nosniff" \
}, \
{ \
"key": "X-Frame-Options", \
"value": "DENY" \
}, \
{ \
"key": "X-XSS-Protection", \
"value": "1; mode=block" \
} \
] \
} \
] \
}' > serve.json
# Create health check endpoint
RUN mkdir -p build && echo 'healthy' > build/health.txt
# Create health check script
RUN echo '#!/bin/sh\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/health)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
status=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${APP_PORT}/)\n\
if [ "$status" -eq 200 ] || [ "$status" -eq 404 ]; then exit 0; fi\n\
\n\
exit 1' > /health-check.sh && \
chmod +x /health-check.sh
# Create non-root user
RUN addgroup --system --gid 1001 reactapp && \
adduser --system --uid 1001 reactapp
# Set proper permissions
RUN chown -R reactapp:reactapp /app
# Switch to non-root user
USER reactapp
# Expose port
EXPOSE 3000
# Set environment variables
ENV NODE_ENV=production
ENV PORT=3000
ENV APP_PORT=3000
ENV NODE_OPTIONS="--max-old-space-size={{ MEMORY_LIMIT_MB }}"
# Health check
#HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 CMD /health-check.sh
# Start the application
#CMD ["npm", "start"]
#CMD [ "serve", "-s", "build" ]
CMD {{ RUN_CMD }}