Skip to content

Commit

Permalink
Optimize docker image size
Browse files Browse the repository at this point in the history
  • Loading branch information
paustint committed Sep 24, 2024
1 parent cad0401 commit c188348
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 1,225 deletions.
74 changes: 34 additions & 40 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
.editorconfig
.env
.env.example
.env.staging
.git
.github
.nx
.vscode
.workspace.code-workspace
app
apps/landing/.next
dist
logs
node_modules
npm-debug.log
test-results
tmp
yarn-error.log
# ignore everything
*

# System Files
.DS_Store
Thumbs.db
# Include directories
!/apps/api
!/apps/jetstream
!/apps/landing
!/custom-typings
!/libs
!/migrations
!/prisma
!/scripts

# Generated Docusaurus files
.docusaurus/
.cache-loader/
# Include specific files
!/yarn.lock
!/.eslintrc.json
!/.gitignore
!/babel.config.json
!/Dockerfile
!/formulon.d.ts
!/LICENSE.md
!/next-sitemap.config.js
!/nx.json
!/package.json
!/README.md
!/schema.prisma
!/seed-salesforce-api.ts
!/seed.sh
!/tsconfig.base.json
!/webpack-server.config.js
!/webpack-sw.config.js

# Next.js
.next

# Playwright
**/test-results
**/playwright-report
**/playwright/.cache
.nx

# Not required for core jetstream
apps/cron-tasks
apps/docs
apps/jetstream-e2e
# Ignore unnecessary files inside allowed directories
*#
*~
.DS_Store
Thumbs.db
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db

18 changes: 11 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ARG NODE_VERSION=20.10.0
ARG ENVIRONMENT=production

FROM node:${NODE_VERSION}-slim as base
FROM node:${NODE_VERSION}-slim AS base

# App lives here
WORKDIR /app
Expand All @@ -14,7 +14,7 @@ ARG YARN_VERSION=1.22.21
RUN npm install -g yarn@$YARN_VERSION --force

# Throw-away build stage to reduce size of final image
FROM base as build
FROM base AS build

# Install packages needed to build node modules
RUN apt-get update -qq && \
Expand All @@ -32,11 +32,15 @@ RUN yarn run db:generate
COPY --link . .

# Build application
RUN yarn build:core
RUN yarn build:landing

# Remove development dependencies
RUN yarn install --production=true
RUN yarn build:core && \
yarn build:landing && \
# Replace dependencies with only the ones required by API
yarn scripts:replace-deps && \
rm -rf .nx

# Remove development dependencies and unused prod dependecies
RUN yarn install --production=true && \
yarn add cross-env npm-run-all --save-dev

# Final stage for app image
FROM base
Expand Down
4 changes: 2 additions & 2 deletions apps/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/api/src",
"projectType": "application",
"tags": ["server"],
"targets": {
"build": {
"executor": "@nx/esbuild:esbuild",
Expand Down Expand Up @@ -92,6 +93,5 @@
"dependsOn": ["build"],
"command": "docker build -f apps/api/Dockerfile . -t api"
}
},
"tags": ["server"]
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"build:storybook": "storybook build -c libs/ui/.storybook -o dist/storybook -s node_modules/@salesforce-ux/design-system/assets/styles",
"build:web-extension:dev": "nx run jetstream-web-extension:build --configuration=development",
"build:web-extension": "nx run jetstream-web-extension:build",
"scripts:replace-deps": "node ./scripts/replace-package-deps.mjs",
"release": "dotenv release-it ${0}",
"release:build": "zx ./scripts/build-release.mjs",
"release:web-extension": "dotenv -- release-it --config .release-it-web-ext.json",
Expand All @@ -62,8 +63,8 @@
"generate:data-table-css": "nx workspace-generator convert-sass-to-css ui --filename /src/lib/data-table/data-table-styles.scss",
"generate:version": "zx ./scripts/generate-version.mjs",
"db:generate": "prisma generate",
"db:migrate": "prisma migrate deploy",
"db:seed": "chmod +x prisma/seed.sh && prisma db seed",
"db:migrate": "npx --yes prisma migrate deploy",
"db:seed": "chmod +x prisma/seed.sh && npx --yes prisma db seed",
"icons:build": "npm-run-all icons:build:*",
"icons:build:action": "npx @svgr/[email protected] --config-file .svgo-config.json -d ./libs/icon-factory/src/lib/icons/action ./node_modules/@salesforce-ux/design-system/assets/icons/action",
"icons:build:custom": "npx @svgr/[email protected] --config-file .svgo-config.json -d ./libs/icon-factory/src/lib/icons/custom ./node_modules/@salesforce-ux/design-system/assets/icons/custom",
Expand All @@ -87,7 +88,7 @@
},
"private": true,
"prisma": {
"seed": "ts-node prisma/seed-salesforce-api.ts"
"seed": "node prisma/seed-salesforce-api.mjs"
},
"devDependencies": {
"@babel/core": "^7.24.4",
Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions scripts/replace-package-deps.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from 'fs-extra';
import path from 'path';

async function replaceDependencies() {
try {
// Define the paths to the package.json files
const rootPackageJsonPath = path.resolve('package.json');
const distPackageJsonPath = path.resolve('dist/apps/api/package.json');

// Read the package.json files
const [rootPackageJsonData, distPackageJsonData] = await Promise.all([
fs.readFile(rootPackageJsonPath, 'utf-8'),
fs.readFile(distPackageJsonPath, 'utf-8'),
]);

// Parse the JSON data
const rootPackageJson = JSON.parse(rootPackageJsonData);
const distPackageJson = JSON.parse(distPackageJsonData);

const xlsx = rootPackageJson.dependencies.xlsx;

// Replace the dependencies and devDependencies
rootPackageJson.dependencies = distPackageJson.dependencies;
rootPackageJson.dependencies.xlsx = xlsx;
rootPackageJson.devDependencies = {};

// Write the updated package.json back to the root package.json file
await fs.writeFile(rootPackageJsonPath, JSON.stringify(rootPackageJson, null, 2), 'utf-8');

console.log('Dependencies successfully replaced in package.json');
} catch (error) {
console.error('Error replacing dependencies:', error);
}
}

// Run the function
replaceDependencies();
Loading

0 comments on commit c188348

Please sign in to comment.