Skip to content

Commit

Permalink
Feat: Slim down docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
Vija02 committed Mar 1, 2025
1 parent 7dd2281 commit 395e8ef
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ loadedPlugins
/uploads

packages/graphql/apollo-react-hooks.*
!packages/graphql/apollo-react-hooks.tsx
!packages/graphql/apollo-react-hooks.tsx

nft_results
node_modules_nft
1 change: 1 addition & 0 deletions apps/homepage/src/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = () => {
poweredByHeader: false,
distDir: `../.next`,
trailingSlash: false,
output: "standalone",
redirects() {
return [
{
Expand Down
13 changes: 12 additions & 1 deletion backend/server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
{
"name": "@repo/server",
"version": "0.0.0",
"main": "dist/index.js",
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"scripts": {
"build": "pkgroll",
"start": "sh ./scripts/process-public-next-env.sh && node -r @repo/config/env -r @repo/observability/initTracing dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"devDependencies": {
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.1.0",
"@vercel/nft": "^0.29.2",
"concurrently": "^9.1.0",
"nodemon": "^3.1.7",
"patch-package": "^8.0.0",
Expand Down
35 changes: 30 additions & 5 deletions production.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ RUN yarn workspace @repo/base-plugin build
COPY packages/ui/ /app/packages/ui/
RUN yarn workspace @repo/ui build

RUN node scripts/build_utils/extract_core.js

################################################################################
# Build stage 3 - Build server

Expand All @@ -97,6 +99,8 @@ RUN yarn worker build
COPY backend/server/ /app/backend/server/
RUN yarn server build

RUN node scripts/build_utils/extract_server.js

################################################################################
# Build stage 4 - Build client

Expand Down Expand Up @@ -148,8 +152,20 @@ RUN yarn workspace @repo/plugin-radio build
COPY plugins/worship-pads/ /app/plugins/worship-pads/
RUN yarn workspace @repo/plugin-worship-pads build

RUN node scripts/build_utils/extract_plugins.js

################################################################################
# Build stage 6 - Extract just the needed dependencies for runtime

FROM deps AS nft-extract

# We don't need to copy from core since both builder extends core
COPY --from=builder-server /app/nft_results /app/nft_results
COPY --from=builder-plugin /app/nft_results /app/nft_results
RUN node scripts/build_utils/copy_deps.js

################################################################################
# Build stage 6 - Combine deps and build, taking only needed files
# Build stage 7 - Combine deps and build, taking only needed files

FROM node:22-alpine AS clean
# Import our shared args
Expand All @@ -159,7 +175,17 @@ ARG ROOT_URL
# Copy over selectively just the tings we need, try and avoid the rest
COPY --from=deps /app/turbo.json /app/package.json /app/yarn.lock /app/.yarnrc.yml /app/
COPY --from=deps /app/.yarn /app/.yarn/
COPY --from=deps /app/node_modules /app/node_modules/
# Copy only the dependencies we need
COPY --from=nft-extract /app/node_modules_nft/node_modules /app/node_modules
# Get the standalone deps from next
COPY --from=builder-client /app/apps/homepage/.next/standalone/node_modules /app/node_modules
# Copy over the yarn state
COPY --from=deps /app/node_modules/.yarn-state.yml /app/node_modules/
# And also the @repo symlink
COPY --from=deps /app/node_modules/@repo /app/node_modules/@repo/
# And last but not least, get next specifically due to its complicated require setup. We'll get problems otherwise
COPY --from=deps /app/node_modules/next /app/node_modules/next/

COPY --from=builder-core /app/packages/graphql/ /app/packages/graphql/
COPY --from=builder-core /app/backend/config/ /app/backend/config/
COPY --from=builder-core /app/packages/observability/ /app/packages/observability/
Expand All @@ -174,9 +200,8 @@ COPY --from=builder-client /app/apps/remote/.env.production /app/apps/remote/
COPY --from=builder-client /app/apps/renderer/package.json /app/apps/renderer/
COPY --from=builder-client /app/apps/renderer/dist/ /app/apps/renderer/dist/
COPY --from=builder-client /app/apps/renderer/.env.production /app/apps/renderer/
COPY --from=builder-client /app/apps/homepage/package.json /app/apps/homepage/
COPY --from=builder-client /app/apps/homepage/src/next.config.js /app/apps/homepage/src/next.config.js
COPY --from=builder-client /app/apps/homepage/.next /app/apps/homepage/.next
COPY --from=builder-client /app/apps/homepage/.next/standalone/apps/homepage /app/apps/homepage
COPY --from=builder-client /app/apps/homepage/.next/static /app/apps/homepage/.next/static/

COPY --from=builder-server /app/backend/server/package.json /app/backend/server/
COPY --from=builder-server /app/backend/server/postgraphile.tags.jsonc /app/backend/server/
Expand Down
37 changes: 37 additions & 0 deletions scripts/build_utils/copy_deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require("fs");
const path = require("path");

const run = () => {
const targetDir = "./node_modules_nft";

const files = fs.readdirSync("./nft_results");

let filesToCopy = [];

for (const file of files) {
if (!file.endsWith("json")) {
continue;
}

const data = JSON.parse(
fs.readFileSync(path.join("./nft_results", file), "utf-8"),
);
filesToCopy.push(...data);
}

const uniqueFilesToCopy = Array.from(new Set(filesToCopy)).filter((x) =>
x.startsWith("node_modules"),
);

for (const filePath of uniqueFilesToCopy) {
const targetPath = path.join(targetDir, filePath);

// Create necessary directories
fs.mkdirSync(path.dirname(targetPath), { recursive: true });

// Copy the file
fs.copyFileSync(filePath, targetPath);
}
};

run();
31 changes: 31 additions & 0 deletions scripts/build_utils/extract_core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { nodeFileTrace } = require("@vercel/nft");
const fs = require("fs");

const files = [
"./packages/graphql/dist/index.js",
"./backend/config/dist/index.js",
"./backend/config/env.js",
"./packages/lib/dist/index.js",
"./packages/observability/dist/index.js",
"./packages/observability/initTracing.js",
"./backend/backend-shared/dist/index.js",
"./packages/base-plugin/dist/index.js",
"./packages/base-plugin/dist/server/index.js",
];

(async () => {
try {
const { fileList } = await nodeFileTrace(files, {
ignore: (prop) =>
!prop.startsWith("node_modules") || prop.includes("/@repo/"),
});

fs.mkdirSync("./nft_results", { recursive: true });
fs.writeFileSync(
"./nft_results/core.json",
JSON.stringify(Array.from(fileList)),
);
} catch (e) {
console.error(e);
}
})();
24 changes: 24 additions & 0 deletions scripts/build_utils/extract_plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { nodeFileTrace } = require("@vercel/nft");
const fs = require("fs");
const path = require("path");

const plugins = fs.readdirSync("./plugins");

const files = plugins.map((x) => path.join("./plugins", x, "dist/index.js"));

(async () => {
try {
const { fileList } = await nodeFileTrace(files, {
ignore: (prop) =>
!prop.startsWith("node_modules") || prop.includes("/@repo/"),
});

fs.mkdirSync("./nft_results", { recursive: true });
fs.writeFileSync(
"./nft_results/plugins.json",
JSON.stringify(Array.from(fileList)),
);
} catch (e) {
console.error(e);
}
})();
21 changes: 21 additions & 0 deletions scripts/build_utils/extract_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { nodeFileTrace } = require("@vercel/nft");
const fs = require("fs");

const files = ["./backend/server/dist/index.js"];

(async () => {
try {
const { fileList } = await nodeFileTrace(files, {
ignore: (prop) =>
!prop.startsWith("node_modules") || prop.includes("/@repo/"),
});

fs.mkdirSync("./nft_results", { recursive: true });
fs.writeFileSync(
"./nft_results/server.json",
JSON.stringify(Array.from(fileList)),
);
} catch (e) {
console.error(e);
}
})();
Loading

0 comments on commit 395e8ef

Please sign in to comment.