Skip to content

Instantly share code, notes, and snippets.

@blueskyfish
Last active September 15, 2020 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blueskyfish/c567fd287d630eae615bcfe7e630559d to your computer and use it in GitHub Desktop.
Save blueskyfish/c567fd287d630eae615bcfe7e630559d to your computer and use it in GitHub Desktop.
A docker file in order to build node application with multi stage

Node Application

Work with Multi Stage Dockerfile

Folder

  • backend contains the backend source code
  • frontend contains the frontend source code
#
# Multipart dockerfile for build the application and the delivery docker image
#
FROM node:12-alpine AS builder
WORKDIR /builder
RUN \
apk add --update tree openssl && \
mkdir -p /builder/frontend/src /builder/app /builder/backend/src
# Copy Frontend
COPY frontend/*.json /builder/frontend/
COPY frontend/src /builder/frontend/src
# Copy Backend
COPY backend/*.json /builder/backend/
COPY backend/src/ /builder/backend/src/
# RUN \
# tree /builder/backend
# Build Frontend
RUN \
cd /builder/frontend && \
npm ci && \
npm run build:prod && \
mv /builder/frontend/dist/xev /builder/app/html && \
echo "> Info: Fontend is built success... Path: \"/builder/app/html\""
# Build Backend
RUN \
cd /builder/backend && \
npm ci && \
npm run build && \
mv /builder/backend/dist /builder/app/lib && \
cp /builder/backend/*.json /builder/app/ && \
mv /builder/backend/node_modules /builder/app/node_modules && \
echo "> Info: Backend is built successful.. Path: \"/builder/app\""
# Generates public / private RSA keys
RUN \
mkdir /builder/app/etc && \
# generates the private key with 2048 bits
openssl genrsa -out /builder/app/etc/prod-private.pem 2048 && \
# generates the public key
openssl rsa -in /builder/app/etc/prod-private.pem -pubout -out /builder/app/etc/prod-public.pem && \
# View the folder content
tree -I node_modules /builder/app && \
echo "Info: Build is finish... Path: \"/builder/app\""
FROM node:12-alpine AS delivery
WORKDIR /app
COPY docker/start.sh /app
COPY --from=builder /builder/app /app
RUN \
chmod +x /app/start.sh
EXPOSE $PORT
CMD ["/app/start.sh"]
#!/usr/bin/env sh
# checked the required Envs
if [[ -z "${PORT}" ]]; then
echo "> Error: Server Port is required"
exit 1
fi
if [[ -z "${DB_HOST}" ]]; then
echo "> Error: Database Host is required"
exit 1
fi
if [[ -z "${DB_PORT}" ]]; then
echo "> Error: Database Port is required"
exit 1
fi
if [[ -z "${DB_USER}" ]]; then
echo "> Error: Database User is required"
exit 1
fi
if [[ -z "${DB_PASSWORD}" ]]; then
echo "> Error: Database User Password is required"
exit 1
fi
if [[ -z "${DB_DATABASE}" ]]; then
echo "> Error: Database Name is required"
exit 1
fi
if [[ -z "${DIGEST_SECRET}" ]]; then
echo "> Error: Digest Secrets is required"
exit 1
fi
export AUTH_PRIVATE_URL=/app/etc/prod-private.pem
export AUTH_PUBLIC_URL=/app/etc/prod-public.pem
#
# application stage is always "prod" in the AWS environment
#
export APP_STAGE=prod
# Change working directory
cd /app
# Node
node lib/main.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment