Containerizing Aurelia apps with Docker
Prerequisites
Dockerfile
# Build stage
FROM node:20-alpine AS builder
# Create non-root user for security
RUN addgroup -g 1001 -S nodejs
RUN adduser -S aurelia -u 1001
# Set working directory
WORKDIR /app
# Copy package files for dependency installation
COPY --chown=aurelia:nodejs package*.json ./
# Install dependencies (production only)
RUN npm ci --omit=dev --silent && \
npm cache clean --force
# Copy source code
COPY --chown=aurelia:nodejs . .
# Build the application
RUN npm run build
# Production stage
FROM nginx:alpine
# Copy built application
COPY --from=builder /app/dist /usr/share/nginx/html
# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Create necessary directories and set permissions for nginx user
RUN mkdir -p /var/run/nginx && \
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 /var/run/nginx && \
chown -R nginx:nginx /etc/nginx/conf.d
# Switch to non-root user (nginx user already exists in nginx:alpine)
USER nginx
# Expose port
EXPOSE 8080
# Health check (using wget as curl is not available in Alpine)
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
# Start nginx
CMD ["nginx", "-g", "daemon off;"]Nginx Configuration
.dockerignore
Build and Run
Production Optimizations
Multi-Environment Support
Resource Limits
Docker Compose
Security Best Practices
Last updated
Was this helpful?