From aadbee12bf71d164c49627cf4302b8b6a5990d33 Mon Sep 17 00:00:00 2001 From: starbirdtech383 Date: Fri, 12 May 2023 01:38:42 +0530 Subject: [PATCH] fix local docker and utility image build script --- .gitignore | 1 + build-local-docker-image.sh | 42 ++++++++++++++++++++++++ packages/nocodb/.gitignore | 3 ++ packages/nocodb/Dockerfile.local | 55 ++++++++++++++++++++++++++++++++ packages/nocodb/src/run/local.ts | 18 +++++++++++ 5 files changed, 119 insertions(+) create mode 100755 build-local-docker-image.sh create mode 100644 packages/nocodb/Dockerfile.local create mode 100644 packages/nocodb/src/run/local.ts diff --git a/.gitignore b/.gitignore index 82f59b54ce..2d333cbc36 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,7 @@ _site/ *.pid *.gz *.tmp +*.tmp.md *.bak *.swp logs/ diff --git a/build-local-docker-image.sh b/build-local-docker-image.sh new file mode 100755 index 0000000000..a4ccab4a89 --- /dev/null +++ b/build-local-docker-image.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# script to build local docker image. +# highlevel steps involved +# 1. build nocodb-sdk +# 2. build nc-gui +# 2a. static build of nc-gui +# 2b. copy nc-gui build to nocodb dir +# 3. build nocodb + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +#build nocodb-sdk +echo "Building nocodb-sdk" +cd ${SCRIPT_DIR}/packages/nocodb-sdk +npm ci +npm run build + +# build nc-gui +echo "Building nc-gui" +export NODE_OPTIONS="--max_old_space_size=16384" +# generate static build of nc-gui +cd ${SCRIPT_DIR}/packages/nc-gui +npm ci +npm run generate + +# copy nc-gui build to nocodb dir +rsync -rvzh --delete ./dist/ ${SCRIPT_DIR}/packages/nocodb/docker/nc-gui/ + +#build nocodb +# build nocodb ( pack nocodb-sdk and nc-gui ) +cd ${SCRIPT_DIR}/packages/nocodb +npm install +EE=true ./node_modules/.bin/webpack --config webpack.local.config.js +# remove nocodb-sdk since it's packed with the build +npm uninstall --save nocodb-sdk + +# build docker +docker build . -f Dockerfile.local -t nocodb-local + +echo 'docker image with tag "nocodb-local" built sussessfully. Use below sample command to run the container' +echo 'docker run -d -p 3333:8080 --name nocodb-local nocodb-local ' + diff --git a/packages/nocodb/.gitignore b/packages/nocodb/.gitignore index 22a836d74a..1a182ed6dd 100644 --- a/packages/nocodb/.gitignore +++ b/packages/nocodb/.gitignore @@ -41,4 +41,7 @@ export/** # test dbs test_sakila_?.db +# ignoring to avoid commiting those files +# nc-gui dir is copied for building local docker. /docker/main.js +/docker/nc-gui diff --git a/packages/nocodb/Dockerfile.local b/packages/nocodb/Dockerfile.local new file mode 100644 index 0000000000..dff9c15b7d --- /dev/null +++ b/packages/nocodb/Dockerfile.local @@ -0,0 +1,55 @@ +########### +# Builder +########### +FROM node:16.17.0-alpine3.15 as builder +WORKDIR /usr/src/app + +# install node-gyp dependencies +RUN apk add --no-cache python3 make g++ + +# Copy application dependency manifests to the container image. +# A wildcard is used to ensure both package.json AND package-lock.json are copied. +# Copying this separately prevents re-running npm ci on every code change. +COPY ./package*.json ./ +COPY ./docker/nc-gui/ ./docker/nc-gui/ +COPY ./docker/main.js ./docker/index.js +COPY ./docker/start-local.sh /usr/src/appEntry/start.sh +COPY ./public/css/*.css ./docker/public/css/ +COPY ./public/js/*.js ./docker/public/js/ +COPY ./public/favicon.ico ./docker/public/ + +# install production dependencies, +# reduce node_module size with modclean & removing sqlite deps, +# package built code into app.tar.gz & add execute permission to start.sh +RUN npm ci --omit=dev --quiet \ + && npx modclean --patterns="default:*" --ignore="nc-lib-gui/**,dayjs/**,express-status-monitor/**,@azure/msal-node/dist/**" --run \ + && rm -rf ./node_modules/sqlite3/deps \ + && tar -czf ../appEntry/app.tar.gz ./* \ + && chmod +x /usr/src/appEntry/start.sh + +########## +# Runner +########## +FROM alpine:3.15 +WORKDIR /usr/src/app + +ENV NC_DOCKER 0.6 +ENV NODE_ENV production +ENV PORT 8080 +ENV NC_TOOL_DIR=/usr/app/data/ + +RUN apk --update --no-cache add \ + nodejs \ + tar \ + dumb-init \ + curl \ + jq + +# Copy packaged production code & main entry file +COPY --from=builder /usr/src/appEntry/ /usr/src/appEntry/ + +EXPOSE 8080 +ENTRYPOINT ["/usr/bin/dumb-init", "--"] + +# Start Nocodb +CMD ["/usr/src/appEntry/start.sh"] diff --git a/packages/nocodb/src/run/local.ts b/packages/nocodb/src/run/local.ts new file mode 100644 index 0000000000..df29c1092a --- /dev/null +++ b/packages/nocodb/src/run/local.ts @@ -0,0 +1,18 @@ +import path from 'path'; +import cors from 'cors'; +import express from 'express'; + +import Noco from '../Noco'; + +const server = express(); +server.enable('trust proxy'); +server.use(cors()); +server.use('/dashboard', express.static(path.join(__dirname, 'nc-gui'))); +server.set('view engine', 'ejs'); + +(async () => { + const httpServer = server.listen(process.env.PORT || 8080, () => { + console.log(`App started successfully.\nVisit -> ${Noco.dashboardUrl}`); + }); + server.use(await Noco.init({}, httpServer, server)); +})().catch((e) => console.log(e));