Skip to content

Commit e747ad5

Browse files
authored
feat(ws): containerize frontend component (#394)
Signed-off-by: Noa <[email protected]>
1 parent 0db1fd7 commit e747ad5

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

workspaces/frontend/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
node_modules/

workspaces/frontend/Dockerfile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 config
33+
COPY nginx.conf /etc/nginx/nginx.conf
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+
# Switch to nginx user (UID 101)
51+
USER 101:101
52+
53+
# Expose port
54+
EXPOSE 8080
55+
56+
# Set environment variables
57+
ENV PORT=8080
58+
59+
# Start the production server
60+
CMD ["nginx", "-g", "daemon off;"]

workspaces/frontend/nginx.conf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/yaml 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+
server {
44+
listen 8080;
45+
46+
# Health check endpoint
47+
location /health {
48+
access_log off;
49+
return 200 'healthy\n';
50+
}
51+
52+
location / {
53+
root /usr/share/nginx/html;
54+
index index.html;
55+
try_files $uri $uri/ /index.html;
56+
}
57+
58+
# Static assets (cache enabled)
59+
location ~* \.(css|js|gif|jpeg|jpg|png|ico|woff|woff2|ttf|otf|svg|eot)$ {
60+
root /usr/share/nginx/html;
61+
expires 30d;
62+
add_header Cache-Control "public, no-transform";
63+
try_files $uri =404;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)