refactor(docker): rework docker

- Move Docker files to own directory (tidier since I added several more files)
- Optimise images and reduce size
  - Uses multi-stage builds
  - Optimises layer efficiency
  - Uses NGINX as base for frontend
- Sets default env vars
- Uses tini for proper signal handling
This commit is contained in:
tigattack
2025-09-22 22:33:01 +01:00
parent 43ce146987
commit bf2ea908f4
8 changed files with 152 additions and 38 deletions

View File

@@ -1,22 +0,0 @@
FROM node:lts-alpine
RUN apk add --no-cache openssl
WORKDIR /app
COPY backend/package*.json ./
RUN npm install --only=production
RUN npm install -g prisma
COPY backend/ .
COPY agents/ ./agents
RUN npx prisma generate
RUN mkdir -p logs
EXPOSE 3001
CMD ["sh", "-c", "sleep 10 && npx prisma migrate deploy && npm start"]

45
docker/backend.Dockerfile Normal file
View File

@@ -0,0 +1,45 @@
FROM node:lts-alpine AS builder
RUN apk add --no-cache openssl
WORKDIR /app
COPY --chown=node:node package*.json /app/
COPY --chown=node:node backend/ /app/backend/
WORKDIR /app/backend
RUN npm ci &&\
npx prisma generate &&\
npm prune --omit=dev &&\
npm cache clean --force
FROM node:lts-alpine
ENV NODE_ENV=production \
ENABLE_LOGGING=true \
LOG_LEVEL=info \
PM_LOG_TO_CONSOLE=true
RUN apk add --no-cache openssl tini curl
USER node
WORKDIR /app
COPY --from=builder /app/backend /app/backend
COPY --from=builder /app/node_modules /app/node_modules
COPY --chown=node:node agents ./agents
COPY --chmod=755 docker/backend.docker-entrypoint.sh ./entrypoint.sh
WORKDIR /app/backend
EXPOSE 3001
VOLUME [ "/app/agents" ]
HEALTHCHECK --interval=10s --timeout=5s --start-period=30s --retries=5 \
CMD curl -f http://localhost:3001/health || exit 1
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/app/entrypoint.sh"]

View File

@@ -0,0 +1 @@
**/env.example

View File

@@ -0,0 +1,17 @@
#!/bin/sh
# Enable strict error handling
set -e
# Function to log messages with timestamp
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" >&2
}
log "Starting PatchMon Backend..."
log "Running database migrations..."
npx prisma migrate deploy
log "Starting application..."
exec npm start

View File

@@ -0,0 +1,24 @@
FROM node:lts-alpine AS builder
WORKDIR /app
COPY package*.json ./
COPY frontend/package*.json ./frontend/
RUN npm ci
COPY frontend/ ./frontend/
RUN npm run build:frontend
FROM nginxinc/nginx-unprivileged:alpine
ENV BACKEND_HOST=backend
ENV BACKEND_PORT=3001
COPY --from=builder /app/frontend/dist /usr/share/nginx/html
COPY docker/nginx.conf.template /etc/nginx/templates/default.conf.template
EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]

View File

@@ -0,0 +1,2 @@
**/Dockerfile
**/dist

View File

@@ -0,0 +1,63 @@
server {
listen 3000;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
tcp_nopush on;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/javascript
application/xml+rss
application/json
application/xml;
# Security headers
add_header X-Frame-Options DENY 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;
# Handle client-side routing
location / {
try_files $uri $uri/ /index.html;
}
# API proxy
location /api/ {
proxy_pass http://${BACKEND_HOST}:${BACKEND_PORT};
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# CORS headers for API calls
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
add_header Access-Control-Allow-Headers "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization" always;
# Handle preflight requests
if ($request_method = 'OPTIONS') {
return 204;
}
}
# Static assets caching
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# Health check endpoint
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
}

View File

@@ -1,16 +0,0 @@
FROM node:lts-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
RUN npm install --only=production
EXPOSE 3000
CMD ["node", "server.js"]