build: update Dockerfile for dynamic env configuration
This commit is contained in:
parent
c68b3f2f7e
commit
657ce4ed40
5 changed files with 66 additions and 14 deletions
|
@ -1,23 +1,47 @@
|
||||||
FROM oven/bun:debian AS build
|
# Build stage
|
||||||
|
FROM oven/bun:latest as build
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
RUN apt-get update -y && apt-get install nodejs -y
|
# Copy package.json and bun.lock
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
|
||||||
|
|
||||||
COPY package.json bun.lock ./
|
COPY package.json bun.lock ./
|
||||||
RUN bun install --frozen-lockfile
|
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
RUN bun install
|
||||||
|
|
||||||
|
# Copy the rest of the app
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
# Build the app
|
||||||
RUN bun run build
|
RUN bun run build
|
||||||
|
|
||||||
FROM nginx:alpine AS production
|
# Production stage
|
||||||
|
FROM nginx:alpine
|
||||||
|
|
||||||
RUN rm /etc/nginx/conf.d/default.conf
|
# Copy custom nginx config if needed
|
||||||
COPY .docker/casino.conf /etc/nginx/templates/nginx.conf.template
|
# COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
COPY .docker/entrypoint.sh /docker-entrypoint.d/40-custom-config-env.sh
|
|
||||||
|
|
||||||
COPY --from=build /app/dist/casino /usr/share/nginx/html
|
# Copy the build output
|
||||||
|
COPY --from=build /app/dist/browser /usr/share/nginx/html
|
||||||
|
|
||||||
|
# Copy the env.js file
|
||||||
|
COPY --from=build /app/env.js /usr/share/nginx/html/env.js
|
||||||
|
|
||||||
|
# Copy the entrypoint script
|
||||||
|
COPY docker-entrypoint.sh /docker-entrypoint.sh
|
||||||
|
RUN chmod +x /docker-entrypoint.sh
|
||||||
|
|
||||||
|
# Install envsubst
|
||||||
|
RUN apk add --no-cache gettext
|
||||||
|
|
||||||
|
# Update index.html to load env.js before the app
|
||||||
|
RUN sed -i 's/<head>/<head><script src="env.js"><\/script>/' /usr/share/nginx/html/index.html
|
||||||
|
|
||||||
|
# Set environment variables (these are defaults and can be overridden at runtime)
|
||||||
|
ENV STRIPE_KEY="pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG"
|
||||||
|
ENV API_URL="/backend"
|
||||||
|
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
|
||||||
|
# Use custom entrypoint to replace env vars at runtime
|
||||||
|
ENTRYPOINT ["/docker-entrypoint.sh"]
|
8
frontend/docker-entrypoint.sh
Normal file
8
frontend/docker-entrypoint.sh
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Replace environment variables in env.js
|
||||||
|
envsubst < /usr/share/nginx/html/env.js > /usr/share/nginx/html/env.js.tmp
|
||||||
|
mv /usr/share/nginx/html/env.js.tmp /usr/share/nginx/html/env.js
|
||||||
|
|
||||||
|
# Start nginx
|
||||||
|
exec nginx -g 'daemon off;'
|
8
frontend/env.js
Normal file
8
frontend/env.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
// env.js - Script to replace environment variables at runtime
|
||||||
|
(function(window) {
|
||||||
|
window.__env = window.__env || {};
|
||||||
|
|
||||||
|
// Environment variables
|
||||||
|
window.__env.STRIPE_KEY = '${STRIPE_KEY}';
|
||||||
|
window.__env.API_URL = '${API_URL}';
|
||||||
|
})(this);
|
|
@ -1,5 +1,16 @@
|
||||||
|
// Dynamic environment configuration that can be overridden by Docker
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
__env?: {
|
||||||
|
STRIPE_KEY?: string;
|
||||||
|
API_URL?: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const environment = {
|
export const environment = {
|
||||||
STRIPE_KEY:
|
STRIPE_KEY:
|
||||||
|
window.__env?.STRIPE_KEY ||
|
||||||
'pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG',
|
'pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG',
|
||||||
apiUrl: window.location.origin + '/backend',
|
apiUrl: window.__env?.API_URL || window.location.origin + '/backend',
|
||||||
};
|
};
|
|
@ -2,6 +2,7 @@
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
|
<script src="env.js"></script>
|
||||||
<title>Trustworthy Casino</title>
|
<title>Trustworthy Casino</title>
|
||||||
<base href="/" />
|
<base href="/" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
|
Reference in a new issue