Skip to content

Commit

Permalink
feat: support flyio (#264)
Browse files Browse the repository at this point in the history
* optimize: reducing docker image size to lower than 500mb (#263)
* feat: support flyio for deploying backend service
  • Loading branch information
hudy9x authored Nov 22, 2024
1 parent a61491f commit 8c596db
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 12 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
docker-compose.yml
Dockerfile
node_modules
dist
.github
.vscode
.git
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Thumbs.db
# Next.js
.next
.env.local
.env.flyio
.env
pm2.json

Expand Down
22 changes: 14 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
services:
backend:
container_name: backend
container_name: namviek-backend
build:
context: .
dockerfile: ./docker/Dockerfile
dockerfile: ./docker/backend/Dockerfile
# dockerfile: ./docker/Dockerfile
args:
- ENV_FILE=.env.local
image: namviek
Expand All @@ -14,8 +15,9 @@ services:
- MONGODB_URL=mongodb://mongodb:27017/namviek?replicaSet=rs0&authSource=admin&directConnection=true
- AWS_MINIO_ENDPOINT=http://localhost:9000
ports:
- '3333:3333'
command: /bin/sh -c "./docker/be-entrypoint.sh && yarn backend"
- '3333:3000'
# command: /bin/sh -c "./docker/be-entrypoint.sh && yarn backend"
command: /bin/sh -c "node ./main.js"
healthcheck:
test: curl --fail http://localhost:3333 || exit 1
interval: 30s
Expand All @@ -29,13 +31,17 @@ services:
- namviek-network

frontend:
container_name: frontend
image: namviek
container_name: namviek-frontend
build:
context: .
dockerfile: ./docker/frontend/Dockerfile
# dockerfile: ./docker/Dockerfile
env_file:
- .env.local
ports:
- '3000:3000'
command: /bin/sh -c "./docker/fe-entrypoint.sh && yarn prod:fe"
- '3005:3000'
# command: /bin/sh -c "./docker/fe-entrypoint.sh && yarn prod:fe"
command: /bin/sh -c 'HOSTNAME="0.0.0.0" node ./packages/ui-app/server.js'
depends_on:
- backend
networks:
Expand Down
67 changes: 67 additions & 0 deletions docker/backend/Dockerfile
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"]

47 changes: 47 additions & 0 deletions docker/backend/Dockerfile-flyio
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
62 changes: 62 additions & 0 deletions docker/frontend/Dockerfile
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
8 changes: 8 additions & 0 deletions docker/test.js
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()
29 changes: 29 additions & 0 deletions fly.toml
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
11 changes: 11 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
"affected": {
"defaultBase": "master"
},
"pluginsConfig": {
"@nx/js": {
"analyzeSourceFiles": true
}
},
"targetDefaults": {
"build": {
"executor": "@nx/webpack:webpack",
"options": {
"generatePackageJson": true
}
},
"test": {
"inputs": ["default", "^default", "{workspaceRoot}/jest.preset.js"]
},
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"seed": "prisma db seed --schema=.\\packages\\shared-models\\src\\prisma\\schema.prisma",
"generate2": "prisma generate --schema=./packages/shared-models/src/prisma/schema.prisma",
"pushdb2": "prisma db push --schema=./packages/shared-models/src/prisma/schema.prisma",
"seed2": "prisma db seed --schema=./packages/shared-models/src/prisma/schema.prisma",
"postinstall": "yarn generate2"
"seed2": "prisma db seed --schema=./packages/shared-models/src/prisma/schema.prisma"
},
"private": true,
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/be-gateway/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"tsConfig": "packages/be-gateway/tsconfig.app.json",
"assets": ["packages/be-gateway/src/assets"],
"isolatedConfig": true,
"webpackConfig": "packages/be-gateway/webpack.config.js"
"webpackConfig": "packages/be-gateway/webpack.config.js",
"generatePackageJson": true
},
"configurations": {
"development": {},
Expand Down
1 change: 1 addition & 0 deletions packages/ui-app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { composePlugins, withNx } = require('@nx/next')
* @type {import('@nx/next/plugins/with-nx').WithNxOptions}
**/
const nextConfig = {
output: 'standalone',
nx: {
// Set this to true if you would like to use SVGR
// See: https://github.com/gregberge/svgr
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"baseUrl": ".",
"paths": {
"@/components/*": ["packages/ui-app/app/_components/*"],
"@/events/*": ["packages/ui-app/app/_events/*"],
"@/features/*": ["packages/ui-app/app/_features/*"],
"@/hooks/*": ["packages/ui-app/app/_hooks/*"],
"@/events/*": ["packages/ui-app/app/_events/*"],
"@/services/*": ["packages/ui-app/services/*"],
"@/store/*": ["packages/ui-app/store/*"],
"@be/storage": ["packages/be-storage/src/index.ts"],
Expand Down

0 comments on commit 8c596db

Please sign in to comment.