diff --git a/docker/Dockerfile b/docker/Dockerfile index 04fe1d685..80a897f02 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,32 +1,36 @@ -# Stage 1 - build environment FROM node:22-alpine AS build RUN apk add --no-cache python3 make g++ git -# Create app directory WORKDIR /usr/src/app -# Swtich to node user -#RUN chown node:node ./ -#USER node - COPY .npmrc ./ COPY package.json pnpm-lock.yaml ./ -# Install app dependencies RUN corepack enable && pnpm install --frozen-lockfile -# Copy all required files COPY . . -# Build the application -RUN pnpm run build:web +RUN pnpm nx build web --configuration=pwa +RUN pnpm nx build web-backend -# Stage 2 - the production environment -FROM nginx:stable-alpine +FROM node:22-alpine -# Copy artifacts and nignx.conf -COPY --from=build /usr/src/app/dist/browser /usr/share/nginx/html -COPY --from=build /usr/src/app/docker/nginx.conf /etc/nginx/conf.d/default.conf +RUN apk add --no-cache nginx -CMD sed -i "s#http://localhost:3333#$BACKEND_URL#g" /usr/share/nginx/html/main.js && nginx -g 'daemon off;' +WORKDIR /opt/iptvnator + +ENV PORT=3000 +ENV BACKEND_URL=/api +ENV CLIENT_URL=http://localhost:4333 + +COPY --from=build /usr/src/app/dist/apps/web /usr/share/nginx/html +COPY --from=build /usr/src/app/dist/apps/web-backend ./web-backend +COPY docker/nginx.conf /etc/nginx/http.d/default.conf +COPY docker/docker-entrypoint.sh /usr/local/bin/iptvnator-entrypoint + +RUN chmod +x /usr/local/bin/iptvnator-entrypoint + +EXPOSE 80 + +CMD ["iptvnator-entrypoint"] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index d36ab1c08..11639797b 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,18 +1,12 @@ --- services: - backend: - image: 4gray/iptvnator-backend:latest - ports: - - "7333:3000" - environment: - - CLIENT_URL=http://localhost:4333 - # this one should match with the address and port in frontend CLIENT_URL env - - frontend: + iptvnator: image: 4gray/iptvnator:latest + build: + context: .. + dockerfile: docker/Dockerfile ports: - "4333:80" environment: - - BACKEND_URL=http://localhost:7333 - # this one should match with the address of the backend service - + BACKEND_URL: /api + CLIENT_URL: http://localhost:4333 diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh new file mode 100644 index 000000000..77466bca1 --- /dev/null +++ b/docker/docker-entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/sh +set -eu + +: "${PORT:=3000}" +: "${BACKEND_URL:=/api}" +: "${CLIENT_URL:=http://localhost:4333}" + +export PORT BACKEND_URL CLIENT_URL + +sed -i "s#127.0.0.1:3000#127.0.0.1:${PORT}#g" /etc/nginx/http.d/default.conf + +node <<'NODE' +const fs = require('node:fs'); + +const configPath = '/usr/share/nginx/html/assets/app-config.js'; +const config = { BACKEND_URL: process.env.BACKEND_URL || '/api' }; + +fs.mkdirSync('/usr/share/nginx/html/assets', { recursive: true }); +fs.writeFileSync( + configPath, + `window.__IPTVNATOR_CONFIG__ = Object.assign({}, window.__IPTVNATOR_CONFIG__, ${JSON.stringify(config)});\n` +); +NODE + +node /opt/iptvnator/web-backend/main.cjs & +backend_pid="$!" + +shutdown() { + kill "$backend_pid" 2>/dev/null || true +} + +trap shutdown INT TERM EXIT + +nginx -g 'daemon off;' diff --git a/docker/nginx.conf b/docker/nginx.conf index 3eacd9674..5ed772655 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -1,11 +1,33 @@ server { listen 80; - - location / { - root /usr/share/nginx/html; - index index.html index.htm; - try_files $uri $uri/ /index.html =404; + + root /usr/share/nginx/html; + index index.html; + + location = /assets/app-config.js { + add_header Cache-Control "no-store"; + try_files $uri =404; } - - include /etc/nginx/extra-conf.d/*.conf; -} \ No newline at end of file + + location = /ngsw-worker.js { + add_header Cache-Control "no-cache"; + try_files $uri =404; + } + + location = /ngsw.json { + add_header Cache-Control "no-cache"; + try_files $uri =404; + } + + location /api/ { + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_pass http://127.0.0.1:3000/; + } + + location / { + try_files $uri $uri/ /index.html; + } +}