Skip to content

Commit

Permalink
test deploy using docker-compose with env variable inject during dock…
Browse files Browse the repository at this point in the history
…er compose up
xeroxis-xs committed Sep 30, 2024

Verified

This commit was signed with the committer’s verified signature.
ekohl Ewoud Kohl van Wijngaarden
1 parent 90cda20 commit b9061ee
Showing 8 changed files with 171 additions and 53 deletions.
6 changes: 6 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NEXT_PUBLIC_AZURE_CLIENT_ID=NEXT_PUBLIC_AZURE_CLIENT_ID
NEXT_PUBLIC_AZURE_REDIRECT_URI=NEXT_PUBLIC_AZURE_REDIRECT_URI
NEXT_PUBLIC_SITE_URL=NEXT_PUBLIC_SITE_URL
NEXT_PUBLIC_BACKEND_URL=NEXT_PUBLIC_BACKEND_URL
NEXT_PUBLIC_MICROSERVICE_URL=NEXT_PUBLIC_MICROSERVICE_URL
NEXT_PUBLIC_LOGIN_REQUEST_SCOPE=NEXT_PUBLIC_LOGIN_REQUEST_SCOPE
11 changes: 11 additions & 0 deletions .run/Docker Compose Dev.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Docker Compose Dev" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<deployment type="docker-compose.yml">
<settings>
<option name="envFilePath" value="" />
<option name="sourceFilePath" value="docker-compose-dev.yml" />
</settings>
</deployment>
<method v="2" />
</configuration>
</component>
12 changes: 12 additions & 0 deletions .run/Docker Image Pull.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Docker Image Pull" type="docker-deploy" factoryName="docker-image" server-name="Docker">
<deployment type="docker-image">
<settings>
<option name="imageTag" value="xeroxisxs/eduquest-frontend-nextjs:latest" />
<option name="containerName" value="" />
<option name="commandLineOptions" value="--platform linux/amd64" />
</settings>
</deployment>
<method v="2" />
</configuration>
</component>
90 changes: 64 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -49,40 +49,78 @@



# Stage 1: Build the Next.js app
FROM node:20-alpine AS build
## Stage 1: Build the Next.js app
#FROM node:20-alpine AS build
#
#WORKDIR /app
#
## Install dependencies
#COPY package.json package-lock.json ./
#RUN npm install
#
## Copy all files and build the project
#COPY . .
#
## Temporary set the env variables and Build the project
#RUN NEXT_PUBLIC_AZURE_CLIENT_ID=PLACEHOLDER_NEXT_PUBLIC_AZURE_CLIENT_ID NEXT_PUBLIC_AZURE_REDIRECT_URI=PLACEHOLDER_NEXT_PUBLIC_AZURE_REDIRECT_URI NEXT_PUBLIC_SITE_URL=PLACEHOLDER_NEXT_PUBLIC_SITE_URL NEXT_PUBLIC_BACKEND_URL=PLACEHOLDER_NEXT_PUBLIC_BACKEND_URL NEXT_PUBLIC_MICROSERVICE_URL=PLACEHOLDER_NEXT_PUBLIC_MICROSERVICE_URL NEXT_PUBLIC_LOGIN_REQUEST_SCOPE=PLACEHOLDER_NEXT_PUBLIC_LOGIN_REQUEST_SCOPE npm run build
#
## Stage 2: Run the Next.js app
#FROM node:20-alpine
#
#WORKDIR /app
#
## Install necessary dependencies
#RUN apk add --no-cache libc6-compat
#
## Copy the built files from the previous stage
#COPY --from=build /app ./
#
#ENV PORT=80
#
## Expose the port the app runs on
#EXPOSE 80
#
#COPY --chown=nextjs:nodejs entrypoint.sh /entrypoint.sh
#RUN chmod +x /entrypoint.sh
##ENTRYPOINT ["/entrypoint.sh"]
#
## Start the Next.js app
#CMD ["npm", "start"]

WORKDIR /app

# Install dependencies
COPY package.json package-lock.json ./
RUN npm install
FROM node:20-alpine AS dependencies
RUN apk add --no-cache libc6-compat
WORKDIR /home/app
COPY package.json ./
COPY package-lock.json ./
RUN npm i

# Copy all files and build the project
FROM node:20-alpine AS builder
WORKDIR /home/app
COPY --from=dependencies /home/app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED 1
ARG NODE_ENV
ENV NODE_ENV=${NODE_ENV}
RUN npm run build

# Temporary set the env variables and Build the project
RUN NEXT_PUBLIC_AZURE_CLIENT_ID=PLACEHOLDER_NEXT_PUBLIC_AZURE_CLIENT_ID NEXT_PUBLIC_AZURE_REDIRECT_URI=PLACEHOLDER_NEXT_PUBLIC_AZURE_REDIRECT_URI NEXT_PUBLIC_SITE_URL=PLACEHOLDER_NEXT_PUBLIC_SITE_URL NEXT_PUBLIC_BACKEND_URL=PLACEHOLDER_NEXT_PUBLIC_BACKEND_URL NEXT_PUBLIC_MICROSERVICE_URL=PLACEHOLDER_NEXT_PUBLIC_MICROSERVICE_URL NEXT_PUBLIC_LOGIN_REQUEST_SCOPE=PLACEHOLDER_NEXT_PUBLIC_LOGIN_REQUEST_SCOPE npm run build

# Stage 2: Run the Next.js app
FROM node:20-alpine
FROM node:18-slim AS runner

WORKDIR /app

# Install necessary dependencies
RUN apk add --no-cache libc6-compat

# Copy the built files from the previous stage
COPY --from=build /app ./
WORKDIR /home/app
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
COPY --from=builder /home/app/.next/standalone ./standalone
COPY --from=builder /home/app/public /home/app/standalone/public
COPY --from=builder /home/app/.next/static /home/app/standalone/.next/static
COPY --from=builder /home/app/entrypoint.sh ./scripts/entrypoint.sh
COPY --from=builder /home/app/.env.production ./.env.production

ENV PORT=80

# Expose the port the app runs on
EXPOSE 80
ENV HOSTNAME="0.0.0.0"

RUN chmod +x ./scripts/entrypoint.sh

COPY --chown=nextjs:nodejs entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
#ENTRYPOINT ["/entrypoint.sh"]
ENTRYPOINT [ "./scripts/entrypoint.sh" ]

# Start the Next.js app
CMD ["npm", "start"]
CMD ["node", "./standalone/server.js"]
34 changes: 34 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#services:
# eduquest-frontend:
# build:
# context: .
# dockerfile: Dockerfile
# entrypoint: ["/entrypoint.sh"]
# ports:
# - "3000:80"
# restart: always
# volumes:
# - /app/node_modules
# - /app/.next
# environment:
# NEXT_PUBLIC_SITE_URL: ${NEXT_PUBLIC_SITE_URL}
# NEXT_PUBLIC_AZURE_CLIENT_ID: ${NEXT_PUBLIC_AZURE_CLIENT_ID}
# NEXT_PUBLIC_AZURE_REDIRECT_URI: ${NEXT_PUBLIC_AZURE_REDIRECT_URI}
# NEXT_PUBLIC_BACKEND_URL: ${NEXT_PUBLIC_BACKEND_URL}
# NEXT_PUBLIC_MICROSERVICE_URL: ${NEXT_PUBLIC_MICROSERVICE_URL}
# NEXT_PUBLIC_LOGIN_REQUEST_SCOPE: ${NEXT_PUBLIC_LOGIN_REQUEST_SCOPE}
#
services:
frontend:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:80"
environment:
NEXT_PUBLIC_SITE_URL: ${NEXT_PUBLIC_SITE_URL}
NEXT_PUBLIC_AZURE_CLIENT_ID: ${NEXT_PUBLIC_AZURE_CLIENT_ID}
NEXT_PUBLIC_AZURE_REDIRECT_URI: ${NEXT_PUBLIC_AZURE_REDIRECT_URI}
NEXT_PUBLIC_BACKEND_URL: ${NEXT_PUBLIC_BACKEND_URL}
NEXT_PUBLIC_MICROSERVICE_URL: ${NEXT_PUBLIC_MICROSERVICE_URL}
NEXT_PUBLIC_LOGIN_REQUEST_SCOPE: ${NEXT_PUBLIC_LOGIN_REQUEST_SCOPE}
30 changes: 21 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#services:
# eduquest-frontend:
# image: xeroxisxs/eduquest-frontend-nextjs:latest
# entrypoint: ["/entrypoint.sh"]
# ports:
# - "80:80"
# restart: always
# volumes:
# - /app/node_modules
# - /app/.next
# environment:
# NEXT_PUBLIC_SITE_URL: ${NEXT_PUBLIC_SITE_URL}
# NEXT_PUBLIC_AZURE_CLIENT_ID: ${NEXT_PUBLIC_AZURE_CLIENT_ID}
# NEXT_PUBLIC_AZURE_REDIRECT_URI: ${NEXT_PUBLIC_AZURE_REDIRECT_URI}
# NEXT_PUBLIC_BACKEND_URL: ${NEXT_PUBLIC_BACKEND_URL}
# NEXT_PUBLIC_MICROSERVICE_URL: ${NEXT_PUBLIC_MICROSERVICE_URL}
# NEXT_PUBLIC_LOGIN_REQUEST_SCOPE: ${NEXT_PUBLIC_LOGIN_REQUEST_SCOPE}


services:
eduquest-frontend:
build:
context: .
dockerfile: Dockerfile
entrypoint: ["/entrypoint.sh"]
frontend:
image: xeroxisxs/eduquest-frontend-nextjs:latest
ports:
- "3000:80"
restart: always
volumes:
- /app/node_modules
- /app/.next
environment:
NEXT_PUBLIC_SITE_URL: ${NEXT_PUBLIC_SITE_URL}
NEXT_PUBLIC_AZURE_CLIENT_ID: ${NEXT_PUBLIC_AZURE_CLIENT_ID}
40 changes: 22 additions & 18 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#!/bin/sh
#!/bin/bash
envFilename='./.env.production'
nextFolder='./standalone/.next/'

echo "Starting entrypoint script..."
while read -r line; do
# no comment or not empty
if [ "${line:0:1}" == "#" ] || [ "${line}" == "" ]; then
continue
fi

# Check environment variables
echo "Checking environment variables..."
[ -z "$NEXT_PUBLIC_AZURE_CLIENT_ID" ] && echo "NEXT_PUBLIC_AZURE_CLIENT_ID is not set" && exit 1
[ -z "$NEXT_PUBLIC_AZURE_REDIRECT_URI" ] && echo "NEXT_PUBLIC_AZURE_REDIRECT_URI is not set" && exit 1
[ -z "$NEXT_PUBLIC_SITE_URL" ] && echo "NEXT_PUBLIC_SITE_URL is not set" && exit 1
[ -z "$NEXT_PUBLIC_BACKEND_URL" ] && echo "NEXT_PUBLIC_BACKEND_URL is not set" && exit 1
[ -z "$NEXT_PUBLIC_MICROSERVICE_URL" ] && echo "NEXT_PUBLIC_MICROSERVICE_URL is not set" && exit 1
[ -z "$NEXT_PUBLIC_LOGIN_REQUEST_SCOPE" ] && echo "NEXT_PUBLIC_LOGIN_REQUEST_SCOPE is not set" && exit 1
# split
configName="$(cut -d'=' -f1 <<<"$line")"
configValue="$(cut -d'=' -f2 <<<"$line")"
# get system env
envValue=$(env | grep "^$configName=" | grep -oe '[^=]*$')

echo "All required environment variables are set."
# if config found && configName starts with NEXT_PUBLIC
if [ -n "$configValue" ] && [ -n "$envValue" ]; then
# replace all
echo "Replace: ${configValue} with ${envValue}"
find $nextFolder \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#$configValue#$envValue#g"
fi
done <$envFilename

# Replace placeholders in .next files
echo "Replacing placeholders in .next files..."
# Add your placeholder replacement logic here

# Execute the main command
echo "Executing command: npm start"
exec npm start
echo "Starting Nextjs"
exec "$@"
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('next').NextConfig} */
const config = {
output: 'standalone',
webpack(config) {
// Grab the existing rule that handles SVG imports
const fileLoaderRule = config.module.rules.find((rule) =>

0 comments on commit b9061ee

Please sign in to comment.