-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* optimize: reducing docker image size to lower than 500mb (#263) * feat: support flyio for deploying backend service
- Loading branch information
Showing
13 changed files
with
245 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
docker-compose.yml | ||
Dockerfile | ||
node_modules | ||
dist | ||
.github | ||
.vscode | ||
.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ Thumbs.db | |
# Next.js | ||
.next | ||
.env.local | ||
.env.flyio | ||
.env | ||
pm2.json | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# ----------------------- base ----------------- | ||
FROM node:lts-alpine AS base | ||
|
||
USER root | ||
# Update image | ||
RUN apk update && apk upgrade --available && apk add --no-cache g++ make python3 | ||
|
||
COPY . /app | ||
WORKDIR /app | ||
|
||
# Add NX | ||
RUN yarn global add nx@latest | ||
|
||
# ----------------------- dependencies ----------------- | ||
FROM base AS deps | ||
|
||
RUN npm install --only=development | ||
|
||
# ------------------------ builder --------------- | ||
FROM base AS builder | ||
|
||
# add non root user | ||
# RUN addgroup -S namviek && \ | ||
# adduser -S -G namviek namviek | ||
|
||
WORKDIR /app | ||
|
||
# Argument to select the env file | ||
ARG ENV_FILE=.env | ||
# Copy the env file | ||
COPY ${ENV_FILE} .env | ||
|
||
COPY --from=deps /app/node_modules ./node_modules | ||
|
||
RUN yarn generate2 | ||
RUN yarn build:be | ||
|
||
# ------------------------ runner --------------------- | ||
FROM node:lts-alpine AS runner | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=builder /app/dist/packages/be-gateway ./ | ||
COPY ./packages/shared-models/src/prisma/schema.prisma . | ||
COPY ./yarn.lock . | ||
|
||
# Only need MONGODB_URL in .env file | ||
COPY .env .env | ||
|
||
# RUN yarn install --frozen-lockfile | ||
RUN yarn install | ||
|
||
|
||
# ---------------- TEST ------------ | ||
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma | ||
# -------------------------------- | ||
|
||
|
||
ENV PORT=3000 | ||
|
||
EXPOSE 3000 | ||
|
||
|
||
# CMD HOSTNAME="0.0.0.0" node main.js | ||
CMD ["node", "main.js"] | ||
# CMD ["node", "test.js"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# ----------------------- base ----------------- | ||
FROM node:lts-alpine AS base | ||
|
||
USER root | ||
# Update image | ||
RUN apk update && apk upgrade --available && apk add --no-cache g++ make python3 | ||
|
||
COPY . /app | ||
WORKDIR /app | ||
|
||
# Add NX | ||
RUN yarn global add nx@latest | ||
|
||
# ----------------------- dependencies ----------------- | ||
FROM base AS deps | ||
|
||
RUN npm install --only=development | ||
|
||
# ------------------------ builder --------------- | ||
FROM base AS builder | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=deps /app/node_modules ./node_modules | ||
|
||
RUN yarn generate2 | ||
RUN yarn build:be | ||
|
||
# ------------------------ runner --------------------- | ||
FROM node:lts-alpine AS runner | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=builder /app/dist/packages/be-gateway ./ | ||
COPY ./packages/shared-models/src/prisma/schema.prisma . | ||
COPY ./yarn.lock . | ||
|
||
RUN yarn install | ||
|
||
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma | ||
|
||
# Set both HOST and PORT explicitly | ||
ENV HOST=0.0.0.0 | ||
ENV PORT=3000 | ||
|
||
# Use a shell to start the app with explicit host and port | ||
CMD node main.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
FROM node:lts-alpine AS base | ||
|
||
USER root | ||
# Update image | ||
RUN apk update && apk upgrade --available && apk add --no-cache g++ make python3 | ||
|
||
COPY . /app | ||
WORKDIR /app | ||
|
||
# Add NX | ||
RUN yarn global add nx@latest | ||
|
||
# ----------------------- dependencies ----------------- | ||
FROM base AS deps | ||
|
||
# Install everything, I have no idea if we want --production | ||
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn \ | ||
yarn install --immutable | ||
|
||
|
||
# ----------------------- builder ----------------- | ||
|
||
FROM base AS builder | ||
|
||
# # add non root user | ||
# RUN addgroup -S namviek && \ | ||
# adduser -S -G namviek namviek | ||
|
||
WORKDIR /app | ||
|
||
# Argument to select the env file | ||
ARG ENV_FILE=.env | ||
|
||
# Copy the env file | ||
COPY ${ENV_FILE} .env | ||
|
||
COPY --from=deps /app/node_modules ./node_modules | ||
|
||
RUN yarn generate2 | ||
RUN yarn build:fe | ||
|
||
# ----------------------- runner ----------------- | ||
FROM node:lts-alpine AS runner | ||
|
||
# USER namviek | ||
WORKDIR /app | ||
|
||
COPY --from=builder /app/dist/packages/ui-app/.next/standalone ./ | ||
COPY --from=builder /app/dist/packages/ui-app/.next/static ./dist/packages/ui-app/.next/static | ||
COPY --from=builder /app/dist/packages/ui-app/public ./packages/ui-app/public | ||
|
||
RUN chmod -R 0777 * | ||
|
||
# Set environment variables | ||
ENV NODE_ENV=production | ||
ENV NEXT_TELEMETRY_DISABLED=1 | ||
ENV PORT=3000 | ||
|
||
# Expose the port the app runs on | ||
EXPOSE 3000 | ||
|
||
CMD HOSTNAME="0.0.0.0" node ./packages/ui-app/server.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const loop = () => { | ||
console.log('log', new Date()) | ||
setTimeout(() => { | ||
loop() | ||
}, 5000) | ||
} | ||
|
||
loop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
app = 'namviek' | ||
primary_region = 'sin' | ||
|
||
[build] | ||
dockerfile = 'docker/backend/Dockerfile-flyio' | ||
|
||
[env] | ||
JWT_REFRESH_EXPIRED = '2d' | ||
JWT_TOKEN_EXPIRED = '30m' | ||
JWT_VERIFY_USER_LINK_TOKEN_EXPIRED = '10m' | ||
HOST = '0.0.0.0' | ||
PORT = '3000' | ||
|
||
[http_service] | ||
internal_port = 3000 | ||
force_https = true | ||
auto_stop_machines = true | ||
auto_start_machines = true | ||
min_machines_running = 1 | ||
processes = ['app'] | ||
|
||
[scale] | ||
count = 1 | ||
max = 1 | ||
|
||
[[vm]] | ||
cpu_kind = 'shared' | ||
cpus = 1 | ||
memory_mb = 1024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters