FROM node:18-alpine

# Install dumb-init for proper signal handling
RUN apk add --no-cache dumb-init

WORKDIR /app

# Copy package files first for better caching
COPY package*.json ./

# Install only production dependencies and clean cache
RUN npm install --only=production --no-audit --no-fund && \
    npm cache clean --force

# Set build arguments
ARG API_ADMIN_BROWSER_URL
ENV API_ADMIN_BROWSER_URL=$API_ADMIN_BROWSER_URL

# Copy application code
COPY . .

# Create cert and logs directories and set up non-root user
RUN mkdir -p /app/cert /app/logs

EXPOSE 8091

# Use dumb-init as PID 1
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "./bin/www"]
