build: add Docker deployment

Created a Dockerfile and nginx configuration and added environment
variables into the codebase to support Docker compose.

ref: N25B-282
This commit is contained in:
2025-11-14 14:06:12 +01:00
parent 04818f48d4
commit c05c74e412
5 changed files with 102 additions and 11 deletions

11
.dockerignore Normal file
View File

@@ -0,0 +1,11 @@
node_modules
dist
Dockerfile
.dockerignore
.git/
.githooks/
__mocks__/
test/
eslint.config.js
jest.config.js
README.md

46
Dockerfile Normal file
View File

@@ -0,0 +1,46 @@
# --- Building static files ---
FROM node:23-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
# --- Serving ---
FROM nginx:alpine
RUN mkdir -p /app/www
COPY --from=build /app/dist /app/www
COPY nginx.conf /etc/nginx/templates/default.conf.template
RUN adduser -D -H -u 1001 -s /sbin/nologin webuser
RUN chown -R webuser:webuser /app/www && \
chmod -R 755 /app/www && \
chown -R webuser:webuser /var/cache/nginx && \
chown -R webuser:webuser /var/log/nginx && \
chown -R webuser:webuser /etc/nginx/conf.d && \
touch /var/run/nginx.pid && \
chown -R webuser:webuser /var/run/nginx.pid && \
chmod -R 777 /etc/nginx/conf.d
ENV PORT=80
ENV NGINX_ENVSUBST_TEMPLATE_DIR=/etc/nginx/templates
ENV NGINX_ENVSUBST_TEMPLATE_SUFFIX=.template
ENV NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx/conf.d
# Default value, potentially overwritten in compose file
ENV BACKEND_ADDRESS="http://localhost:8000"
EXPOSE ${PORT}
USER webuser
CMD [ "nginx", "-g", "daemon off;" ]

26
nginx.conf Normal file
View File

@@ -0,0 +1,26 @@
server {
listen ${PORT};
server_name localhost;
root /app/www;
# Security headers
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
# Compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
location / {
try_files $uri $uri/ /index.html;
expires -1;
}
# Cache static assets
location /assets {
expires 1y;
add_header Cache-Control "public, no-transform";
}
}

View File

@@ -10,7 +10,7 @@ export default function Robot() {
const sendMessage = async () => { const sendMessage = async () => {
try { try {
const response = await fetch("http://localhost:8000/message", { const response = await fetch(`${process.env.BACKEND_ADDRESS}/message`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -25,7 +25,7 @@ export default function Robot() {
}; };
useEffect(() => { useEffect(() => {
const eventSource = new EventSource("http://localhost:8000/sse"); const eventSource = new EventSource(`${process.env.BACKEND_ADDRESS}/sse`);
eventSource.onmessage = (event) => { eventSource.onmessage = (event) => {
try { try {

View File

@@ -3,11 +3,17 @@
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
"target": "ES2022", "target": "ES2022",
"useDefineForClassFields": true, "useDefineForClassFields": true,
"lib": ["ES2022", "DOM", "DOM.Iterable"], "lib": [
"ES2022",
"DOM",
"DOM.Iterable"
],
"module": "ESNext", "module": "ESNext",
"types": ["vite/client"], "types": [
"vite/client",
"node"
],
"skipLibCheck": true, "skipLibCheck": true,
/* Bundler mode */ /* Bundler mode */
"moduleResolution": "bundler", "moduleResolution": "bundler",
"allowImportingTsExtensions": true, "allowImportingTsExtensions": true,
@@ -15,7 +21,6 @@
"moduleDetection": "force", "moduleDetection": "force",
"noEmit": true, "noEmit": true,
"jsx": "react-jsx", "jsx": "react-jsx",
/* Linting */ /* Linting */
"strict": true, "strict": true,
"noUnusedLocals": true, "noUnusedLocals": true,
@@ -24,5 +29,8 @@
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true "noUncheckedSideEffectImports": true
}, },
"include": ["src","test"] "include": [
"src",
"test"
]
} }