deploymate / app /templates /docker /dockerfile_nodejs.j2
shakauthossain's picture
V2.0.0
2df0cf9 verified
{% if config.multiStage %}
# Multi-stage build for Node.js
FROM node:{{ config.nodeVersion or '18' }}-alpine AS builder
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy source code
COPY . .
{% if config.buildCommand %}
# Build application
RUN {{ config.buildCommand }}
{% endif %}
# Production stage
FROM node:{{ config.nodeVersion or '18' }}-alpine
WORKDIR /app
# Copy from builder
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app .
{% else %}
# Node.js {{ config.nodeVersion or '18' }} Dockerfile
FROM node:{{ config.nodeVersion or '18' }}-alpine
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
{% if config.developmentMode %}
RUN npm install
{% else %}
RUN npm ci --only=production
{% endif %}
# Copy application code
COPY . .
{% if config.buildCommand and not config.developmentMode %}
# Build application
RUN {{ config.buildCommand }}
{% endif %}
{% endif %}
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001 && \
chown -R nodejs:nodejs /app
USER nodejs
# Expose port
EXPOSE {{ config.port or '3000' }}
{% if config.developmentMode %}
# Development mode
ENV NODE_ENV=development
# Install development dependencies
CMD ["npm", "run", "dev"]
{% else %}
# Production mode
ENV NODE_ENV=production
# Start application
CMD ["node", "{{ config.entrypoint or 'index.js' }}"]
{% endif %}