From 1a5765d61537d353c11f624373c97814aea77360 Mon Sep 17 00:00:00 2001 From: Nick Jongens Date: Tue, 26 Nov 2024 20:00:44 +1300 Subject: [PATCH] Combined Docker container - Added support for a single container to run the backend and frontend from. - Still allows for separate frontend and backend hostnames to allow for API Gateways. --- Dockerfile | 60 +++++++++++++++++++++++++++++++++++++++ keyfade-backend/README.md | 16 ++++------- keyfade-backend/readme.md | 4 +-- start.sh | 13 +++++++++ 4 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 Dockerfile create mode 100644 start.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..357ce0d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,60 @@ +# Use the official Node.js 22 image as the base +FROM node:22 + +# Set a root working directory +WORKDIR /usr/src/app + +# Copy the .env file into the container for both frontend and backend +COPY .env .env + +# -------- Backend Setup -------- + +# Set the working directory for the backend +WORKDIR /usr/src/app/keyfade-backend + +# Copy backend package.json and package-lock.json +COPY keyfade-backend/package*.json ./ + +# Install backend dependencies +RUN npm install + +# Copy the rest of the backend code +COPY keyfade-backend . + +# -------- Frontend Setup -------- + +# Set the working directory for the frontend +WORKDIR /usr/src/app/keyfade-frontend + +# Copy frontend package.json and package-lock.json +COPY keyfade-frontend/package*.json ./ + +# Install frontend dependencies +RUN npm install + +# Copy the rest of the frontend code +COPY keyfade-frontend . + +# Copy the .env file into the frontend directory (if required by build tools) +RUN cp /usr/src/app/.env /usr/src/app/keyfade-frontend/.env + +# Build the frontend app +RUN npm run build + +# Clean up unnecessary files +RUN rm -f /usr/src/app/keyfade-frontend/.env + +# -------- Final Steps -------- + +# Set the working directory back to the root +WORKDIR /usr/src/app + +# Expose both frontend and backend ports +EXPOSE 3001 3002 + +# Copy the start script +COPY start.sh /usr/src/app/start.sh +RUN chmod +x /usr/src/app/start.sh + +# Start the applications using the start script +CMD ["/usr/src/app/start.sh"] \ No newline at end of file diff --git a/keyfade-backend/README.md b/keyfade-backend/README.md index 8de6e4d..e1635fb 100644 --- a/keyfade-backend/README.md +++ b/keyfade-backend/README.md @@ -33,7 +33,7 @@ This service is built using the following technologies: TENANT_ID: Your Azure Active Directory Tenant ID KEY_VAULT_NAME: - Name of the Key Vault used for secure data storage (e.g., keyfade-kv) + Name of the Key Vault used for secure data storage without the full url BACKEND_URL: URL of the backend (e.g., https://demo-api.keyfade.com) @@ -55,26 +55,19 @@ Build the container if you've made any changes or would like to ensure you have docker build -t keyfade-backend:latest . ``` -Or Use the publicly available backend container: -``` -docker pull ghcr.io/nickjongens/keyfade-backend:latest -``` - Docker Run: -``` -docker run -d -p :3002 \ +```docker run -d -p :3002 \ --name keyfade-backend \ - -e TZ=Pacific/Auckland \ -e CLIENT_ID= \ -e CLIENT_SECRET= \ -e TENANT_ID= \ - -e KEY_VAULT_NAME= \ -e BACKEND_URL=https://demo-api.keyfade.com \ -e FRONTEND_URL=https://demo.keyfade.com \ -e HMAC_SECRET= \ -e WEBHOOK_URL= \ - ghcr.io/nickjongens/keyfade-backend:latest + keyfade-backend:latest ``` ## Azure Key Vault Setup @@ -104,3 +97,4 @@ There are no API permissions to be given here. These are provided in the Key Vau - Run the containers within Azure Container Instances. + diff --git a/keyfade-backend/readme.md b/keyfade-backend/readme.md index b337d23..e1635fb 100644 --- a/keyfade-backend/readme.md +++ b/keyfade-backend/readme.md @@ -33,7 +33,7 @@ This service is built using the following technologies: TENANT_ID: Your Azure Active Directory Tenant ID KEY_VAULT_NAME: - Name of the Key Vault used for secure data storage (e.g., keyfade-kv) + Name of the Key Vault used for secure data storage without the full url BACKEND_URL: URL of the backend (e.g., https://demo-api.keyfade.com) @@ -62,7 +62,7 @@ Docker Run: -e CLIENT_ID= \ -e CLIENT_SECRET= \ -e TENANT_ID= \ - -e KEY_VAULT_NAME=keyfade-kv \ + -e KEY_VAULT_NAME= \ -e BACKEND_URL=https://demo-api.keyfade.com \ -e FRONTEND_URL=https://demo.keyfade.com \ -e HMAC_SECRET= \ diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..6eafd5d --- /dev/null +++ b/start.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Start the backend process +node /usr/src/app/keyfade-backend/index.js & + +# Start the frontend process +npx serve -s /usr/src/app/keyfade-frontend/dist -l 3001 & + +# Wait for any process to exit +wait -n + +# Exit with the status of the process that exited first +exit $?