Skip to content

Commit

Permalink
build: new docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj Nandan Sharma authored and Raj Nandan Sharma committed Nov 13, 2024
1 parent c992ec2 commit 18e851a
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 54 deletions.
103 changes: 50 additions & 53 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,72 +1,69 @@
# Stage 1: Build stage
FROM node:18-alpine AS builder
FROM lsiobase/alpine:3.18 as base

WORKDIR /app
ENV TZ=Etc/GMT

# Copy package files first for better caching
COPY package*.json ./
RUN \
echo "**** install build packages ****" && \
apk add --no-cache \
nodejs \
npm && \
echo "**** cleanup ****" && \
rm -rf \
/root/.cache \
/tmp/*

# Install dependencies
RUN npm install
# set OS timezone specified by docker ENV
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Copy project files
COPY . .

# Build the application
RUN npm run build

# Stage 2: Run stage
FROM lsiobase/alpine:3.18
COPY docker/root/ /

# Set timezone and user
ENV TZ=Etc/GMT \
PUID=911 \
PGID=911 \
NODE_ENV=production \
PORT=3000

# Install Node.js and npm
RUN echo "**** install build packages ****" && \
apk add --no-cache \
nodejs \
npm && \
echo "**** cleanup ****" && \
rm -rf \
/root/.cache \
/tmp/*

# Configure timezone
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
echo $TZ > /etc/timezone
# build requires devDependencies which are not used by production deploy
# so build in a stage so we can copy results to clean "deploy" stage later
FROM base as build

WORKDIR /app

# Copy built files from builder stage
COPY --from=builder /app/build ./build
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/main.js ./
COPY --from=builder /app/build.js ./
COPY --from=builder /app/src ./src
COPY --from=builder /app/static ./static
COPY --from=builder /app/database ./database
COPY --from=builder /app/config ./config
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.env ./.env
COPY --chown=abc:abc . /app

RUN npm install \
&& chown -R root:root node_modules \
&& npm run build

FROM base as app

# copy package, required libs (npm,nodejs) results of build, prod entrypoint, and examples to be used to populate config dir
# to clean, new stage
COPY --chown=abc:abc package*.json ./
COPY --from=base /usr/local/bin /usr/local/bin
COPY --from=base /usr/local/lib /usr/local/lib

COPY --chown=abc:abc static /app/static
COPY --chown=abc:abc config /app/config
COPY --chown=abc:abc database /app/database
COPY --chown=abc:abc build.js /app/build.js
COPY --chown=abc:abc src/lib/server /app/src/lib/server
COPY --chown=abc:abc src/lib/helpers.js /app/src/lib/helpers.js

# Install production dependencies only
COPY --from=build --chown=abc:abc /app/build /app/build
COPY --from=build --chown=abc:abc /app/main.js /app/main.js



# Create and configure database directory
RUN mkdir -p /app/database && \
chown -R $PUID:$PGID /app/database && \
chmod -R 755 /app/database
ENV NODE_ENV=production

# Declare volume for persistence
VOLUME /app/database
# install prod depdendencies and clean cache
RUN npm install --omit=dev \
&& npm cache clean --force \
&& chown -R abc:abc node_modules

# Expose port
ARG webPort=3000
ENV PORT=$webPort
EXPOSE $PORT

# Set startup command
CMD ["sh", "-c", "node build.js && (node src/lib/server/startup.js & node main.js & wait)"]
# leave entrypoint blank!
# uses LSIO s6-init entrypoint with scripts
# that populate CONFIG_DIR with static dir, monitor/site.yaml when dir is empty
# and chown's all files so they are owned by proper user based on PUID/GUID env
Empty file.
36 changes: 36 additions & 0 deletions docker/root/etc/s6-overlay/s6-rc.d/init-app-config/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/with-contenv bash

# used https://github.com/linuxserver/docker-plex as a template

POPULATE_EXAMPLES=false

echo "-------------------------------------"
echo -e "Setting up app config directory based on CONFIG_DIR env: ${CONFIG_DIR}\n"

# make config folder if it does not exist
if [ ! -d "${CONFIG_DIR}" ]; then
echo "Directory does not exist! Creating..."
POPULATE_EXAMPLES=true
mkdir -p "${CONFIG_DIR}"
else
if [ "$(ls -A ${CONFIG_DIR})" ]; then
echo "Directory is not empty, not populating with defaults."
else
POPULATE_EXAMPLES=true
fi
fi

# add example configs
if [ "$POPULATE_EXAMPLES" = true ]; then
echo "Directory is empty, adding defaults..."
mkdir -p "${CONFIG_DIR}"/static
cp -r /app/static/. "${CONFIG_DIR}"/static
mv /app/config/monitors.example.yaml "${CONFIG_DIR}"/monitors.yaml
mv /app/config/site.example.yaml "${CONFIG_DIR}"/site.yaml
fi

# permissions
echo "chown'ing directory to ensure correct permissions."
chown -R abc:abc "${CONFIG_DIR}"
echo "Done!"
echo -e "-------------------------------------\n"
1 change: 1 addition & 0 deletions docker/root/etc/s6-overlay/s6-rc.d/init-app-config/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oneshot
1 change: 1 addition & 0 deletions docker/root/etc/s6-overlay/s6-rc.d/init-app-config/up
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/etc/s6-overlay/s6-rc.d/init-app-config/run
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions docker/root/etc/s6-overlay/s6-rc.d/svc-app/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/with-contenv bash

echo -e "\nApp is starting!"
export NODE_ENV=production
cd /app || exit

# Run build first
s6-setuidgid abc /usr/bin/node $NODE_ARGS /app/build.js && (
# Run startup and main in parallel
s6-setuidgid abc /usr/bin/node $NODE_ARGS /app/src/lib/server/startup.js &
s6-setuidgid abc /usr/bin/node $NODE_ARGS /app/main.js &
# Wait for both processes
wait
)
1 change: 1 addition & 0 deletions docker/root/etc/s6-overlay/s6-rc.d/svc-app/type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion src/lib/server/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let monitors = [];
let site = {};
const envSecrets = [];
const DATABASE_PATH = "./database";

console.log(process.env);
const Startup = async () => {
const FOLDER_MONITOR_JSON = DATABASE_PATH + "/monitors.json";
const monitors = fs.readJSONSync(FOLDER_MONITOR_JSON);
Expand Down

0 comments on commit 18e851a

Please sign in to comment.