Skip to content

Commit b146c0e

Browse files
committed
feat(ws): containerize_frontend_component/#392
Signed-off-by: Noa <[email protected]>
1 parent f23af69 commit b146c0e

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

workspaces/frontend/.dockerignore

Whitespace-only changes.

workspaces/frontend/Dockerfile

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# ---------- Builder stage ----------
2+
FROM node:20-slim AS builder
3+
4+
# Set working directory
5+
WORKDIR /usr/src/app
6+
7+
# Copy package files to the container
8+
COPY package*.json ./
9+
10+
# Install the dependencies and build
11+
RUN npm cache clean --force \
12+
&& npm ci
13+
14+
# Copy source code
15+
COPY . .
16+
17+
# Build the application
18+
RUN npm run build:prod
19+
20+
21+
# ---------- Production stage ----------
22+
FROM nginx:alpine
23+
24+
USER root
25+
26+
# Install envsubst (gettext package)
27+
RUN apk add --no-cache gettext
28+
29+
# Copy built assets from builder stage
30+
COPY --from=builder /usr/src/app/dist /usr/share/nginx/html
31+
32+
# Copy nginx template
33+
COPY nginx.conf.template /etc/nginx/nginx.conf.template
34+
35+
# Create directories and set permissions for non-root user
36+
RUN mkdir -p /var/cache/nginx/client_temp \
37+
/var/cache/nginx/proxy_temp \
38+
/var/cache/nginx/fastcgi_temp \
39+
/var/cache/nginx/uwsgi_temp \
40+
/var/cache/nginx/scgi_temp \
41+
/var/run/nginx \
42+
/tmp/nginx && \
43+
# Change ownership of nginx directories to nginx user (UID 101)
44+
chown -R 101:101 /var/cache/nginx \
45+
/var/run/nginx \
46+
/usr/share/nginx/html \
47+
/tmp/nginx \
48+
/etc/nginx
49+
50+
# Create startup script that works with non-root user
51+
RUN echo '#!/bin/sh' > /docker-entrypoint.sh && \
52+
echo 'envsubst "\${BACKEND_SERVICE}" < /etc/nginx/nginx.conf.template > /tmp/nginx/nginx.conf' >> /docker-entrypoint.sh && \
53+
echo 'exec nginx -c /tmp/nginx/nginx.conf -g "daemon off;"' >> /docker-entrypoint.sh && \
54+
chmod +x /docker-entrypoint.sh && \
55+
chown 101:101 /docker-entrypoint.sh
56+
57+
# Switch to nginx user (UID 101)
58+
USER 101:101
59+
60+
# Expose port
61+
EXPOSE 8080
62+
63+
# Set environment variables
64+
ENV PORT=8080
65+
66+
# Set default backend service
67+
ENV BACKEND_SERVICE=localhost:4000
68+
69+
# Start the production server
70+
CMD ["/docker-entrypoint.sh"]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
worker_processes auto;
2+
3+
error_log /dev/stderr warn;
4+
pid /tmp/nginx.pid;
5+
6+
events {
7+
worker_connections 1024;
8+
}
9+
10+
http {
11+
log_format main '$remote_addr - $remote_user [$time_local] - $http_x_api_version - "$request" '
12+
'$status $body_bytes_sent "$http_referer" '
13+
'"$http_user_agent" "$http_x_forwarded_for"';
14+
15+
access_log /dev/stdout main;
16+
17+
include /etc/nginx/mime.types;
18+
default_type application/octet-stream;
19+
20+
# Temporary file paths for non-root user
21+
client_body_temp_path /var/cache/nginx/client_temp;
22+
proxy_temp_path /var/cache/nginx/proxy_temp;
23+
fastcgi_temp_path /var/cache/nginx/fastcgi_temp;
24+
uwsgi_temp_path /var/cache/nginx/uwsgi_temp;
25+
scgi_temp_path /var/cache/nginx/scgi_temp;
26+
27+
# Security headers
28+
add_header X-Frame-Options "SAMEORIGIN" always;
29+
add_header X-XSS-Protection "1; mode=block" always;
30+
add_header X-Content-Type-Options "nosniff" always;
31+
add_header Referrer-Policy "no-referrer-when-downgrade" always;
32+
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
33+
34+
# Gzip Compression
35+
gzip on;
36+
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
37+
gzip_comp_level 5;
38+
gzip_min_length 1000;
39+
gzip_proxied any;
40+
gzip_vary on;
41+
gzip_disable "msie6";
42+
43+
# Upstream backend configuration
44+
upstream backend {
45+
server ${BACKEND_SERVICE};
46+
}
47+
48+
server {
49+
listen 8080;
50+
51+
# Health check endpoint
52+
location /health {
53+
access_log off;
54+
return 200 'healthy\n';
55+
}
56+
57+
location / {
58+
root /usr/share/nginx/html;
59+
index index.html;
60+
try_files $uri $uri/ /index.html;
61+
}
62+
63+
# Static assets (cache enabled)
64+
location ~* \.(css|js|gif|jpeg|jpg|png|ico|woff|woff2|ttf|otf|svg|eot)$ {
65+
root /usr/share/nginx/html;
66+
expires 30d;
67+
add_header Cache-Control "public, no-transform";
68+
try_files $uri =404;
69+
}
70+
71+
# Backend API
72+
location /api/ {
73+
proxy_pass http://backend/api/;
74+
proxy_http_version 1.1;
75+
proxy_set_header Host $host;
76+
proxy_set_header X-Real-IP $remote_addr;
77+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
78+
proxy_set_header X-Forwarded-Proto $scheme;
79+
80+
# Timeouts
81+
proxy_connect_timeout 60s;
82+
proxy_send_timeout 60s;
83+
proxy_read_timeout 60s;
84+
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)