Setup stats API container:

This commit is contained in:
Anthony Mineo 2020-08-31 16:27:31 -04:00
parent 9176889086
commit b3925a8fc6
10 changed files with 13775 additions and 55 deletions

2
build/api/.dockerignore Normal file
View file

@ -0,0 +1,2 @@
.git
node_modules

70
build/api/Dockerfile Normal file
View file

@ -0,0 +1,70 @@
# [ Stage 1 ] - install node modules
FROM node:12.18-alpine as builder
WORKDIR /build/app
RUN npm i -g @nestjs/cli
COPY ./app/api .
RUN npm install
ENV NODE_ENV=production
RUN nest build
# ============================
# ============================
# [ Stage 2 ]
FROM node:12.18-alpine
LABEL maintainer="Season 4 <webmaster@season4.io>"
RUN apk update && apk add --no-cache openssl bash curl
# Default envs as prod
ENV HOST=0.0.0.0 \
PORT=8080 \
NODE_ENV=production \
APP_NAME=NestJS
ENV APP_URL=https://${HOST}:${PORT}
# Setup pm2 as our node process manager
# https://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
RUN npm i -g pm2 @nestjs/cli
RUN mkdir /opt/node_app && chown node:node /opt/node_app && mkdir /localcert && chown node:node /localcert
RUN mkdir -p /opt/node_app/app/node_modules/ && chown node:node /opt/node_app/app/node_modules/
WORKDIR /opt/node_app
#USER node
# Generate a localhost cert
# RUN openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout /localcert/key.pem -out /localcert/cert.pem -subj "/C=US/ST=New Jersey/L=Warren/O=localhost/OU=IT/CN=127.0.0.1"
ENV PATH /opt/node_app/node_modules/.bin:$PATH
# Our App
WORKDIR /opt/node_app/app
# Set node modules outside our app to keep it clean
COPY --from=builder /build/app/node_modules/ /opt/node_app/node_modules/
COPY --from=builder /build/app/dist/ /opt/node_app/app/dist/
COPY ./app/api/package.json ./app/api/package-lock.json* ./
# Start script and config
COPY ./build/api/ecosystem._PROD_.config.js /opt/node_app/ecosystem._PROD_.config.js
COPY ./build/api/entrypoint.sh /entrypoint.sh
EXPOSE 8080
HEALTHCHECK --interval=20s --timeout=30s --start-period=5s --retries=5 \
CMD curl -f -k https://localhost:8443/healthz || exit 1
ENTRYPOINT [ "/entrypoint.sh" ]

View file

@ -0,0 +1,14 @@
module.exports = {
apps: [
{
name: process.env.APP_NAME,
cwd: '/opt/node_app/app/',
script: 'npm run start:dev --interpreter bash',
// Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
instances: 1,
autorestart: true,
watch: false,
ignore_watch: [ 'node_modules', 'dist' ]
}
]
};

View file

@ -0,0 +1,10 @@
module.exports = {
apps: [
{
cwd: '/opt/node_app/app/',
script: 'node dist/main --interpreter bash',
autorestart: true,
instances: 1
}
]
};

41
build/api/entrypoint.sh Executable file
View file

@ -0,0 +1,41 @@
#!/bin/bash
echo "Node ENV: $NODE_ENV"
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
# file_env 'APP_KEY'
# # Exit if APP_KEY is missing, this key is important!
# if [ -z "$APP_KEY" ]
# then
# echo >&2 "[ERROR] No ENV for APP_KEY or APP_KEY_FILE found!"
# echo >&2 "Run the ./generate-adonis-app-key.sh script or provide one at runtime"
# exit 1
# fi
if [[ "$NODE_ENV" == "production" ]]; then
pm2-runtime start /opt/node_app/ecosystem._PROD_.config.js
else
npm install && \
pm2-runtime start /opt/node_app/ecosystem._DEV_.config.js
fi